Skip to content

Commit ffcb806

Browse files
authored
Create number-of-ways-to-select-buildings.cpp
1 parent 8c2694c commit ffcb806

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(k * n) = O(n)
2+
// Space: O(k) = O(1)
3+
4+
// dp
5+
class Solution {
6+
public:
7+
long long numberOfWays(string s) {
8+
static const int K = 3;
9+
vector<vector<int64_t>> dp(K, vector<int64_t>(2)); // dp[i][j]: number of ways of selecting i+1 buildings ending with type j
10+
for (const auto& c : s) {
11+
int j = c - '0';
12+
++dp[0][j];
13+
for (int i = 1; i < size(dp); ++i) {
14+
dp[i][j] += dp[i - 1][1 ^ j];
15+
}
16+
}
17+
return dp.back()[0] + dp.back()[1];
18+
}
19+
};

0 commit comments

Comments
 (0)