-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmaximum-amount-of-money-robot-can-earn.py
62 lines (59 loc) · 2.4 KB
/
maximum-amount-of-money-robot-can-earn.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Time: O(m * n * k) = O(m * n)
# Space: O(min(m, n) * k) = O(min(m, n))
# dp
class Solution(object):
def maximumAmount(self, coins):
"""
:type coins: List[List[int]]
:rtype: int
"""
K = 2
mn = min(len(coins), len(coins[0]))
mx = max(len(coins), len(coins[0]))
get = (lambda i, j: coins[i][j]) if len(coins) == mx else (lambda i, j: coins[j][i])
dp = [[float("-inf")]*(K+1) for _ in xrange(mn)]
for i in xrange(mx):
new_dp = [[float("-inf")]*(K+1) for _ in xrange(mn)]
for j in xrange(mn):
for k in xrange(K+1):
if i == 0 and j == 0:
new_dp[j][k] = max(get(i, j), 0) if k-1 >= 0 else get(i, j)
continue
if i-1 >= 0:
new_dp[j][k] = max(new_dp[j][k], dp[j][k]+get(i, j))
if k-1 >= 0:
new_dp[j][k] = max(new_dp[j][k], dp[j][k-1])
if j-1 >= 0:
new_dp[j][k] = max(new_dp[j][k], new_dp[j-1][k]+get(i, j))
if k-1 >= 0:
new_dp[j][k] = max(new_dp[j][k], new_dp[j-1][k-1])
dp = new_dp
return dp[-1][-1]
# Time: O(m * n * k) = O(m * n)
# Space: O(n * k) = O(n)
# dp
class Solution2(object):
def maximumAmount(self, coins):
"""
:type coins: List[List[int]]
:rtype: int
"""
K = 2
dp = [[float("-inf")]*(K+1) for _ in xrange(len(coins[0]))]
for i in xrange(len(coins)):
new_dp = [[float("-inf")]*(K+1) for _ in xrange(len(coins[0]))]
for j in xrange(len(coins[0])):
for k in xrange((K+1)):
if i == 0 and j == 0:
new_dp[j][k] = max(coins[i][j], 0) if k-1 >= 0 else coins[i][j]
continue
if i-1 >= 0:
new_dp[j][k] = max(new_dp[j][k], dp[j][k]+coins[i][j])
if k-1 >= 0:
new_dp[j][k] = max(new_dp[j][k], dp[j][k-1])
if j-1 >= 0:
new_dp[j][k] = max(new_dp[j][k], new_dp[j-1][k]+coins[i][j])
if k-1 >= 0:
new_dp[j][k] = max(new_dp[j][k], new_dp[j-1][k-1])
dp = new_dp
return dp[-1][-1]