Maximum Height of a Triangle - Problem
Imagine you're building a colorful ball pyramid! You have red and blue colored balls, and you want to stack them into a triangle where:
- The 1st row has 1 ball
- The 2nd row has 2 balls
- The 3rd row has 3 balls
- And so on...
Rules:
- All balls in the same row must be the same color
- Adjacent rows must have different colors
Given the count of red and blue balls, return the maximum height of the triangle you can build!
Example: With 2 red and 4 blue balls, you could build:
Row 1: 1 red ball
Row 2: 2 blue balls
Row 3: 3 red balls (but you only have 1 red left!)
So maximum height is 2.
Input & Output
example_1.py β Basic Case
$
Input:
red = 2, blue = 4
βΊ
Output:
3
π‘ Note:
Starting with blue: Row 1 (1 blue), Row 2 (2 red), Row 3 (3 blue). Total height = 3. Starting with red only gives height 2.
example_2.py β Equal Balls
$
Input:
red = 4, blue = 9
βΊ
Output:
3
π‘ Note:
Best pattern: Row 1 (1 red), Row 2 (2 blue), Row 3 (3 red). Uses 4 red and 2 blue balls. Height = 3.
example_3.py β Minimum Case
$
Input:
red = 1, blue = 1
βΊ
Output:
1
π‘ Note:
Can only build 1 row with either 1 red ball or 1 blue ball. Height = 1.
Constraints
- 1 β€ red, blue β€ 100
- Adjacent rows must have different colors
- All balls in the same row must be the same color
Visualization
Tap to expand
Understanding the Visualization
1
π΄ Try Red First
Start with red balls in row 1, then blue in row 2, red in row 3...
2
π΅ Try Blue First
Start with blue balls in row 1, then red in row 2, blue in row 3...
3
π Compare Heights
Both approaches will give different heights - pick the maximum!
4
π Optimal Solution
The winning pattern gives us the tallest possible pyramid
Key Takeaway
π― Key Insight: The alternating color constraint means we only need to check 2 scenarios - starting with red or starting with blue!
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code