/**
* @param {number} num
* @return {number[]}
*/
var countBits = function(num) {
var result=[0];
var start=1;
var curr =1;
for(var i=1;i<=num;i++){
if((i&(i))===i){
result.push(1);
start=1;
curr=i;
}
else{
result.push(result[curr]+result[start]);
start++;
}
}
return result;
};
Test case runs fine, but got MLE on submitting, anyone can help?

I had the same problem. Great solution by the way! Very clever with the i&i check for powers of 2. One improvement could be removing the curr variable. Correct me if I'm wrong, but isn't result[curr] always going to equal 1? curr will only ever point to powers of 2 and you skipped the only case where a power of 2 has a number of 1s other than exactly one (0). Anyway, nice solution.