문제 링크 : Counting Bits
/**
* @param {number} n
* @return {number[]}
*/
var countBits = function(n) {
let result = new Array(n+1).fill(0);
for(let i=1; i<n+1; i++) {
result[i] = i.toString(2).match(/1/g).length
}
return result
};
/**
* @param {number} n
* @return {number[]}
*/
var countBits = function(n) {
let 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;
};