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 778edbb commit da2339eCopy full SHA for da2339e
counting-bits/Jeehay28.ts
@@ -0,0 +1,17 @@
1
+// TC: O(n)
2
+// SC: O(n)
3
+function countBits(n: number): number[] {
4
+ const dp: number[] = [];
5
+ dp[0] = 0;
6
+
7
+ for (let i = 0; i <= n; i++) {
8
+ dp[i] = dp[i >> 1] + (i & 1);
9
+ }
10
11
+ // The number of 1s in the quotient (i >> 1) + number of 1s in the remainder (i & 1)
12
+ // dp[i >> 1]: number of 1's in Math.floor(i / 2)
13
+ // i & 1: 1 if i is odd, 0 if even
14
15
+ return dp;
16
+}
17
0 commit comments