Skip to content

Commit 31b7ee6

Browse files
committed
2: counting bits
1 parent 33af494 commit 31b7ee6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

counting-bits/whewchews.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function countBits(n: number): number[] {
2+
// SC: O(N)
3+
const ans = Array(n + 1).fill(0);
4+
// TC: O(N)
5+
for (let i = 1; i <= n; i++) {
6+
let k = i;
7+
8+
// TC: O(log N)
9+
while (k > 0) {
10+
ans[i] += k % 2;
11+
k = Math.floor(k / 2);
12+
}
13+
}
14+
15+
return ans;
16+
}
17+
18+
// TC: O(N log N)
19+
// SC: O(N)

0 commit comments

Comments
 (0)