Skip to content

Commit 04174df

Browse files
add 3936
1 parent d4069a8 commit 04174df

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

  • paginated_contents/algorithms/4th_thousand
  • src/main/java/com/fishercoder/solutions/fourththousand

‎paginated_contents/algorithms/4th_thousand/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
| 3945 | [Digit Frequency Score](https://leetcode.com/problems/digit-frequency-score/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3945.java) | | Easy |
1313
| 3941 | [Password Strength](https://leetcode.com/problems/password-strength/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3941.java) | | Easy |
1414
| 3940 | [Limit Occurrences in Sorted Array](https://leetcode.com/problems/limit-occurrences-in-sorted-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3940.java) | | Easy |
15+
| 3936 | [Minimum Swaps to Move Zeros to End](https://leetcode.com/problems/minimum-swaps-to-move-zeros-to-end/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3936.java) | | Easy |
1516
| 3925 | [Concatenate Array With Reverse](https://leetcode.com/problems/concatenate-array-with-reverse/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3925.java) | | Easy |
1617
| 3913 | [Sort Vowels by Frequency](https://leetcode.com/problems/sort-vowels-by-frequency/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3913.java) | | Easy |
1718
| 3908 | [Valid Digit Number](https://leetcode.com/problems/valid-digit-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3908.java) | | Easy |
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
public class _3936 {
4+
public static class Solution1 {
5+
/**
6+
* My completely original solution:
7+
* two pointer technique:
8+
* left pointer keeps looking for the zero number
9+
* right pointer keeps looking for the non-zero number
10+
* once we find a pair, increase the swap count;
11+
* if we don't find a pair, keep moving both pointers.
12+
*
13+
*/
14+
public int minimumSwaps(int[] nums) {
15+
int minsSwaps = 0;
16+
int left = 0;
17+
int right = nums.length - 1;
18+
while (left < right) {
19+
while (left < right && nums[right] == 0) {
20+
right--;
21+
}
22+
if (left >= right) {
23+
return minsSwaps;
24+
}
25+
while (left < right && nums[left] != 0) {
26+
left++;
27+
}
28+
if (left >= right) {
29+
return minsSwaps;
30+
}
31+
minsSwaps++;
32+
left++;
33+
right--;
34+
}
35+
return minsSwaps;
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)