↓Skip to main content
  1. LeetCode/

LeetCode 1089: Duplicate Zeros

·2 mins· ·
LeetCode Easy Array Two-Pointers
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-10-18

  • Submissions: Runtime Error → Wrong Answer → Accepted

Approach
#

Each value ends at “its index + the zeros before it”. Moving back to front avoids overwriting values not yet moved (Two Pointers Template 2):

  • zeros starts as the total number of zeros.
  • From the end, nums[i] goes to i + zeros (dropped if out of range).
  • When nums[i] is 0, first write the duplicate 0 at i + zeros, decrement zeros, then write the original 0 at i + zeros.

My Attempts
#

Attempt 1 worked front to back and went out of range; attempt 2 moved back to front but wrote each zero only once (no duplicate): [0, 0, 3] should become [0, 0, 0]; it stayed [0, 0, 3].

Solution
#

class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
        n, zeros = len(arr), arr.count(0)
        for i in range(n - 1, -1, -1):
            if arr[i] == 0:
                if i + zeros < n:
                    arr[i + zeros] = 0                   # 複製出來的 0
                zeros -= 1
            if i + zeros < n:
                arr[i + zeros] = arr[i]

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Moving front to back overwrites unprocessed values.
  • A zero is written twice, one slot apart.

Interview Explanation
#

Each value shifts by the number of zeros before it, so I count the zeros, then move from the back, writing an extra 0 and reducing the shift on each zero. Time O(n), space O(1).

Related

LeetCode 977: Squares of a Sorted Array
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 977 note: the largest squares are at the two ends, so compare the ends with two pointers and fill the result from the back
LeetCode 905: Sort Array By Parity
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 905 note: a write pointer swaps even numbers to the front, just like move zeroes
LeetCode 922: Sort Array By Parity II
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 922 note: pointers i over even slots and j over odd slots, each stepping by two; when slot i holds an odd number, move j to a misplaced even and swap
LeetCode 821: Shortest Distance to a Character
·2 mins
LeetCode Easy Array Two-Pointers String
LeetCode 821 note: one left-to-right pass for the distance to the nearest c on the left, then a right-to-left pass taking the minimum
LeetCode 832: Flipping an Image
·2 mins
LeetCode Easy Array Two-Pointers Bit-Manipulation Matrix
LeetCode 832 note: reverse each row and invert its bits, which two pointers can do at once
LeetCode 455: Assign Cookies
·2 mins
LeetCode Easy Array Two-Pointers Greedy Sorting
LeetCode 455 note: sort children and cookies, satisfying the least greedy child with the smallest cookie that suffices; each pointer walks once