Skip to content

feat: add weekly contest 403 #3178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 184 additions & 0 deletions solution/3200-3299/3200.Maximum Height of a Triangle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3200.Maximum%20Height%20of%20a%20Triangle/README.md
---

<!-- problem:start -->

# [3200. 三角形的最大高度](https://leetcode.cn/problems/maximum-height-of-a-triangle)

[English Version](/solution/3200-3299/3200.Maximum%20Height%20of%20a%20Triangle/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给你两个整数 <code>red</code> 和 <code>blue</code>,分别表示红色球和蓝色球的数量。你需要使用这些球来组成一个三角形,满足第 1 行有 1 个球,第 2 行有 2 个球,第 3 行有 3 个球,依此类推。</p>

<p>每一行的球必须是 <strong>相同 </strong>颜色,且相邻行的颜色必须<strong> 不同</strong>。</p>

<p>返回可以实现的三角形的 <strong>最大 </strong>高度。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">red = 2, blue = 4</span></p>

<p><strong>输出:</strong> 3</p>

<p><strong>解释:</strong></p>

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3200.Maximum%20Height%20of%20a%20Triangle/images/brb.png" style="width: 300px; height: 240px; padding: 10px;" /></p>

<p>上图显示了唯一可能的排列方式。</p>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">red = 2, blue = 1</span></p>

<p><strong>输出:</strong> <span class="example-io">2</span></p>

<p><strong>解释:</strong></p>

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3200.Maximum%20Height%20of%20a%20Triangle/images/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
上图显示了唯一可能的排列方式。</p>
</div>

<p><strong class="example">示例 3:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">red = 1, blue = 1</span></p>

<p><strong>输出:</strong> <span class="example-io">1</span></p>
</div>

<p><strong class="example">示例 4:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">red = 10, blue = 1</span></p>

<p><strong>输出:</strong> <span class="example-io">2</span></p>

<p><strong>解释:</strong></p>

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3200.Maximum%20Height%20of%20a%20Triangle/images/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
上图显示了唯一可能的排列方式。</p>
</div>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= red, blue &lt;= 100</code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:模拟

我们可以枚举第一行的颜色,然后模拟构造三角形,计算最大高度。

时间复杂度 $O(\sqrt{n})$,其中 $n$ 为红色球和蓝色球的数量。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def maxHeightOfTriangle(self, red: int, blue: int) -> int:
ans = 0
for k in range(2):
c = [red, blue]
i, j = 1, k
while i <= c[j]:
c[j] -= i
j ^= 1
ans = max(ans, i)
i += 1
return ans
```

#### Java

```java
class Solution {
public int maxHeightOfTriangle(int red, int blue) {
int ans = 0;
for (int k = 0; k < 2; ++k) {
int[] c = {red, blue};
for (int i = 1, j = k; i <= c[j]; j ^= 1, ++i) {
c[j] -= i;
ans = Math.max(ans, i);
}
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int maxHeightOfTriangle(int red, int blue) {
int ans = 0;
for (int k = 0; k < 2; ++k) {
int c[2] = {red, blue};
for (int i = 1, j = k; i <= c[j]; j ^= 1, ++i) {
c[j] -= i;
ans = max(ans, i);
}
}
return ans;
}
};
```

#### Go

```go
func maxHeightOfTriangle(red int, blue int) (ans int) {
for k := 0; k < 2; k++ {
c := [2]int{red, blue}
for i, j := 1, k; i <= c[j]; i, j = i+1, j^1 {
c[j] -= i
ans = max(ans, i)
}
}
return
}
```

#### TypeScript

```ts
function maxHeightOfTriangle(red: number, blue: number): number {
let ans = 0;
for (let k = 0; k < 2; ++k) {
const c: [number, number] = [red, blue];
for (let i = 1, j = k; i <= c[j]; ++i, j ^= 1) {
c[j] -= i;
ans = Math.max(ans, i);
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
182 changes: 182 additions & 0 deletions solution/3200-3299/3200.Maximum Height of a Triangle/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
---
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3200.Maximum%20Height%20of%20a%20Triangle/README_EN.md
---

<!-- problem:start -->

# [3200. Maximum Height of a Triangle](https://leetcode.com/problems/maximum-height-of-a-triangle)

[中文文档](/solution/3200-3299/3200.Maximum%20Height%20of%20a%20Triangle/README.md)

## Description

<!-- description:start -->

<p>You are given two integers <code>red</code> and <code>blue</code> representing the count of red and blue colored balls. You have to arrange these balls to form a triangle such that the 1<sup>st</sup> row will have 1 ball, the 2<sup>nd</sup> row will have 2 balls, the 3<sup>rd</sup> row will have 3 balls, and so on.</p>

<p>All the balls in a particular row should be the <strong>same</strong> color, and adjacent rows should have <strong>different</strong> colors.</p>

<p>Return the <strong>maximum</strong><em> height of the triangle</em> that can be achieved.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 4</span></p>

<p><strong>Output:</strong> 3</p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3200.Maximum%20Height%20of%20a%20Triangle/images/brb.png" style="width: 300px; height: 240px; padding: 10px;" /></p>

<p>The only possible arrangement is shown above.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 2, blue = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3200.Maximum%20Height%20of%20a%20Triangle/images/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 1, blue = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">1</span></p>
</div>

<p><strong class="example">Example 4:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">red = 10, blue = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3200-3299/3200.Maximum%20Height%20of%20a%20Triangle/images/br.png" style="width: 150px; height: 135px; padding: 10px;" /><br />
The only possible arrangement is shown above.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= red, blue &lt;= 100</code></li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1: Simulation

We can enumerate the color of the first row, then simulate the construction of the triangle, calculating the maximum height.

The time complexity is $O(\sqrt{n})$, where $n$ is the number of red and blue balls. The space complexity is $O(1)$.

<!-- tabs:start -->

#### Python3

```python
class Solution:
def maxHeightOfTriangle(self, red: int, blue: int) -> int:
ans = 0
for k in range(2):
c = [red, blue]
i, j = 1, k
while i <= c[j]:
c[j] -= i
j ^= 1
ans = max(ans, i)
i += 1
return ans
```

#### Java

```java
class Solution {
public int maxHeightOfTriangle(int red, int blue) {
int ans = 0;
for (int k = 0; k < 2; ++k) {
int[] c = {red, blue};
for (int i = 1, j = k; i <= c[j]; j ^= 1, ++i) {
c[j] -= i;
ans = Math.max(ans, i);
}
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int maxHeightOfTriangle(int red, int blue) {
int ans = 0;
for (int k = 0; k < 2; ++k) {
int c[2] = {red, blue};
for (int i = 1, j = k; i <= c[j]; j ^= 1, ++i) {
c[j] -= i;
ans = max(ans, i);
}
}
return ans;
}
};
```

#### Go

```go
func maxHeightOfTriangle(red int, blue int) (ans int) {
for k := 0; k < 2; k++ {
c := [2]int{red, blue}
for i, j := 1, k; i <= c[j]; i, j = i+1, j^1 {
c[j] -= i
ans = max(ans, i)
}
}
return
}
```

#### TypeScript

```ts
function maxHeightOfTriangle(red: number, blue: number): number {
let ans = 0;
for (let k = 0; k < 2; ++k) {
const c: [number, number] = [red, blue];
for (let i = 1, j = k; i <= c[j]; ++i, j ^= 1) {
c[j] -= i;
ans = Math.max(ans, i);
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
14 changes: 14 additions & 0 deletions solution/3200-3299/3200.Maximum Height of a Triangle/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int maxHeightOfTriangle(int red, int blue) {
int ans = 0;
for (int k = 0; k < 2; ++k) {
int c[2] = {red, blue};
for (int i = 1, j = k; i <= c[j]; j ^= 1, ++i) {
c[j] -= i;
ans = max(ans, i);
}
}
return ans;
}
};
10 changes: 10 additions & 0 deletions solution/3200-3299/3200.Maximum Height of a Triangle/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
func maxHeightOfTriangle(red int, blue int) (ans int) {
for k := 0; k < 2; k++ {
c := [2]int{red, blue}
for i, j := 1, k; i <= c[j]; i, j = i+1, j^1 {
c[j] -= i
ans = max(ans, i)
}
}
return
}
Loading
Loading