Skip to content

Commit 2a5b4bd

Browse files
add 1705
1 parent 10e13af commit 2a5b4bd

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ _If you like this project, please leave me a star._ ★
5252
|1711|[Count Good Meals](https://leetcode.com/problems/count-good-meals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1711.java) ||Medium|Array, HashTable, Two Pointers|
5353
|1710|[Maximum Units on a Truck](https://leetcode.com/problems/maximum-units-on-a-truck/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1710.java) ||Easy|Greedy, Sort|
5454
|1708|[Largest Subarray Length K](https://leetcode.com/problems/largest-subarray-length-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1708.java) ||Easy|Array, Greedy|
55+
|1705|[Maximum Number of Eaten Apples](https://leetcode.com/problems/maximum-number-of-eaten-apples/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1705.java) ||Medium|Heap, Greedy|
5556
|1704|[Determine if String Halves Are Alike](https://leetcode.com/problems/determine-if-string-halves-are-alike/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1704.java) ||Easy|String|
5657
|1700|[Number of Students Unable to Eat Lunch](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1700.java) ||Easy|Array|
5758
|1694|[Reformat Phone Number](https://leetcode.com/problems/reformat-phone-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1694.java) ||Easy|String|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.PriorityQueue;
4+
5+
public class _1705 {
6+
public static class Solution1 {
7+
public int eatenApples(int[] apples, int[] days) {
8+
/**we sort the heap by its expiration dates, we'll eat the earliest expiration apples first*/
9+
PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]);
10+
int eatenApples = 0;
11+
for (int i = 0; i < apples.length || !minHeap.isEmpty(); i++) {
12+
if (i < apples.length) {
13+
minHeap.offer(new int[]{i + days[i], apples[i]});
14+
}
15+
while (!minHeap.isEmpty() && (minHeap.peek()[0] <= i || minHeap.peek()[1] <= 0)) {
16+
minHeap.poll();
17+
}
18+
if (!minHeap.isEmpty()) {
19+
eatenApples++;
20+
minHeap.peek()[1]--;
21+
}
22+
}
23+
return eatenApples;
24+
}
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1705;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1705Test {
10+
private static _1705.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1705.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(7, solution1.eatenApples(new int[]{1, 2, 3, 5, 2}, new int[]{3, 2, 1, 4, 2}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(5, solution1.eatenApples(new int[]{3, 0, 0, 0, 0, 2}, new int[]{3, 0, 0, 0, 0, 2}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(5, solution1.eatenApples(new int[]{9, 2}, new int[]{3, 5}));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(8, solution1.eatenApples(new int[]{2, 1, 1, 4, 5}, new int[]{10, 10, 6, 4, 2}));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)