forked from NilaakashSingh/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreeSumClosest.swift
45 lines (38 loc) · 1.45 KB
/
ThreeSumClosest.swift
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
/**
* Question Link: https://leetcode.com/problems/3sum-closest/
* Primary idea: Sort the array, and iterate through it while updating the diff,
* increment left or decrease right predicated on
* their sum is greater or less than the target
* Time Complexity: O(n^2), Space Complexity: O(nC3)
*/
class ThreeSumClosest {
func threeSumClosest(_ nums: [Int], _ target: Int) -> Int {
var minDiff = Int.max
let nums = nums.sorted()
for i in 0..<nums.count - 2 {
if i == 0 || nums[i] != nums[i - 1] {
let twoSum = target - nums[i]
var left = i + 1
var right = nums.count - 1
while left < right {
let diff = nums[left] + nums[right] - twoSum
if abs(diff) < abs(minDiff) {
minDiff = diff
}
if diff == 0 {
return target
} else if diff > 0 {
repeat {
right -= 1
} while left < right && nums[right] == nums[right + 1]
} else {
repeat {
left += 1
} while left < right && nums[left] == nums[left - 1]
}
}
}
}
return target + minDiff
}
}