3200. Maximum Height of a Triangle

3200. Maximum Height of a Triangle

Description

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

All the balls in a particular row should be the same color, and adjacent rows should have different colors.

Return the maximum height of the triangle that can be achieved.

Example 1:

1
2
3
Input: red = 2, blue = 4

Output: 3

Explanation:

The only possible arrangement is shown above.

Example 2:

1
2
3
Input: red = 2, blue = 1

Output: 2

Explanation:



The only possible arrangement is shown above.

Example 3:

1
2
3
Input: red = 1, blue = 1

Output: 1

Example 4:

1
2
3
Input: red = 10, blue = 1

Output: 2

Explanation:



The only possible arrangement is shown above.

Constraints:

  • 1 <= red, blue <= 100

Hints/Notes

  • Weekly Contest 404

Solution

Language: C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int maxHeightOfTriangle(int red, int blue) {
int odd = 0, even = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2) {
odd += i;
} else {
even += i;
}
if ((max(odd, even) > max(red, blue)) ||
(min(odd, even) > min(red, blue))) {
return i - 1;
}
}
return 1;
}
};