-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3SumSmaller.h
More file actions
44 lines (36 loc) · 1.09 KB
/
3SumSmaller.h
File metadata and controls
44 lines (36 loc) · 1.09 KB
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
41
42
43
44
/*
2015-08-18
bluepp
May the force be with me!
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.
For example, given nums = [-2, 0, 1, 3], and target = 2.
Return 2. Because there are two triplets which sums are less than 2:
[-2, 0, 1]
[-2, 0, 3]
Follow up:
Could you solve it in O(n2) runtime?
https://leetcode.com/problems/3sum-smaller/
*/
/* https://leetcode.com/discuss/52362/11-lines-o-n-2-python */
int threeSumSmaller(vector<int>& nums, int target) {
int n = nums.size();
if (n < 3) return 0;
sort(nums.begin(), nums.end());
int ret = 0;
for (int i = 0; i < n-2; i++)
{
int l = i+1, r = n-1;
while (l < r)
{
int t = nums[i] + nums[l] + nums[r];
if (t < target)
{
ret += r-l;
l++;
}
else r--;
}
}
return ret;
}