Skip to content

Commit e113687

Browse files
committed
feat(soobing): week14 > counting-bits
1 parent cf7dbbe commit e113687

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

โ€Žcounting-bits/soobing.tsโ€Ž

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* ๋ฌธ์ œ ์„ค๋ช…
3+
* - 0๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ๋ชจ๋“  ์ˆ˜์— ๋Œ€ํ•ด ์ด์ง„์ˆ˜์—์„œ 1์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ.
4+
*
5+
* ์•„์ด๋””์–ด
6+
* 1) DP + ๋น„ํŠธ์—ฐ์‚ฐ
7+
* - ans[i] = ans[i >> 1] + (i & 1)
8+
*/
9+
function countBits(n: number): number[] {
10+
const ans = Array(n + 1).fill(0);
11+
for (let i = 1; i <= n; i++) {
12+
ans[i] = ans[i >> 1] + (i & 1);
13+
}
14+
return ans;
15+
}

0 commit comments

Comments
ย (0)