-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path3_sum_closest.go
45 lines (39 loc) · 928 Bytes
/
3_sum_closest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import "sort"
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func threeSumClosest(nums []int, target int) int {
l := len(nums)
sort.Slice(nums, func(i, j int) bool { return nums[i] < nums[j] })
// take arbitrary sum as closest
closest := nums[0] + nums[1] + nums[2]
for i := 0; i < l-2; i++ {
// ideal sum for (2nd+3rd)
c := target - nums[i]
// start looking at ends
l, h := i+1, l-1
for l < h {
v := nums[l] + nums[h]
w := nums[i] + v
// for each three numbers check if their sum is smaller than current closest number
if abs(target-w) < abs(target-closest) {
closest = w
}
if v > c {
// if sum of (2nd+3rd) is larger that ideal, try to decrease it
h--
} else if v < c {
// if sum of (2nd+3rd) is smaller than ideal, try to increase it
l++
} else {
// we found exact match, bail out
return target
}
}
}
return closest
}