Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d90e3fe

Browse files
committed
1089_Duplicate_Zeros
1 parent 85132d6 commit d90e3fe

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ Also, there are open source implementations for basic data structs and algorithm
217217
| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/977_Squares_of_a_Sorted_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/977_Squares_of_a_Sorted_Array.java) | 1. Sort, O(nlogn) and O(n)<br>2. Two point, O(n) and O(n) |
218218
| 973 | [K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/973_K_Closest_Points_to_Origin.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/973_K_Closest_Points_to_Origin.java) | 1. Sort and get 0-K, O(nlogn) and O(1)<br>2. Min Heap, O(nlogk) and O(k) |
219219
| 1064 | [Fixed Point](https://leetcode.com/problems/fixed-point/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1064_Fixed_Point.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1064_Fixed_Point.java) | 1. Go through index and value, until find solution encounter index < value, O(n) and O(1)<br>2. Binary search, O(logn) and O(1) |
220+
| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1089_Duplicate_Zeros.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1089_Duplicate_Zeros.java) | 2 Pass, store last position and final move steps, O(n) and O(1) |
220221
| 1260 | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1260_Shift_2D_Grid.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1260_Shift_2D_Grid.java) | Final position of each element can be computed according to k, m and n, e.g., k == mn, then don't move, O(mn) and O(mn) |
221222
| 1337 | [The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1337_The_K_Weakest_Rows_in_a_Matrix.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1337_The_K_Weakest_Rows_in_a_Matrix.java) | Check by row, from left to right, until encount first zero, O(mn) and O(1) |
222223

java/1089_Duplicate_Zeros.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public void duplicateZeros(int[] arr) {
3+
int movePos = 0;
4+
int lastPos = arr.length - 1;
5+
// Only check [0, lastPos - movePos]
6+
for (int i = 0; i <= lastPos - movePos; i++) {
7+
if (arr[i] == 0) {
8+
// Special case
9+
if (i == lastPos - movePos) {
10+
arr[lastPos] = 0;
11+
lastPos--;
12+
break;
13+
}
14+
movePos++;
15+
}
16+
}
17+
lastPos = lastPos - movePos;
18+
for (int i = lastPos; i >= 0; i--) {
19+
if (arr[i] == 0) {
20+
arr[i + movePos] = 0;
21+
movePos--;
22+
arr[i + movePos] = 0;
23+
} else {
24+
arr[i + movePos] = arr[i];
25+
}
26+
}
27+
}
28+
}

python/1089_Duplicate_Zeros.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def duplicateZeros(self, arr: List[int]) -> None:
3+
"""
4+
Do not return anything, modify arr in-place instead.
5+
"""
6+
move_pos = 0
7+
last_pos = len(arr) - 1
8+
for i in range(last_pos + 1):
9+
# Only check [0, lastPos - movePos]
10+
if i > last_pos - move_pos:
11+
break
12+
if arr[i] == 0:
13+
# Special case
14+
if i == last_pos - move_pos:
15+
arr[last_pos] = 0
16+
last_pos -= 1
17+
break
18+
move_pos += 1
19+
last_pos -= move_pos
20+
for i in range(last, -1, -1):
21+
if arr[i] == 0:
22+
arr[i + move_pos] = 0
23+
move_pos -= 1
24+
arr[i + move_pos] = 0
25+
else:
26+
arr[i + move_pos] = arr[i]

0 commit comments

Comments
 (0)