-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL1402.py
More file actions
29 lines (22 loc) · 710 Bytes
/
L1402.py
File metadata and controls
29 lines (22 loc) · 710 Bytes
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
# 1402. 做菜顺序
from typing import List
class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
n = len(satisfaction)
satisfaction.sort()
sums = [-1] * n
sums[n - 1] = satisfaction[n - 1]
index = -1
for i in range(n - 2, -1, -1):
sums[i] = satisfaction[i]
sums[i] += sums[i + 1]
print(sums)
dp = [0] * (n)
dp[n - 1] = satisfaction[n - 1]
for i in range(n - 2, -1, -1):
ans = dp[i + 1] + sums[i]
dp[i] = max(ans, dp[i + 1])
return max(0, dp[0])
if __name__ == '__main__':
s = Solution()
print(s.maxSatisfaction([-1, -8, 0, 5, -9]))