Skip to content

Commit 55f4dab

Browse files
add 1482
1 parent b25a834 commit 55f4dab

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ _If you like this project, please leave me a star._ ★
249249
|1487|[Making File Names Unique](https://leetcode.com/problems/making-file-names-unique/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1487.java) | |Medium|HashTable, String|
250250
|1486|[XOR Operation in an Array](https://leetcode.com/problems/xor-operation-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1486.java) | |Medium|Array, Bit Manipulation|
251251
|1485|[Clone Binary Tree With Random Pointer](https://leetcode.com/problems/clone-binary-tree-with-random-pointer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1485.java) | |Medium|HashTable, Tree, DFS, BFS|
252+
|1482|[Minimum Number of Days to Make m Bouquets](https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1482.java) | |Medium|Array, Binary Search|
252253
|1481|[Least Number of Unique Integers after K Removals](https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1481.java) | |Medium|Array, Sort|
253254
|1480|[Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1480.java), [C++](../master/cpp/_1480.cpp)| |Easy|Array|
254255
|1476|[Subrectangle Queries](https://leetcode.com/problems/subrectangle-queries/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1476.java) | |Medium|Array|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1482 {
4+
public static class Solution1 {
5+
/**
6+
* https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/discuss/686316/JavaC%2B%2BPython-Binary-Search
7+
*/
8+
public int minDays(int[] bloomDay, int m, int k) {
9+
int n = bloomDay.length;
10+
int left = 1;
11+
int right = Integer.MAX_VALUE;
12+
if (m * k > n) {
13+
return -1;
14+
}
15+
while (left < right) {
16+
int mid = left + (right - left) / 2;
17+
int flower = 0;
18+
int bouq = 0;
19+
for (int j = 0; j < n; j++) {
20+
if (bloomDay[j] > mid) {
21+
flower = 0;
22+
} else if (++flower >= k) {
23+
bouq++;
24+
flower = 0;
25+
}
26+
}
27+
if (bouq < m) {
28+
left = mid + 1;
29+
} else {
30+
right = mid;
31+
}
32+
}
33+
return left;
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1482;
4+
import com.fishercoder.solutions._2024;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _1482Test {
11+
private static _1482.Solution1 solution1;
12+
private static int expected;
13+
private static int[] bloomDay;
14+
private static int m;
15+
private static int k;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _1482.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
expected = 3;
25+
bloomDay = new int[]{1, 10, 3, 10, 2};
26+
m = 3;
27+
k = 1;
28+
assertEquals(expected, solution1.minDays(bloomDay, m, k));
29+
}
30+
31+
@Test
32+
public void test2() {
33+
expected = -1;
34+
bloomDay = new int[]{1, 10, 3, 10, 2};
35+
m = 3;
36+
k = 2;
37+
assertEquals(expected, solution1.minDays(bloomDay, m, k));
38+
}
39+
40+
@Test
41+
public void test3() {
42+
expected = 9;
43+
bloomDay = new int[]{1, 10, 2, 9, 3, 8, 4, 7, 5, 6};
44+
m = 4;
45+
k = 2;
46+
assertEquals(expected, solution1.minDays(bloomDay, m, k));
47+
}
48+
}

0 commit comments

Comments
 (0)