-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
maximum-score-from-grid-operations.py
62 lines (58 loc) · 2.78 KB
/
maximum-score-from-grid-operations.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
# Time: O(n^3)
# Space: O(n)
# prefix sum, dp
class Solution(object):
def maximumScore(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
prefix = [0]*(len(grid)+1)
for i in xrange(len(grid)):
prefix[i+1] = prefix[i]+grid[i][0]
result = 0
# dp[0][i]: the maximum score from 0 to the current column, and the current column has i black cells, without scoring the white cells of the current column
# dp[1][i]: the maximum score from 0 to the current column, and the current column has i black cells, with scoring the white cells of the current column
dp = [[0]*(len(grid)+1) for _ in xrange(2)]
for j in xrange(1, len(grid[0])):
new_prefix = [0]*(len(grid)+1)
for i in xrange(len(grid)):
new_prefix[i+1] = new_prefix[i]+grid[i][j]
new_dp = [[0]*(len(grid)+1) for _ in xrange(2)]
for i in xrange(len(grid)+1):
for k in xrange(i+1):
new_dp[0][i] = max(new_dp[0][i], (prefix[i]-prefix[k])+dp[0][k])
new_dp[0][i] = max(new_dp[0][i], max(dp[1]))
for k in xrange(i+1, len(grid)+1):
new_dp[1][i] = max(new_dp[1][i], dp[1][k]+(new_prefix[k]-new_prefix[i]))
new_dp[1][i] = max(new_dp[1][i], new_dp[0][i])
dp, prefix = new_dp, new_prefix
return max(dp[1])
# Time: O(n^3)
# Space: O(n)
# prefix sum, dp
class Solution2(object):
def maximumScore(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
prefix = [0]*(len(grid)+1)
for i in xrange(len(grid)):
prefix[i+1] = prefix[i]+grid[i][0]
# dp[0][i]: the maximum score from 0 to the current column, and the current column has i black cells, without scoring the white cells of the current column
# dp[1][i]: the maximum score from 0 to the current column, and the current column has i black cells, with scoring the white cells of the current column
dp = [[0]*(len(grid)+1) for _ in xrange(2)]
for j in xrange(1, len(grid[0])):
new_prefix = [0]*(len(grid)+1)
for i in xrange(len(grid)):
new_prefix[i+1] = new_prefix[i]+grid[i][j]
new_dp = [[0]*(len(grid)+1) for _ in xrange(2)]
for i in xrange(len(grid)+1):
for k in xrange(len(grid)+1):
new_dp[0][i] = max(new_dp[0][i], max(prefix[i]-prefix[k], 0)+dp[0][k])
new_dp[1][i] = max(new_dp[1][i], dp[1][k]+max(new_prefix[k]-new_prefix[i], 0))
new_dp[0][i] = max(new_dp[0][i], max(dp[1]))
new_dp[1][i] = max(new_dp[1][i], new_dp[0][i])
dp, prefix = new_dp, new_prefix
return max(dp[1])