Maximum Height of a Triangle Problem
Description
LeetCode Problem 3200.
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:
Input: red = 2, blue = 4
Output: 3
Explanation:

The only possible arrangement is shown above.
Example 2:
Input: red = 2, blue = 1
Output: 2
Explanation:

The only possible arrangement is shown above.
Example 3:
Input: red = 1, blue = 1
Output: 1
Example 4:
Input: red = 10, blue = 1
Output: 2
Explanation:

The only possible arrangement is shown above.
Constraints:
1 <= red, blue <= 100
Solution
Approach: Simulation
Row i needs i balls, and colors alternate between rows. Only the color of the first row is a choice, so try both.
For each starting color, build rows 1, 2, 3, ... taking row i from the current color's balls while enough remain, and switch color after every row. The largest number of rows built over the two attempts is the answer.
Complexity: O(sqrt(n)) time and O(1) space, since the heights grow until about sqrt(n) balls are used.
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