forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum-initial-energy-to-finish-tasks.py
More file actions
31 lines (28 loc) · 1.05 KB
/
minimum-initial-energy-to-finish-tasks.py
File metadata and controls
31 lines (28 loc) · 1.05 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
# Time: O(nlogn)
# Space: O(1)
class Solution(object):
def minimumEffort(self, tasks):
"""
:type tasks: List[List[int]]
:rtype: int
"""
tasks.sort(key=lambda x: x[1]-x[0]) # sort by waste in asc
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 a, m in tasks: # we need to pick all the wastes, so greedily to pick the least waste first is always better
result = max(result+a, m)
return result
# Time: O(nlogn)
# Space: O(1)
class Solution2(object):
def minimumEffort(self, tasks):
"""
:type tasks: List[List[int]]
:rtype: int
"""
tasks.sort(key=lambda x: x[0]-x[1]) # sort by save in desc
result = curr = 0
for a, m in tasks: # we need to pick all the saves, so greedily to pick the most save first is always better
result += max(m-curr, 0)
curr = max(curr, m)-a
return result