-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path139.h
More file actions
34 lines (32 loc) · 1.13 KB
/
139.h
File metadata and controls
34 lines (32 loc) · 1.13 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
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) {
// write your code here
vector<pair<int,int>> diff(1, pair<int,int>(nums[0],0));
for(int i = 1; i < nums.size(); i++){
diff.push_back(pair<int,int>(diff[i-1].first+nums[i],i));
}
sort(diff.begin(), diff.end(), compare);
int m = INT_MAX;
vector<int> ret(2,0);
for(int i = 1; i < diff.size(); i++){
int num1 = diff[i].first;
int num2 = diff[i-1].first;
int index_end1 = diff[i].second;
int index_end2 = diff[i-1].second;
if(abs(num1-num2) < m){
m = abs(num1-num2);
ret[0] = min(index_end1, index_end2) + 1;
ret[1] = max(index_end1, index_end2);
}
}
return ret;
}
static bool compare(const pair<int,int> &l, const pair<int,int> &r){
return l.first < r.first;
}
};