Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2a5b4bd

Browse files
add 1705
1 parent10e13af commit2a5b4bd

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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|
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.PriorityQueue;
4+
5+
publicclass_1705 {
6+
publicstaticclassSolution1 {
7+
publicinteatenApples(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 =newPriorityQueue<>((a,b) ->a[0] -b[0]);
10+
inteatenApples =0;
11+
for (inti =0;i <apples.length || !minHeap.isEmpty();i++) {
12+
if (i <apples.length) {
13+
minHeap.offer(newint[]{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+
returneatenApples;
24+
}
25+
}
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1705;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1705Test {
10+
privatestatic_1705.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1705.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(7,solution1.eatenApples(newint[]{1,2,3,5,2},newint[]{3,2,1,4,2}));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(5,solution1.eatenApples(newint[]{3,0,0,0,0,2},newint[]{3,0,0,0,0,2}));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(5,solution1.eatenApples(newint[]{9,2},newint[]{3,5}));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals(8,solution1.eatenApples(newint[]{2,1,1,4,5},newint[]{10,10,6,4,2}));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp