-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2106.cpp
More file actions
38 lines (37 loc) · 1.17 KB
/
2106.cpp
File metadata and controls
38 lines (37 loc) · 1.17 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
class Solution {
public:
int maxTotalFruits(vector<vector<int>>& fruits, int startPos, int k) {
int accum = 0;
map<int, int> mp;
mp[-100000] = 0;
for (auto& fruit : fruits) {
accum += fruit[1];
mp[fruit[0]] = accum;
}
mp[300000] = accum;
int res = 0;
for (int left = startPos - k; left <= startPos; left += 1) {
int right = max(left + k - (startPos - left), startPos);
// [left, right]
auto it = mp.lower_bound(left);
it--;
int leftV = it->second;
it = mp.upper_bound(right);
it--;
int rightV = it->second;
res = max(res, rightV - leftV);
}
for (int right = startPos + k; right >= startPos; right -= 1) {
int left = min(right - (k - (right - startPos)), startPos);
// [left, right]
auto it = mp.lower_bound(left);
it--;
int leftV = it->second;
it = mp.upper_bound(right);
it--;
int rightV = it->second;
res = max(res, rightV - leftV);
}
return res;
}
};