forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHoonDongKang.ts
More file actions
32 lines (28 loc) · 775 Bytes
/
HoonDongKang.ts
File metadata and controls
32 lines (28 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* [Problem]: [338] Counting Bits
* (https://leetcode.com/problems/counting-bits/description/)
*/
function countBits(n: number): number[] {
//시간복잡도 O(n)
//공간복잡도 O(n)
function dpFunc(n: number): number[] {
const dp = new Array(n + 1).fill(0);
let offset = 1;
for (let i = 1; i <= n; i++) {
if (offset * 2 === i) {
offset = i;
}
dp[i] = 1 + dp[i - offset];
}
return dp;
}
//시간복잡도 O(n)
//공간복잡도 O(n)
function optimizedFunc(n: number): number[] {
const dp = new Array(n + 1).fill(0);
for (let i = 0; i <= n; i++) {
dp[i] = dp[i >> 1] + (i & 1);
}
return dp;
}
}