Skip to content

Commit fd3d3d2

Browse files
authored
Create minimum-white-tiles-after-covering-with-carpets.cpp
1 parent 7c52d97 commit fd3d3d2

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 {
6+
public:
7+
int minimumWhiteTiles(string floor, int numCarpets, int carpetLen) {
8+
vector<vector<int>> dp(size(floor) + 1, vector<int>(numCarpets + 1)); // dp[i][j] : min number of white tiles in the first i floors with j carpets
9+
for (int i = 1; i < size(dp); ++i) {
10+
dp[i][0] = dp[i - 1][0] + static_cast<int>(floor[i - 1] == '1');
11+
for (int j = 1; j <= numCarpets; ++j) {
12+
dp[i][j] = min(dp[i - 1][j] + static_cast<int>(floor[i - 1] == '1'),
13+
dp[max(i - carpetLen, 0)][j - 1]);
14+
}
15+
}
16+
return dp.back().back();
17+
}
18+
};

0 commit comments

Comments
 (0)