[LeetCode] Maximum Height of a Triangle

3200. Maximum Height of a Triangle

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 1st row will have 1 ball, the 2nd row will have 2 balls, the 3rd 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
int helper(int a, int b, int c) {
int res = 0;
while(a >= c) {
a -= c;
res++;
c++;
swap(a,b);
}
return res;
}
public:
int maxHeightOfTriangle(int red, int blue) {
return max(helper(red,blue,1), helper(blue,red,1));
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/06/30/PS/LeetCode/maximum-height-of-a-triangle/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.