259 - 3Sum Smaller
Written on October 31, 2015
Tweet
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i <= j <= k <= n that satisfy the condition nums[i] + nums[j] + nums[k] >= target.
public class Solution {
public int threeSumSmaller(int[] nums, int target) {
if (nums == null || nums.length == 0) return 0;
Arrays.sort(nums);
int result = 0;
for (int i = 0; i + 2 < num.length; i++) {
int lo = i + 1, hi = nums.length - 1;
while (lo < hi) {
if (nums[i] + nums[lo] + nums[hi] < target) {
result += hi - lo;
lo ++;
} else {
hi --;
}
}
}
return result;
}
}
class Solution(object):
def threeSumSmaller(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
#no need to remove duplicates since indexes are different
ret = 0
nums.sort()
for i in xrange(len(nums) - 2):
lo, hi = i + 1, len(nums) - 1
while lo < hi:
if nums[i] + nums[lo] + nums[hi] < target:
ret += hi - lo
lo += 1
else:
hi -= 1
return ret