Skip to content

Commit 8b385c2

Browse files
add 875
1 parent 6f0918c commit 8b385c2

File tree

3 files changed

+99
-0
lines changed
  • paginated_contents/algorithms/1st_thousand
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

3 files changed

+99
-0
lines changed

paginated_contents/algorithms/1st_thousand/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
| 880 | [Decoded String at Index](https://leetcode.com/problems/decoded-string-at-index/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_880.java) | | Medium | Stack
6767
| 877 | [Stone Game](https://leetcode.com/problems/stone-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_877.java) | | Medium | Math, DP, Minimax
6868
| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_876.java) | | Easy |
69+
| 875 | [Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_875.java) | | Medium | Binary Search
6970
| 872 | [Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_872.java) | | Easy | DFS, recursion
7071
| 870 | [Advantage Shuffle](https://leetcode.com/problems/advantage-shuffle/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_870.java) | | Medium | Array, Greedy
7172
| 868 | [Binary Gap](https://leetcode.com/problems/binary-gap/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_868.java) | | Easy |
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
public class _875 {
4+
public static class Solution1 {
5+
public int minEatingSpeed(int[] piles, int h) {
6+
long left = Long.MAX_VALUE;
7+
long right = Long.MIN_VALUE;
8+
for (int pile : piles) {
9+
left = Math.min(left, pile);
10+
right = Math.max(right, pile);
11+
}
12+
left /= h;
13+
while (left + 1 < right) {
14+
long mid = left + (right - left) / 2;
15+
if (possibleSpeed((int) mid, piles, h)) {
16+
right = mid;
17+
} else {
18+
left = mid;
19+
}
20+
}
21+
return possibleSpeed((int) left, piles, h) ? (int) left : (int) (left + 1);
22+
}
23+
24+
private boolean possibleSpeed(int speed, int[] piles, int hour) {
25+
if (speed <= 0) {
26+
return false;
27+
}
28+
long usedHours = 0;
29+
for (int pile : piles) {
30+
if (pile <= speed) {
31+
usedHours++;
32+
} else {
33+
usedHours += pile / speed;
34+
pile %= speed;
35+
usedHours += pile > 0 ? 1 : 0;
36+
}
37+
}
38+
return usedHours <= hour;
39+
}
40+
}
41+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fishercoder.firstthousand;
2+
3+
import com.fishercoder.solutions.firstthousand._875;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _875Test {
10+
private static _875.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _875.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(4, solution1.minEatingSpeed(new int[]{3, 6, 7, 11}, 8));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(30, solution1.minEatingSpeed(new int[]{30, 11, 23, 4, 20}, 5));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(23, solution1.minEatingSpeed(new int[]{30, 11, 23, 4, 20}, 6));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(2, solution1.minEatingSpeed(new int[]{2, 2}, 2));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(2, solution1.minEatingSpeed(new int[]{312884470}, 312884469));
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals(500000000, solution1.minEatingSpeed(new int[]{1000000000}, 2));
45+
}
46+
47+
@Test
48+
public void test7() {
49+
assertEquals(1, solution1.minEatingSpeed(new int[]{312884470}, 968709470));
50+
}
51+
52+
@Test
53+
public void test8() {
54+
assertEquals(3, solution1.minEatingSpeed(new int[]{805306368, 805306368, 805306368}, 1000000000));
55+
}
56+
57+
}

0 commit comments

Comments
 (0)