forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-initial-energy-to-finish-tasks.cpp
36 lines (34 loc) · 1.27 KB
/
minimum-initial-energy-to-finish-tasks.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
// Time: O(nlogn)
// Space: O(1)
class Solution {
public:
int minimumEffort(vector<vector<int>>& tasks) {
sort(begin(tasks), end(tasks),
[](const auto& a, const auto& b) {
return a[1] - a[0] < b[1] - b[0]; // sort by waste in asc
});
int result = 0;
// you can see proof here, https://leetcode.com/problems/minimum-initial-energy-to-finish-tasks/discuss/944633/Explanation-on-why-sort-by-difference
for (const auto& task : tasks) { // we need to pick all the wastes, so greedily to pick the least waste first is always better
result = max(result + task[0], task[1]);
}
return result;
}
};
// Time: O(nlogn)
// Space: O(1)
class Solution2 {
public:
int minimumEffort(vector<vector<int>>& tasks) {
sort(begin(tasks), end(tasks),
[](const auto& a, const auto& b) {
return a[1] - a[0] > b[1] - b[0]; // sort by save in desc
});
int result = 0, curr = 0;
for (const auto& task : tasks) { // we need to pick all the saves, so greedily to pick the most save first is always better
result += max(task[1] - curr, 0);
curr = max(curr, task[1]) - task[0];
}
return result;
}
};