← Back

3Sum Closest

Same scan, but track the sum nearest the target instead of an exact hit.

Medium LeetCode ↗ 1 / 1
func threeSumClosest(nums []int, target int) int {

}
func threeSumClosest(nums []int, target int) int {
    // Micropattern: sort, fix nums[i], converge — but remember the closest sum.
}
func threeSumClosest(nums []int, target int) int {
    // Micropattern: sort, fix nums[i], converge — but remember the closest sum.
    sort.Ints(nums)
    closest := nums[0] + nums[1] + nums[2]
}
func threeSumClosest(nums []int, target int) int {
    // Micropattern: sort, fix nums[i], converge — but remember the closest sum.
    sort.Ints(nums)
    closest := nums[0] + nums[1] + nums[2]

    for i := 0; i < len(nums)-2; i++ {
        left, right := i+1, len(nums)-1
        for left < right {
            sum := nums[i] + nums[left] + nums[right]
        }
    }
}
func threeSumClosest(nums []int, target int) int {
    // Micropattern: sort, fix nums[i], converge — but remember the closest sum.
    sort.Ints(nums)
    closest := nums[0] + nums[1] + nums[2]

    for i := 0; i < len(nums)-2; i++ {
        left, right := i+1, len(nums)-1
        for left < right {
            sum := nums[i] + nums[left] + nums[right]

            // Keep the sum with the smallest distance to target.
        }
    }
}
func threeSumClosest(nums []int, target int) int {
    // Micropattern: sort, fix nums[i], converge — but remember the closest sum.
    sort.Ints(nums)
    closest := nums[0] + nums[1] + nums[2]

    for i := 0; i < len(nums)-2; i++ {
        left, right := i+1, len(nums)-1
        for left < right {
            sum := nums[i] + nums[left] + nums[right]

            // Keep the sum with the smallest distance to target.
            if abs(sum-target) < abs(closest-target) {
                closest = sum
            }
        }
    }
}
func threeSumClosest(nums []int, target int) int {
    // Micropattern: sort, fix nums[i], converge — but remember the closest sum.
    sort.Ints(nums)
    closest := nums[0] + nums[1] + nums[2]

    for i := 0; i < len(nums)-2; i++ {
        left, right := i+1, len(nums)-1
        for left < right {
            sum := nums[i] + nums[left] + nums[right]

            // Keep the sum with the smallest distance to target.
            if abs(sum-target) < abs(closest-target) {
                closest = sum
            }

            switch {
            case sum == target:
                return sum // can't get closer than exact
            case sum < target:
                left++
            default:
                right--
            }
        }
    }
}
func threeSumClosest(nums []int, target int) int {
    // Micropattern: sort, fix nums[i], converge — but remember the closest sum.
    sort.Ints(nums)
    closest := nums[0] + nums[1] + nums[2]

    for i := 0; i < len(nums)-2; i++ {
        left, right := i+1, len(nums)-1
        for left < right {
            sum := nums[i] + nums[left] + nums[right]

            // Keep the sum with the smallest distance to target.
            if abs(sum-target) < abs(closest-target) {
                closest = sum
            }

            switch {
            case sum == target:
                return sum // can't get closer than exact
            case sum < target:
                left++
            default:
                right--
            }
        }
    }

}

// abs returns the absolute value of x.
func threeSumClosest(nums []int, target int) int {
    // Micropattern: sort, fix nums[i], converge — but remember the closest sum.
    sort.Ints(nums)
    closest := nums[0] + nums[1] + nums[2]

    for i := 0; i < len(nums)-2; i++ {
        left, right := i+1, len(nums)-1
        for left < right {
            sum := nums[i] + nums[left] + nums[right]

            // Keep the sum with the smallest distance to target.
            if abs(sum-target) < abs(closest-target) {
                closest = sum
            }

            switch {
            case sum == target:
                return sum // can't get closer than exact
            case sum < target:
                left++
            default:
                right--
            }
        }
    }

    return closest
}

// abs returns the absolute value of x.
func abs(x int) int {
    if x < 0 {
        return -x
    }
    return x
}

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

Arrow keys or space to navigate · Esc to exit.