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 8360357 commit d8ec096Copy full SHA for d8ec096
counting-bits/HoonDongKang.ts
@@ -0,0 +1,32 @@
1
+/**
2
+ * [Problem]: [338] Counting Bits
3
+ * (https://leetcode.com/problems/counting-bits/description/)
4
+ */
5
+function countBits(n: number): number[] {
6
+ //시간복잡도 O(n)
7
+ //공간복잡도 O(n)
8
+ function dpFunc(n: number): number[] {
9
+ const dp = new Array(n + 1).fill(0);
10
+ let offset = 1;
11
+
12
+ for (let i = 1; i <= n; i++) {
13
+ if (offset * 2 === i) {
14
+ offset = i;
15
+ }
16
17
+ dp[i] = 1 + dp[i - offset];
18
19
20
+ return dp;
21
22
23
24
25
+ function optimizedFunc(n: number): number[] {
26
27
+ for (let i = 0; i <= n; i++) {
28
+ dp[i] = dp[i >> 1] + (i & 1);
29
30
31
32
+}
0 commit comments