Skip to content

Commit db44503

Browse files
committed
Sync LeetCode submission Runtime - 68 ms (44.32%), Memory - 23.1 MB (65.19%)
1 parent 8ca5afc commit db44503

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

0338-counting-bits/solution.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
# Approach 4 - DP + Last Set Bit
1+
# Approach 3: DP + Least Significant Bit
22

3-
# Time: O(N)
3+
# Time: O(n)
4+
# Space: O(1)
45

56
class Solution:
67
def countBits(self, n: int) -> List[int]:
78
ans = [0] * (n + 1)
8-
99
for x in range(1, n + 1):
10-
ans[x] = ans[x & (x - 1)] + 1
11-
12-
return ans
13-
10+
ans[x] = ans[x >> 1] + (x & 1)
11+
12+
return ans

0 commit comments

Comments
 (0)