Skip to content

Commit 172cec7

Browse files
committed
fix LeetCode test
1 parent e4db606 commit 172cec7

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

mug-guava/src/test/java/com/google/mu/collect/LeetCodeTest.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,35 @@ public class LeetCodeTest {
1616

1717
static abstract class Koko {
1818
@Test public void verify() {
19-
assertThat(minEatingSpeed(new int[] {3,6,7,11}, 8)).isEqualTo(4);
20-
assertThat(minEatingSpeed(new int[] {30,11,23,4,20}, 5)).isEqualTo(30);;
19+
assertThat(minEatingSpeed(new int[] {7}, 1)).isEqualTo(7);
20+
assertThat(minEatingSpeed(new int[] {3, 6, 7, 11}, 100)).isEqualTo(1);
21+
assertThat(minEatingSpeed(new int[] {30, 11, 23, 4, 20}, 5)).isEqualTo(30);
22+
assertThat(minEatingSpeed(new int[] {9}, 2)).isEqualTo(5);
23+
assertThat(minEatingSpeed(new int[] {1, 1, 1, 1}, 4)).isEqualTo(1);
24+
assertThat(minEatingSpeed(new int[] {1000000000}, 2)).isEqualTo(500000000);
2125
}
2226

2327
abstract int minEatingSpeed(int[] piles, int h);
28+
29+
static int eat(int[] piles, int speed) {
30+
int time = 0;
31+
for (int pile : piles) {
32+
time += (pile + speed - 1) / speed;
33+
}
34+
return time;
35+
}
2436
}
2537

2638
public static class ManualKoKo extends Koko {
2739
// Generated by GPT
2840
@Override int minEatingSpeed(int[] piles, int h) {
2941
int low = 1, high = Arrays.stream(piles).max().getAsInt();
3042
while (low < high) {
31-
int mid = low + (high - low) / 2;
32-
int hours = 0;
33-
for (int pile : piles) {
34-
hours += (pile + mid - 1) / mid;
35-
}
36-
if (hours > h) {
37-
low = mid + 1;
43+
int mid = low + (high - low) / 2;
44+
if (eat(piles, mid) <= h) {
45+
high = mid;
3846
} else {
39-
high = mid;
47+
low = mid + 1;
4048
}
4149
}
4250
return low;
@@ -46,12 +54,8 @@ public static class ManualKoKo extends Koko {
4654
public static class MugKoko extends Koko {
4755
@Override int minEatingSpeed(int[] piles, final int h) {
4856
return BinarySearch.forInts(Range.closed(1, Arrays.stream(piles).max().getAsInt()))
49-
.insertionPointFor(
50-
(lo, speed, hi) ->
51-
Integer.compare(
52-
Arrays.stream(piles).map(pile -> (pile + speed - 1) / speed).sum(),
53-
h))
54-
.floor();
57+
.insertionPointFor((lo, speed, hi) -> eat(piles, speed) <= h ? -1 : 1)
58+
.ceiling();
5559
}
5660
}
5761

0 commit comments

Comments
 (0)