Talentd
FleetCode

3200. Maximum Height of a Triangle

Easy44.8% acceptance1 company

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.

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

Asked at 1 company

Hints open one at a time

Hint 1
Count the max height using both possibilities. That is, red ball as top and blue ball as top.
Hint 2
For counting the max height, use a simple for loop and remove the number of balls required at this level.
4:54
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def maxHeightOfTriangle(self, red: int, blue: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
2
4
Input
2
1
Input
1
1
Input
10
1
Your code is saved on this device.Solution