Skip to content

Commit c393c93

Browse files
authored
Improved task 18
1 parent ae5a3fe commit c393c93

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed
Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g0001_0100.s0018_4sum;
22

3-
// #Medium #Array #Sorting #Two_Pointers #2022_10_07_Time_6_ms_(96.23%)_Space_44.3_MB_(76.54%)
3+
// #Medium #Array #Sorting #Two_Pointers #2023_03_15_Time_3_ms_(99.47%)_Space_43.2_MB_(35.71%)
44

55
import java.util.ArrayList;
66
import java.util.Arrays;
@@ -9,55 +9,57 @@
99
@SuppressWarnings("java:S135")
1010
public class Solution {
1111
public List<List<Integer>> fourSum(int[] nums, int target) {
12-
List<List<Integer>> ret = new ArrayList<>();
13-
if (nums.length < 4) {
14-
return ret;
15-
}
16-
if (nums[0] == 1000000000 && nums[1] == 1000000000) {
17-
return ret;
18-
}
12+
int n = nums.length;
1913
Arrays.sort(nums);
20-
for (int i = 0; i < nums.length - 3; i++) {
21-
if (i != 0 && nums[i] == nums[i - 1]) {
14+
List<List<Integer>> result = new ArrayList<>();
15+
for (int i = 0; i < n - 3; i++) {
16+
if (i > 0 && nums[i] == nums[i - 1]) {
17+
continue;
18+
}
19+
if ((long) nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) {
20+
break;
21+
}
22+
if ((long) nums[i] + nums[n - 3] + nums[n - 2] + nums[n - 1] < target) {
2223
continue;
2324
}
24-
for (int j = i + 1; j < nums.length - 2; j++) {
25-
if (j != i + 1 && nums[j] == nums[j - 1]) {
25+
for (int j = i + 1; j < n - 2; j++) {
26+
if (j > i + 1 && nums[j] == nums[j - 1]) {
2627
continue;
2728
}
28-
int left = j + 1;
29-
int right = nums.length - 1;
30-
int half = nums[i] + nums[j];
31-
if (half + nums[left] + nums[left + 1] > target) {
32-
continue;
29+
if ((long) nums[j] + nums[j + 1] + nums[j + 2] > target - nums[i]) {
30+
break;
3331
}
34-
if (half + nums[right] + nums[right - 1] < target) {
32+
if ((long) nums[j] + nums[n - 2] + nums[n - 1] < target - nums[i]) {
3533
continue;
3634
}
37-
while (left < right) {
38-
int sum = nums[left] + nums[right] + half;
39-
if (sum == target) {
40-
ret.add(Arrays.asList(nums[left++], nums[right--], nums[i], nums[j]));
41-
while (nums[left] == nums[left - 1] && left < right) {
42-
left++;
35+
int tempTarget = target - (nums[i] + nums[j]);
36+
int low = j + 1;
37+
int high = n - 1;
38+
while (low < high) {
39+
int curSum = nums[low] + nums[high];
40+
if (curSum == tempTarget) {
41+
List<Integer> tempList = new ArrayList<>();
42+
tempList.add(nums[i]);
43+
tempList.add(nums[j]);
44+
tempList.add(nums[low]);
45+
tempList.add(nums[high]);
46+
result.add(tempList);
47+
low++;
48+
high--;
49+
while (low < high && nums[low] == nums[low - 1]) {
50+
low++;
4351
}
44-
while (nums[right] == nums[right + 1] && left < right) {
45-
right--;
46-
}
47-
} else if (sum < target) {
48-
left++;
49-
while (nums[left] == nums[left - 1] && left < right) {
50-
left++;
52+
while (low < high && nums[high] == nums[high + 1]) {
53+
high--;
5154
}
55+
} else if (curSum < tempTarget) {
56+
low++;
5257
} else {
53-
right--;
54-
while (nums[right] == nums[right + 1] && left < right) {
55-
right--;
56-
}
58+
high--;
5759
}
5860
}
5961
}
6062
}
61-
return ret;
63+
return result;
6264
}
6365
}

src/test/java/g0001_0100/s0018_4sum/SolutionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void fourSum() {
1414
new Solution().fourSum(new int[] {1, 0, -1, 0, -2, 2}, 0),
1515
equalTo(
1616
ArrayUtils.getLists(
17-
new int[][] {{1, 2, -2, -1}, {0, 2, -2, 0}, {0, 1, -1, 0}})));
17+
new int[][] {{-2, -1, 1, 2}, {-2, 0, 0, 2}, {-1, 0, 0, 1}})));
1818
}
1919

2020
@Test

0 commit comments

Comments
 (0)