-
Notifications
You must be signed in to change notification settings - Fork 2
/
ThreeSumSmaller.java
35 lines (32 loc) · 991 Bytes
/
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
package src.java.medium.array;
import java.util.Arrays;
public class ThreeSumSmaller {
/**
* @param nums: an array of n integers
* @param target: a target
* @return: the number of index triplets satisfy the condition nums[i] + nums[j] + nums[k] < target
*/
public int threeSumSmaller(int[] nums, int target) {
// Write your code here
if (nums==null || nums.length==0) return 0;
Arrays.sort(nums);
int ret = 0;
for (int i=0; i<nums.length-2; i++) {
ret += helper(nums, i+1, nums.length-1, target-nums[i]);
}
return ret;
}
private int helper(int[] nums, int start, int end, int target) {
int ret = 0;
while(start < end) {
int sum = nums[start] + nums[end];
if (sum < target) {
ret += end - start;
start += 1;
} else {
end -= 1;
}
}
return ret;
}
}