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 6519d16 commit c8399faCopy full SHA for c8399fa
counting-bits/yolophg.py
@@ -0,0 +1,18 @@
1
+# Time Complexity: O(n) -> iterate from 1 to n, updating arr[] in O(1) for each i.
2
+# Space Complexity: O(n) -> store results in an array of size (n+1).
3
+
4
+class Solution:
5
+ def countBits(self, n: int) -> List[int]:
6
+ # make an array of size (n+1), initialized with 0s.
7
+ arr = [0] * (n + 1)
8
9
+ # loop from 1 to n
10
+ for i in range(1, n + 1):
11
+ if i % 2 == 0:
12
+ # even number -> same count as i//2
13
+ arr[i] = arr[i // 2]
14
+ else:
15
+ # odd number -> one more bit than i//2
16
+ arr[i] = arr[i // 2] + 1
17
18
+ return arr
0 commit comments