We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 81cdecd commit 3ec3b9eCopy full SHA for 3ec3b9e
counting-bits/seungriyou.py
@@ -3,7 +3,7 @@
3
from typing import List
4
5
class Solution:
6
- def countBits(self, n: int) -> List[int]:
+ def countBits1(self, n: int) -> List[int]:
7
"""
8
[Complexity]
9
- TC: O(n)
@@ -43,3 +43,17 @@ def countBits(self, n: int) -> List[int]:
43
dp[i] = dp[i - offset] + 1
44
45
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