|
1 | 1 | package g0001_0100.s0018_4sum |
2 | 2 |
|
3 | | -// #Medium #Array #Sorting #Two_Pointers #2022_10_06_Time_244_ms_(100.00%)_Space_38.8_MB_(100.00%) |
| 3 | +// #Medium #Array #Sorting #Two_Pointers #2023_02_27_Time_221_ms_(100.00%)_Space_36.8_MB_(100.00%) |
4 | 4 |
|
5 | 5 | class Solution { |
6 | 6 | fun fourSum(nums: IntArray, target: Int): List<List<Int>> { |
7 | | - val ret: MutableList<List<Int>> = ArrayList() |
8 | | - if (nums.size < 4) { |
9 | | - return ret |
10 | | - } |
11 | | - if (nums[0] == 1000000000 && nums[1] == 1000000000) { |
12 | | - return ret |
13 | | - } |
| 7 | + val n = nums.size |
14 | 8 | nums.sort() |
15 | | - for (i in 0 until nums.size - 3) { |
16 | | - if (i != 0 && nums[i] == nums[i - 1]) { |
| 9 | + val result: MutableList<List<Int>> = ArrayList() |
| 10 | + for (i in 0 until n - 3) { |
| 11 | + if (i > 0 && nums[i] == nums[i - 1]) { |
| 12 | + continue |
| 13 | + } |
| 14 | + if (nums[i].toLong() + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) { |
| 15 | + break |
| 16 | + } |
| 17 | + if (nums[i].toLong() + nums[n - 3] + nums[n - 2] + nums[n - 1] < target) { |
17 | 18 | continue |
18 | 19 | } |
19 | | - for (j in i + 1 until nums.size - 2) { |
20 | | - if (j != i + 1 && nums[j] == nums[j - 1]) { |
| 20 | + for (j in i + 1 until n - 2) { |
| 21 | + if (j > i + 1 && nums[j] == nums[j - 1]) { |
21 | 22 | continue |
22 | 23 | } |
23 | | - var left = j + 1 |
24 | | - var right = nums.size - 1 |
25 | | - val half = nums[i] + nums[j] |
26 | | - if (half + nums[left] + nums[left + 1] > target) { |
27 | | - continue |
| 24 | + if (nums[j].toLong() + nums[j + 1] + nums[j + 2] > target - nums[i]) { |
| 25 | + break |
28 | 26 | } |
29 | | - if (half + nums[right] + nums[right - 1] < target) { |
| 27 | + if (nums[j].toLong() + nums[n - 2] + nums[n - 1] < target - nums[i]) { |
30 | 28 | continue |
31 | 29 | } |
32 | | - while (left < right) { |
33 | | - val sum = nums[left] + nums[right] + half |
34 | | - if (sum == target) { |
35 | | - ret.add(listOf(nums[left++], nums[right--], nums[i], nums[j])) |
36 | | - while (nums[left] == nums[left - 1] && left < right) { |
37 | | - left++ |
| 30 | + val tempTarget = target - (nums[i] + nums[j]) |
| 31 | + var low = j + 1 |
| 32 | + var high = n - 1 |
| 33 | + while (low < high) { |
| 34 | + val curSum = nums[low] + nums[high] |
| 35 | + if (curSum == tempTarget) { |
| 36 | + val tempList: MutableList<Int> = ArrayList() |
| 37 | + tempList.add(nums[i]) |
| 38 | + tempList.add(nums[j]) |
| 39 | + tempList.add(nums[low]) |
| 40 | + tempList.add(nums[high]) |
| 41 | + result.add(tempList) |
| 42 | + low++ |
| 43 | + high-- |
| 44 | + while (low < high && nums[low] == nums[low - 1]) { |
| 45 | + low++ |
38 | 46 | } |
39 | | - while (nums[right] == nums[right + 1] && left < right) { |
40 | | - right-- |
41 | | - } |
42 | | - } else if (sum < target) { |
43 | | - left++ |
44 | | - while (nums[left] == nums[left - 1] && left < right) { |
45 | | - left++ |
| 47 | + while (low < high && nums[high] == nums[high + 1]) { |
| 48 | + high-- |
46 | 49 | } |
| 50 | + } else if (curSum < tempTarget) { |
| 51 | + low++ |
47 | 52 | } else { |
48 | | - right-- |
49 | | - while (nums[right] == nums[right + 1] && left < right) { |
50 | | - right-- |
51 | | - } |
| 53 | + high-- |
52 | 54 | } |
53 | 55 | } |
54 | 56 | } |
55 | 57 | } |
56 | | - return ret |
| 58 | + return result |
57 | 59 | } |
58 | 60 | } |
0 commit comments