2025-08-02 10:02:23 +0000 UTC
Duplicate Zeros
Categories:
Links
Code
class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
possible_dups = 0
length_ = len(arr) - 1
for left in range(length_ + 1):
if left > length_ - possible_dups:
break
if arr[left] == 0:
if left == length_ - possible_dups:
arr[length_] = 0
length_ -= 1
break
possible_dups += 1
last = length_ - possible_dups
for i in range(last, -1, -1):
if arr[i] == 0:
arr[i + possible_dups] = 0
possible_dups -= 1
arr[i + possible_dups] = 0
else:
arr[i + possible_dups] = arr[i]