Move Zeros

Problem Summary

(Write in your own words, not copied from LeetCode. This forces comprehension.)

What is Being Asked?

(One sentence on the actual task.)

Key Observations

(Patterns, constraints, or hints in the problem statement.)

Approach Taken

(Step-by-step logic or pseudocode before coding.)

Why This Works

(Explain the core reason the solution is correct.)

Main Concepts Used

(Mark the CS concepts or algorithms used.)

Time & Space Complexity

Code

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        # Input:  [4, 2, 4, 0, 0, 3, 0, 5, 1, 0]
        # Output: [4, 2, 4, 3, 5, 1, 0, 0, 0, 0]
        # left always moves one
        # right moves until condition

        # l, r = 0, 0
        # while l <= r and l < len(nums) and r < len(nums):
        #     while r < len(nums) and nums[r] == 0:
        #         r += 1
            
        #     if r < len(nums):
        #         nums[l], nums[r] = nums[r], nums[l]
        #         l += 1
        #         r += 1

        ## optimized solution
        left = 0
        for right, rightValue in enumerate(nums):
            if rightValue:
                nums[left], nums[right] = nums[right], nums[left]
                left += 1

Common Mistakes / Things I Got Stuck On