Skip to content

Commit 3ec3b9e

Browse files
committed
solve(w14): 338. Counting Bits (update)
1 parent 81cdecd commit 3ec3b9e

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

counting-bits/seungriyou.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import List
44

55
class Solution:
6-
def countBits(self, n: int) -> List[int]:
6+
def countBits1(self, n: int) -> List[int]:
77
"""
88
[Complexity]
99
- TC: O(n)
@@ -43,3 +43,17 @@ def countBits(self, n: int) -> List[int]:
4343
dp[i] = dp[i - offset] + 1
4444

4545
return dp
46+
47+
def countBits(self, n: int) -> List[int]:
48+
"""
49+
[Complexity]
50+
- TC: O(n)
51+
- SC: O(n)
52+
"""
53+
54+
dp = [0] * (n + 1)
55+
for i in range(1, n + 1):
56+
# dp[i] = dp[i // 2] + (i % 2)
57+
dp[i] = dp[i >> 1] + (i & 1)
58+
59+
return dp

0 commit comments

Comments
 (0)