-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSubarray Sum Closest.cpp
71 lines (65 loc) · 2.06 KB
/
Subarray Sum Closest.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
class Solution {
public:
/**
* @param nums: A list of integers
* @return: A list of integers includes the index of the first number
* and the index of the last number
*/
vector<int> subarraySumClosest(vector<int> nums){
// balanced binary tree to store value, index
map<int, int> bst;
bst[0] = -1;
// boundaries
bst[INT_MIN] = -2;
bst[INT_MAX] = nums.size();
vector<vector<int> > m_res;
int closestNumber = 0;
int gap = INT_MAX;
int sum = 0;
for (int i = 0; i < nums.size(); ++i) {
sum += nums[i];
// O(lgN) to find the fisrt >= sum - closestNumber
map<int, int>::iterator it = bst.lower_bound(sum - closestNumber);
int tmp_gap = abs(closestNumber - (sum - it->first));
if (tmp_gap == gap) {
// add one more possible result
vector<int> temp(2, -1);
temp[0] = it->second + 1;
temp[1] = i;
m_res.push_back(temp);
}
if (tmp_gap < gap && it->first != INT_MIN && it->first != INT_MAX) {
gap = tmp_gap;
// update result need to clear the previous result
m_res.clear();
vector<int> temp(2,-1);
temp[0] = it->second + 1;
temp[1] = i;
m_res.push_back(temp);
}
--it;
// O(lgN) to find the last value < sum - closestNumber
tmp_gap = abs(closestNumber - (sum - it->first));
if (tmp_gap == gap) {
vector<int> temp(2, -1);
temp[0] = it->second + 1;
temp[1] = i;
m_res.push_back(temp);
}
if (tmp_gap < gap && it->first != INT_MIN && it->first != INT_MAX) {
gap = tmp_gap;
// update result need to clear the previous result
m_res.clear();
vector<int> temp(2, -1);
temp[0] = it->second + 1;
temp[1] = i;
m_res.push_back(temp);
}
bst[sum] = i;
}
if (m_res.size() == 0)
m_res.push_back(vector<int> (2,0));
// we just need to return one possible combination.
return m_res[0];
}
};