Skip to content

Commit bac46f1

Browse files
authored
Create minimum-white-tiles-after-covering-with-carpets.py
1 parent fd3d3d2 commit bac46f1

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(m * n)
2+
# Space: O(m * n)
3+
4+
# dp
5+
class Solution(object):
6+
def minimumWhiteTiles(self, floor, numCarpets, carpetLen):
7+
"""
8+
:type floor: str
9+
:type numCarpets: int
10+
:type carpetLen: int
11+
:rtype: int
12+
"""
13+
dp = [[0]*(numCarpets+1) for _ in xrange(len(floor)+1)] # dp[i][j] : min number of white tiles in the first i floors with j carpets
14+
for i in xrange(1, len(dp)):
15+
dp[i][0] = dp[i-1][0] + int(floor[i-1])
16+
for j in xrange(1, numCarpets+1):
17+
dp[i][j] = min(dp[i-1][j] + int(floor[i-1]), dp[max(i-carpetLen, 0)][j-1])
18+
return dp[-1][-1]

0 commit comments

Comments
 (0)