Leetcode 1168 Solution

This article provides solution to leetcode question 1168 (duplicate-zeros)

https://leetcode.com/problems/duplicate-zeros

Solution

class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
        """
        Do not return anything, modify arr in-place instead.
        """
        r2 = len(arr) - 1
        orglen = len(arr)

        cnt = sum([1 for v in arr if v == 0])
        r1 = r2 + cnt

        while r2 >= 0:
            if arr[r2] == 0:
                if r1 < orglen:
                    arr[r1] = 0
                r1 -= 1

                if r1 < orglen:
                    arr[r1] = 0
                r1 -= 1
            else:
                if r1 < orglen:
                    arr[r1] = arr[r2]
                r1 -= 1
            r2 -= 1