Skip to content

Commit ea0b1aa

Browse files
add 2591
1 parent 1458f2d commit ea0b1aa

File tree

3 files changed

+76
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+76
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
| 2641 | [Cousins in Binary Tree II](https://leetcode.com/problems/cousins-in-binary-tree-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2641.java) | | Medium |Tree, BFS, HashTable
2424
| 2596 | [Check Knight Tour Configuration](https://leetcode.com/problems/check-knight-tour-configuration/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2596.java) | [:tv:](https://youtu.be/OBht8NT_09c) | Medium |
2525
| 2595 | [Number of Even and Odd Bits](https://leetcode.com/problems/number-of-even-and-odd-bits/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2595.java) | | Easy |
26+
| 2591 | [Distribute Money to Maximum Children](https://leetcode.com/problems/distribute-money-to-maximum-children/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2591.java) | | Easy |
2627
| 2586 | [Count the Number of Vowel Strings in Range](https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2586.java) | | Easy |
2728
| 2583 | [Kth Largest Sum in a Binary Tree](https://leetcode.com/problems/kth-largest-sum-in-a-binary-tree/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2583.java) | | Medium |
2829
| 2582 | [Pass the Pillow](https://leetcode.com/problems/pass-the-pillow/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2582.java) | | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import java.util.PriorityQueue;
4+
5+
public class _2591 {
6+
public static class Solution1 {
7+
public int distMoney(int money, int children) {
8+
if (money / children == 8 && money % children == 0) {
9+
return children;
10+
}
11+
if (money < children) {
12+
return -1;
13+
}
14+
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
15+
for (int i = 0; i < children; i++) {
16+
minHeap.offer(1);
17+
money--;
18+
}
19+
int maxEights = 0;
20+
while (!minHeap.isEmpty() && money > 0) {
21+
Integer curr = minHeap.poll();
22+
if (money < 7) {
23+
curr += money;
24+
minHeap.offer(curr);
25+
break;
26+
} else if (minHeap.size() > 0) {
27+
money -= 7;
28+
maxEights++;
29+
} else if (minHeap.size() == 0) {
30+
break;
31+
}
32+
}
33+
if (!minHeap.isEmpty() && minHeap.peek() == 4) {
34+
maxEights--;
35+
}
36+
return maxEights;
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2591;
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 _2591Test {
10+
private static _2591.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _2591.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.distMoney(13, 3));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(1, solution1.distMoney(17, 2));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(1, solution1.distMoney(20, 3));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(2, solution1.distMoney(16, 2));
35+
}
36+
}

0 commit comments

Comments
 (0)