Skip to content

Commit 5a98d2c

Browse files
add 1151
1 parent e11d2ba commit 5a98d2c

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ _If you like this project, please leave me a star._ ★
407407
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java)| |Easy||
408408
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java) | |Easy||
409409
|1152|[Analyze User Website Visit Pattern](https://leetcode.com/problems/analyze-user-website-visit-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1152.java) |[:tv:](https://youtu.be/V510Lbtrm5s) |Medium|HashTable, Sort, Array|
410+
|1151|[Minimum Swaps to Group All 1's Together](https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1151.java) ||Medium|Array, Sliding Window|
410411
|1150|[Check If a Number Is Majority Element in a Sorted Array](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1150.java)|[:tv:](https://youtu.be/-t2cdVs9cKk) |Easy||
411412
|1146|[Snapshot Array](https://leetcode.com/problems/snapshot-array/)|[Javascript](./javascript/_1146.js)| | Easy ||
412413
|1143|[Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1143.java) | |Medium||String, DP
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1151 {
4+
public static class Solution1 {
5+
/**
6+
* My completely original solution on 11/4/2021.
7+
* Typical sliding window problem/solution
8+
*/
9+
public int minSwaps(int[] data) {
10+
int oneCount = 0;
11+
for (int d : data) {
12+
if (d == 1) {
13+
oneCount++;
14+
}
15+
}
16+
if (oneCount <= 1) {
17+
return 0;
18+
}
19+
int left = 0;
20+
int right = 0;
21+
int ones = 0;
22+
int zeroes = 0;
23+
int minSwaps = data.length;
24+
while (right < data.length) {
25+
if (data[right] == 1) {
26+
ones++;
27+
} else {
28+
zeroes++;
29+
}
30+
if (ones + zeroes == oneCount) {
31+
minSwaps = Math.min(minSwaps, zeroes);
32+
if (data[left] == 1) {
33+
ones--;
34+
} else {
35+
zeroes--;
36+
}
37+
left++;
38+
}
39+
right++;
40+
}
41+
return minSwaps;
42+
}
43+
}
44+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1151;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1151Test {
10+
private static _1151.Solution1 solution1;
11+
private static int[] data;
12+
private static int expected;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1151.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
data = new int[]{1, 0, 1, 0, 1};
22+
expected = 1;
23+
assertEquals(expected, solution1.minSwaps(data));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
data = new int[]{0, 0, 0, 1, 0};
29+
expected = 0;
30+
assertEquals(expected, solution1.minSwaps(data));
31+
}
32+
33+
@Test
34+
public void test3() {
35+
data = new int[]{1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1};
36+
expected = 3;
37+
assertEquals(expected, solution1.minSwaps(data));
38+
}
39+
40+
@Test
41+
public void test4() {
42+
data = new int[]{1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1};
43+
expected = 8;
44+
assertEquals(expected, solution1.minSwaps(data));
45+
}
46+
47+
@Test
48+
public void test5() {
49+
data = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
50+
expected = 0;
51+
assertEquals(expected, solution1.minSwaps(data));
52+
}
53+
54+
}

0 commit comments

Comments
 (0)