← Back

Squares of a Sorted Array

The biggest square is always at one end — fill the result back to front.

Easy LeetCode ↗ 1 / 1
func sortedSquares(nums []int) []int {

}
func sortedSquares(nums []int) []int {
    // Micropattern: negatives make the largest squares live at the ends.
}
func sortedSquares(nums []int) []int {
    // Micropattern: negatives make the largest squares live at the ends.
    n := len(nums)
    res := make([]int, n)
}
func sortedSquares(nums []int) []int {
    // Micropattern: negatives make the largest squares live at the ends.
    n := len(nums)
    res := make([]int, n)

    left, right := 0, n-1
}
func sortedSquares(nums []int) []int {
    // Micropattern: negatives make the largest squares live at the ends.
    n := len(nums)
    res := make([]int, n)

    left, right := 0, n-1

    // Fill from the back: the next-largest square goes at index i.

}
func sortedSquares(nums []int) []int {
    // Micropattern: negatives make the largest squares live at the ends.
    n := len(nums)
    res := make([]int, n)

    left, right := 0, n-1

    // Fill from the back: the next-largest square goes at index i.
    for i := n - 1; i >= 0; i-- {

    }
}
func sortedSquares(nums []int) []int {
    // Micropattern: negatives make the largest squares live at the ends.
    n := len(nums)
    res := make([]int, n)

    left, right := 0, n-1

    // Fill from the back: the next-largest square goes at index i.
    for i := n - 1; i >= 0; i-- {
        l, r := nums[left]*nums[left], nums[right]*nums[right]
    }
}
func sortedSquares(nums []int) []int {
    // Micropattern: negatives make the largest squares live at the ends.
    n := len(nums)
    res := make([]int, n)

    left, right := 0, n-1

    // Fill from the back: the next-largest square goes at index i.
    for i := n - 1; i >= 0; i-- {
        l, r := nums[left]*nums[left], nums[right]*nums[right]

        // Take whichever end has the larger square, then step it inward.
    }
}
func sortedSquares(nums []int) []int {
    // Micropattern: negatives make the largest squares live at the ends.
    n := len(nums)
    res := make([]int, n)

    left, right := 0, n-1

    // Fill from the back: the next-largest square goes at index i.
    for i := n - 1; i >= 0; i-- {
        l, r := nums[left]*nums[left], nums[right]*nums[right]

        // Take whichever end has the larger square, then step it inward.
        if l > r {
            res[i] = l
            left++
        } else {
            res[i] = r
            right--
        }
    }
}
func sortedSquares(nums []int) []int {
    // Micropattern: negatives make the largest squares live at the ends.
    n := len(nums)
    res := make([]int, n)

    left, right := 0, n-1

    // Fill from the back: the next-largest square goes at index i.
    for i := n - 1; i >= 0; i-- {
        l, r := nums[left]*nums[left], nums[right]*nums[right]

        // Take whichever end has the larger square, then step it inward.
        if l > r {
            res[i] = l
            left++
        } else {
            res[i] = r
            right--
        }
    }

    return res // O(n), no sort needed
}

Tap the left half to go back, the right half to go forward.

Arrow keys or space to navigate · Esc to exit.