Skip to content

Commit 2740e9b

Browse files
authored
Added task 16.
1 parent b989e54 commit 2740e9b

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g0001_0100.s0016_3sum_closest
2+
3+
// #Medium #Array #Sorting #Two_Pointers
4+
5+
import java.util.Arrays
6+
7+
class Solution {
8+
fun threeSumClosest(nums: IntArray?, target: Int): Int {
9+
if (nums == null || nums.size < 3) {
10+
return 0
11+
}
12+
if (nums.size == 3) {
13+
return nums[0] + nums[1] + nums[2]
14+
}
15+
Arrays.sort(nums)
16+
val n = nums.size
17+
var sum = nums[0] + nums[1] + nums[2]
18+
for (i in 0 until n - 2) {
19+
if (nums[i] + nums[n - 1] + nums[n - 2] < target) {
20+
sum = nums[i] + nums[n - 1] + nums[n - 2]
21+
continue
22+
}
23+
if (nums[i] + nums[i + 1] + nums[i + 2] > target) {
24+
val temp = nums[i] + nums[i + 1] + nums[i + 2]
25+
return lessGap(sum, temp, target)
26+
}
27+
var j = i + 1
28+
var k = n - 1
29+
while (j < k) {
30+
val temp = nums[i] + nums[j] + nums[k]
31+
if (temp == target) {
32+
return target
33+
}
34+
if (temp < target) {
35+
j++
36+
} else {
37+
k--
38+
}
39+
sum = lessGap(sum, temp, target)
40+
}
41+
}
42+
return sum
43+
}
44+
45+
private fun lessGap(sum: Int, temp: Int, target: Int): Int {
46+
return if (Math.abs(sum - target) < Math.abs(temp - target)) sum else temp
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
16\. 3Sum Closest
2+
3+
Medium
4+
5+
Given an integer array `nums` of length `n` and an integer `target`, find three integers in `nums` such that the sum is closest to `target`.
6+
7+
Return _the sum of the three integers_.
8+
9+
You may assume that each input would have exactly one solution.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [-1,2,1,-4], target = 1
14+
15+
**Output:** 2
16+
17+
**Explanation:** The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [0,0,0], target = 1
22+
23+
**Output:** 0
24+
25+
**Constraints:**
26+
27+
* `3 <= nums.length <= 1000`
28+
* `-1000 <= nums[i] <= 1000`
29+
* <code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0001_0100.s0016_3sum_closest
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun threeSumClosest() {
10+
assertThat(Solution().threeSumClosest(intArrayOf(-1, 2, 1, -4), 1), equalTo(2))
11+
}
12+
13+
@Test
14+
fun threeSumClosest2() {
15+
assertThat(Solution().threeSumClosest(intArrayOf(0, 0, 0), 1), equalTo(0))
16+
}
17+
18+
@Test
19+
fun threeSumClosest3() {
20+
assertThat(
21+
Solution().threeSumClosest(intArrayOf(1, 2, 4, 8, 16, 32, 64, 128), 82),
22+
equalTo(82)
23+
)
24+
}
25+
26+
@Test
27+
fun threeSumClosest4() {
28+
assertThat(
29+
Solution().threeSumClosest(intArrayOf(4, 0, 5, -5, 3, 3, 0, -4, -5), -2),
30+
equalTo(-2)
31+
)
32+
}
33+
}

0 commit comments

Comments
 (0)