forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain1.cpp
83 lines (63 loc) · 2.12 KB
/
main1.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/// Source : https://leetcode.com/problems/3sum-smaller/
/// Author : liuyubobobo
/// Time : 2016-12-05
#include <iostream>
#include <vector>
#include <cassert>
#include <stdexcept>
using namespace std;
/// Binary Search
/// Sort the entire numbers first.
/// For every number nums[i] and nums[j] in numbers,
/// use binary search to find index k,
/// which makes nums[i] + nums[j] + nums[k] is the closest sum less than the target.
///
/// Time Complexity: O(nlogn) + O((n^2)logn)
/// Space Complexity: O(1)
class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target) {
// There're testcases which the nums.size < 3
//assert( nums.size() >= 3 );
if( nums.size() < 3 )
return 0;
sort(nums.begin(), nums.end());
int res = 0;
for( int i = 0 ; i < nums.size() - 2 ; i ++ ){
for( int j = i + 1 ; j < nums.size() - 1 ; j ++ ){
int t = target - nums[i] - nums[j];
// find the largest index in nums[j+1...nums.size()-1]
// where nums[index] < t
int index = bsearch(nums, j+1, nums.size() - 1, t);
if(index != -1)
res += (index - j);
}
}
return res;
}
private:
// find the largest index j in the range [l...r] where nums[j] < target
// return -1 if there's no element less than the given target
int bsearch(const vector<int> &nums, int l, int r, int target){
assert(l >= 0 && r < nums.size() && l <= r);
// the left point is l-1 to give the space for non solution.
int left = l-1, right = r;
while(left != right){
// Using round-up tecnique to avoid inifinite loop
int mid = left + (right - left + 1) / 2;
if(nums[mid] >= target)
right = mid - 1;
else
left = mid;
}
if(left <= l - 1)
return -1;
return left;
}
};
int main() {
vector<int> nums1 = {-2, 0, 1, 3};
int target1 = 4;
cout << Solution().threeSumSmaller(nums1, target1) << endl;
return 0;
}