-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathThreeSumSmaller.java
40 lines (36 loc) · 1.1 KB
/
ThreeSumSmaller.java
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
package algorithm.pointers.twosum;
import java.util.Arrays;
/**
* Given an array of n integers nums and an integer target.
* Find the number of triplets whose sum is smaller than target.
*/
public class ThreeSumSmaller {
// Sort + TwoPointers
// Time O(n^2 + nlogn) = O(n^2), Space O(n) for sort
public int threeSumSmaller(int[] array, int target) {
if (array == null || array.length < 3) {
return 0;
}
Arrays.sort(array);
int count = 0;
for (int i = 0; i < array.length; i++) {
count += countTwoSumSmaller(array, i, target - array[i]);
}
return count;
}
private int countTwoSumSmaller(int[] array, int start, int target) {
// count number of pairs that sum to target
int count = 0;
int left = start + 1;
int right = array.length - 1;
while (left < right) {
int sum = array[left] + array[right];
if (sum < target) {
count += right - left;
} else {
right--;
}
}
return count;
}
}