Skip to content

Commit cde9b6a

Browse files
authored
Create number-of-ways-to-select-buildings.py
1 parent ffcb806 commit cde9b6a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(k * n) = O(n)
2+
# Space: O(k) = O(1)
3+
4+
# dp
5+
class Solution(object):
6+
def numberOfWays(self, s):
7+
"""
8+
:type s: str
9+
:rtype: int
10+
"""
11+
K = 3
12+
dp = [[0]*2 for _ in xrange(K)] # dp[i][j]: number of ways of selecting i+1 buildings ending with type j
13+
for c in s:
14+
j = ord(c)-ord('0')
15+
dp[0][j] += 1
16+
for i in xrange(1, len(dp)):
17+
dp[i][j] += dp[i-1][1^j]
18+
return dp[-1][0]+dp[-1][1]

0 commit comments

Comments
 (0)