Leetcode Note: Go - Duplicate Zeros

Duplicate Zeros - LeetCode
https://leetcode.com/problems/duplicate-zeros/

  • Go 言語で取り組んだメモ

回答

Duplicate Zeros - LeetCode
https://leetcode.com/problems/duplicate-zeros/solution/

Go simple solution beats 100%/100%, O(n) complexity, O(1) space - LeetCode Discuss
https://leetcode.com/problems/duplicate-zeros/discuss/589488/Go-simple-solution-beats-100100-O(n)-complexity-O(1)-space

func duplicateZeros(arr []int)  {
    zeros := 0

    for _, v := range arr {
        if v == 0 {
            zeros++
        }
    }

    for i := len(arr) - 1; i >= 0; i-- {
        if arr[i] == 0 {
            if zeros + i < len(arr) {
                arr[zeros + i] = 0
            }
            
            if zeros - 1 + i < len(arr) {
                arr[zeros - 1 + i] = 0
            }
            
            zeros--
        } else if i + zeros < len(arr) {
            arr[zeros + i] = arr[i]
        }
    }
}