Skip to content

Commit 15deee8

Browse files
solves #3200: Maximum Height of a Triangle
1 parent 64f4c45 commit 15deee8

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -923,4 +923,4 @@
923923
| 3190 | [Find Minimum Operations to Make All Elements Divisible by Three](https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three) | [![Java](assets/java.png)](src/FindMinimumOperationsToMakeAllElementsDivisibleByThree.java) | |
924924
| 3194 | [Minimum Average of Smallest and Largest Elements](https://leetcode.com/problems/minimum-average-of-smallest-and-largest-elements) | [![Java](assets/java.png)](src/MinimumAverageOfSmallestAndLargestElements.java) | |
925925
| 3199 | [Count Triplets With Even XOR Set Bits I](https://leetcode.com/problems/count-triplets-with-even-xor-set-bits-i) | | |
926-
| 3200 | [Maximum Height of a Triangle](https://leetcode.com/problems/maximum-height-of-a-triangle) | | |
926+
| 3200 | [Maximum Height of a Triangle](https://leetcode.com/problems/maximum-height-of-a-triangle) | [![Java](assets/java.png)](src/MaximumHeightOfATriangle.java) | |

Diff for: src/MaximumHeightOfATriangle.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://leetcode.com/problems/maximum-height-of-a-triangle
2+
// T: O(log(n) + log(m))
3+
// S: O(1)
4+
5+
public class MaximumHeightOfATriangle {
6+
public static int maxHeightOfTriangle(int red, int blue) {
7+
return Math.max(possibleRows(red, blue), possibleRows(blue, red));
8+
}
9+
10+
private static int possibleRows(int oddRowBalls, int evenRowBalls) {
11+
final int evenRows = possibleEvenRows(evenRowBalls);
12+
final int oddRows = possibleOddRows(oddRowBalls);
13+
return Math.min(oddRows, evenRows + 1) + Math.min(oddRows, evenRows);
14+
}
15+
16+
private static int possibleOddRows(int balls) {
17+
return (int) Math.sqrt(balls);
18+
}
19+
20+
/*
21+
k - (-1 + sqrt(1 + 4b)) / 2 = 0
22+
k = (-1 + sqrt(1 + 4b)) / 2
23+
*/
24+
private static int possibleEvenRows(int balls) {
25+
return (int) ((Math.sqrt(1 + 4 * balls) - 1) / 2);
26+
}
27+
28+
public static void main(String[] args) {
29+
System.out.println(maxHeightOfTriangle(10, 1));
30+
}
31+
}

0 commit comments

Comments
 (0)