← 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 {
    // Squaring flips sign, so the largest squares come from the most negative OR
    // the most positive values — that is, the two ends, never the middle.
}
func sortedSquares(nums []int) []int {
    // Squaring flips sign, so the largest squares come from the most negative OR
    // the most positive values — that is, the two ends, never the middle.
    n := len(nums)
    res := make([]int, n)
}
func sortedSquares(nums []int) []int {
    // Squaring flips sign, so the largest squares come from the most negative OR
    // the most positive values — that is, the two ends, never the middle.
    n := len(nums)
    res := make([]int, n)

    // One pointer at each end. Whichever end squares larger is the next biggest.
}
func sortedSquares(nums []int) []int {
    // Squaring flips sign, so the largest squares come from the most negative OR
    // the most positive values — that is, the two ends, never the middle.
    n := len(nums)
    res := make([]int, n)

    // One pointer at each end. Whichever end squares larger is the next biggest.
    left, right := 0, n-1
}
func sortedSquares(nums []int) []int {
    // Squaring flips sign, so the largest squares come from the most negative OR
    // the most positive values — that is, the two ends, never the middle.
    n := len(nums)
    res := make([]int, n)

    // One pointer at each end. Whichever end squares larger is the next biggest.
    left, right := 0, n-1

    // We discover squares largest-first, so fill the result from the back.
}
func sortedSquares(nums []int) []int {
    // Squaring flips sign, so the largest squares come from the most negative OR
    // the most positive values — that is, the two ends, never the middle.
    n := len(nums)
    res := make([]int, n)

    // One pointer at each end. Whichever end squares larger is the next biggest.
    left, right := 0, n-1

    // We discover squares largest-first, so fill the result from the back.
    for i := n - 1; i >= 0; i-- {
        l, r := nums[left]*nums[left], nums[right]*nums[right]
    }
}
func sortedSquares(nums []int) []int {
    // Squaring flips sign, so the largest squares come from the most negative OR
    // the most positive values — that is, the two ends, never the middle.
    n := len(nums)
    res := make([]int, n)

    // One pointer at each end. Whichever end squares larger is the next biggest.
    left, right := 0, n-1

    // We discover squares largest-first, so fill the result from the back.
    for i := n - 1; i >= 0; i-- {
        l, r := nums[left]*nums[left], nums[right]*nums[right]

        // Drop the larger square into slot i, then step that pointer inward.
    }
}
func sortedSquares(nums []int) []int {
    // Squaring flips sign, so the largest squares come from the most negative OR
    // the most positive values — that is, the two ends, never the middle.
    n := len(nums)
    res := make([]int, n)

    // One pointer at each end. Whichever end squares larger is the next biggest.
    left, right := 0, n-1

    // We discover squares largest-first, so fill the result from the back.
    for i := n - 1; i >= 0; i-- {
        l, r := nums[left]*nums[left], nums[right]*nums[right]

        // Drop the larger square into slot i, then step that pointer inward.
        if l > r {
            res[i] = l
            left++
        } else {
            res[i] = r
            right--
        }
    }
}
func sortedSquares(nums []int) []int {
    // Squaring flips sign, so the largest squares come from the most negative OR
    // the most positive values — that is, the two ends, never the middle.
    n := len(nums)
    res := make([]int, n)

    // One pointer at each end. Whichever end squares larger is the next biggest.
    left, right := 0, n-1

    // We discover squares largest-first, so fill the result from the back.
    for i := n - 1; i >= 0; i-- {
        l, r := nums[left]*nums[left], nums[right]*nums[right]

        // Drop the larger square into slot i, then step that pointer inward.
        if l > r {
            res[i] = l
            left++
        } else {
            res[i] = r
            right--
        }
    }

    return res // O(n) — no sorting needed, the input is already sorted
}

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

Arrow keys or space to navigate · Esc to exit.