Skip to content

Commit ae3737f

Browse files
authored
Update LeetCode_875_KokoEatingBananas.java
1 parent a9827ce commit ae3737f

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

250310/LeetCode_875_KokoEatingBananas.java

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// 첫번째 풀이 : 29ms
2+
13
class Solution {
24
public int minEatingSpeed(int[] piles, int h) {
35
int left = 1;
@@ -10,12 +12,12 @@ public int minEatingSpeed(int[] piles, int h) {
1012
while (left <= right) {
1113
int mid = left + (right - left) / 2;
1214

13-
int count = 0;
15+
int hour = 0;
1416
for (int pile : piles) {
15-
count += Math.ceil((double) pile / mid);
17+
hour += Math.ceil((double) pile / mid);
1618
}
1719

18-
if (count <= h) {
20+
if (hour <= h) {
1921
right = mid - 1;
2022
} else {
2123
left = mid + 1;
@@ -25,3 +27,29 @@ public int minEatingSpeed(int[] piles, int h) {
2527
return left;
2628
}
2729
}
30+
31+
// 두번째 풀이 : 7ms
32+
33+
class Solution {
34+
public int minEatingSpeed(int[] piles, int h) {
35+
int left = 1, right = 0;
36+
for (int pile : piles) right = Math.max(right, pile);
37+
38+
while (left < right) {
39+
int mid = left + (right - left) / 2;
40+
41+
int hour = 0;
42+
for (int pile : piles) {
43+
hour += (pile + mid - 1) / mid;
44+
}
45+
46+
if (hour <= h) {
47+
right = mid;
48+
} else {
49+
left = mid + 1;
50+
}
51+
}
52+
53+
return left;
54+
}
55+
}

0 commit comments

Comments
 (0)