Skip to content

Commit 91017b5

Browse files
[N-0] add 683
1 parent ccbfd3a commit 91017b5

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Your ideas/fixes/algorithms are more than welcome!
4848
|686|[Repeated String Match](https://leetcode.com/problems/repeated-string-match/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_686.java) | O(n*(m+n)) | O(m+n) | Easy |
4949
|685|[Redundant Connection II](https://leetcode.com/problems/redundant-connection-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_685.java) | O(n) | O(n) | Hard | Union Find
5050
|684|[Redundant Connection](https://leetcode.com/problems/redundant-connection/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_684.java) | O(n) | O(n) | Medium | Union Find
51+
|683|[K Empty Slots](https://leetcode.com/problems/k-empty-slots/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_683.java) | O(n) | O(n) | Hard |
5152
|682|[Baseball Game](https://leetcode.com/problems/baseball-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_682.java) | O(n) | O(1) | Easy |
5253
|681|[Next Closest Time](https://leetcode.com/problems/parents-closest-time/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_681.java) | O(1) | O(1) | Medium |
5354
|680|[Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_680.java) | O(n) | O(1) | Easy | String

src/main/java/com/fishercoder/solutions/_683.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
* 683. K Empty Slots
55
*
66
* There is a garden with N slots. In each slot, there is a flower.
7+
*
78
* The N flowers will bloom one by one in N days.
9+
*
810
* In each day, there will be exactly one flower blooming and it will be in the status of blooming since then.
11+
*
912
* Given an array flowers consists of number from 1 to N. Each number in the array represents the place where the flower will open in that day.
10-
* For example, flowers[i] = x means that the unique flower that blooms at day i will be at position x, where i and x will be in the range from 1 to N.
13+
* For example, flowers[i] = x means that the unique flower that blooms at day i will be at position x,
14+
* where i and x will be in the range from 1 to N.
15+
*
1116
* Also given an integer k, you need to output in which day there exists two flowers in the status of blooming,
1217
* and also the number of flowers between them is k and these flowers are not blooming.
1318
* If there isn't such day, output -1.
@@ -33,7 +38,23 @@ public class _683 {
3338

3439
public static class Solution1 {
3540
public int kEmptySlots(int[] flowers, int k) {
36-
return -1;
41+
int[] days = new int[flowers.length];
42+
for (int i = 0; i < flowers.length; i++) {
43+
days[flowers[i] - 1] = i + 1;
44+
}
45+
int left = 0;
46+
int right = k + 1;
47+
int result = Integer.MAX_VALUE;
48+
for (int i = 0; right < flowers.length; i++) {
49+
if (days[i] < days[left] || days[i] <= days[right]) {
50+
if (i == right) {
51+
result = Math.min(result, Math.max(days[left], days[right]));
52+
}
53+
left = i;
54+
right = k + 1 + i;
55+
}
56+
}
57+
return result == Integer.MAX_VALUE ? -1 : result;
3758
}
3859
}
3960
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._683;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _683Test {
10+
private static _683.Solution1 solution1;
11+
private static int[] flowers;
12+
private static int k;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _683.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
flowers = new int[]{1, 3, 2};
22+
k = 1;
23+
assertEquals(2, solution1.kEmptySlots(flowers, k));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
flowers = new int[]{1, 2, 3};
29+
k = 1;
30+
assertEquals(-1, solution1.kEmptySlots(flowers, k));
31+
}
32+
33+
}

0 commit comments

Comments
 (0)