LeetCode 1089 Duplicate Zeros

LeetCode 1089 Duplicate Zeros Problem

Download Code
class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
        """
        Do not return anything, modify arr in-place instead.
        """
        move_pos = 0
        last_pos = len(arr) - 1
        for i in range(last_pos + 1):
            # Only check [0, lastPos - movePos]
            if i > last_pos - move_pos:
                break
            if arr[i] == 0:
                # Special case
                if i == last_pos - move_pos:
                    arr[last_pos] = 0
                    last_pos -= 1
                    break
                move_pos += 1
        last_pos -= move_pos
        for i in range(last, -1, -1):
            if arr[i] == 0:
                arr[i + move_pos] = 0
                move_pos -= 1
                arr[i + move_pos] = 0
            else:
                arr[i + move_pos] = arr[i]
Download Duplicate Zeros.py

List of all Duplicate Zeros problems

Leetcode 1089 Duplicate Zeros problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.