← 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 {
    // Same skeleton as 3Sum — sort, fix an anchor, converge — but there's no
    // "sum == 0" goal. Instead we remember the sum that lands closest to target.
    // Seed with any real triple so there's something to compare against.
}
func threeSumClosest(nums []int, target int) int {
    // Same skeleton as 3Sum — sort, fix an anchor, converge — but there's no
    // "sum == 0" goal. Instead we remember the sum that lands closest to target.
    sort.Ints(nums)
    // Seed with any real triple so there's something to compare against.
    closest := nums[0] + nums[1] + nums[2]
}
func threeSumClosest(nums []int, target int) int {
    // Same skeleton as 3Sum — sort, fix an anchor, converge — but there's no
    // "sum == 0" goal. Instead we remember the sum that lands closest to target.
    sort.Ints(nums)
    // Seed with any real triple so there's something to compare against.
    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 {
    // Same skeleton as 3Sum — sort, fix an anchor, converge — but there's no
    // "sum == 0" goal. Instead we remember the sum that lands closest to target.
    sort.Ints(nums)
    // Seed with any real triple so there's something to compare against.
    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]

            // Distance to target = |sum - target|. Keep the closest we've seen.
        }
    }
}
func threeSumClosest(nums []int, target int) int {
    // Same skeleton as 3Sum — sort, fix an anchor, converge — but there's no
    // "sum == 0" goal. Instead we remember the sum that lands closest to target.
    sort.Ints(nums)
    // Seed with any real triple so there's something to compare against.
    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]

            // Distance to target = |sum - target|. Keep the closest we've seen.
            if abs(sum-target) < abs(closest-target) {
                closest = sum
            }
        }
    }
}
func threeSumClosest(nums []int, target int) int {
    // Same skeleton as 3Sum — sort, fix an anchor, converge — but there's no
    // "sum == 0" goal. Instead we remember the sum that lands closest to target.
    sort.Ints(nums)
    // Seed with any real triple so there's something to compare against.
    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]

            // Distance to target = |sum - target|. Keep the closest we've seen.
            if abs(sum-target) < abs(closest-target) {
                closest = sum
            }

            // Dead-on: you can't get closer than zero distance, so return now.
        }
    }
}
func threeSumClosest(nums []int, target int) int {
    // Same skeleton as 3Sum — sort, fix an anchor, converge — but there's no
    // "sum == 0" goal. Instead we remember the sum that lands closest to target.
    sort.Ints(nums)
    // Seed with any real triple so there's something to compare against.
    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]

            // Distance to target = |sum - target|. Keep the closest we've seen.
            if abs(sum-target) < abs(closest-target) {
                closest = sum
            }

            switch {
            // Dead-on: you can't get closer than zero distance, so return now.
            case sum == target:
                return sum
            }
        }
    }
}
func threeSumClosest(nums []int, target int) int {
    // Same skeleton as 3Sum — sort, fix an anchor, converge — but there's no
    // "sum == 0" goal. Instead we remember the sum that lands closest to target.
    sort.Ints(nums)
    // Seed with any real triple so there's something to compare against.
    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]

            // Distance to target = |sum - target|. Keep the closest we've seen.
            if abs(sum-target) < abs(closest-target) {
                closest = sum
            }

            switch {
            // Dead-on: you can't get closer than zero distance, so return now.
            case sum == target:
                return sum

            // Below target: move left up to increase the sum toward target.

            // Above target: move right down to decrease it toward target.
            }
        }
    }
}
func threeSumClosest(nums []int, target int) int {
    // Same skeleton as 3Sum — sort, fix an anchor, converge — but there's no
    // "sum == 0" goal. Instead we remember the sum that lands closest to target.
    sort.Ints(nums)
    // Seed with any real triple so there's something to compare against.
    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]

            // Distance to target = |sum - target|. Keep the closest we've seen.
            if abs(sum-target) < abs(closest-target) {
                closest = sum
            }

            switch {
            // Dead-on: you can't get closer than zero distance, so return now.
            case sum == target:
                return sum

            // Below target: move left up to increase the sum toward target.
            case sum < target:
                left++

            // Above target: move right down to decrease it toward target.
            default:
                right--
            }
        }
    }
}
func threeSumClosest(nums []int, target int) int {
    // Same skeleton as 3Sum — sort, fix an anchor, converge — but there's no
    // "sum == 0" goal. Instead we remember the sum that lands closest to target.
    sort.Ints(nums)
    // Seed with any real triple so there's something to compare against.
    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]

            // Distance to target = |sum - target|. Keep the closest we've seen.
            if abs(sum-target) < abs(closest-target) {
                closest = sum
            }

            switch {
            // Dead-on: you can't get closer than zero distance, so return now.
            case sum == target:
                return sum

            // Below target: move left up to increase the sum toward target.
            case sum < target:
                left++

            // Above target: move right down to decrease it toward target.
            default:
                right--
            }
        }
    }

}

// abs returns the absolute value of x.
func threeSumClosest(nums []int, target int) int {
    // Same skeleton as 3Sum — sort, fix an anchor, converge — but there's no
    // "sum == 0" goal. Instead we remember the sum that lands closest to target.
    sort.Ints(nums)
    // Seed with any real triple so there's something to compare against.
    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]

            // Distance to target = |sum - target|. Keep the closest we've seen.
            if abs(sum-target) < abs(closest-target) {
                closest = sum
            }

            switch {
            // Dead-on: you can't get closer than zero distance, so return now.
            case sum == target:
                return sum

            // Below target: move left up to increase the sum toward target.
            case sum < target:
                left++

            // Above target: move right down to decrease it toward target.
            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.