338. Counting Bits

https://leetcode.com/problems/counting-bits/

dp, use what you already have

count(1 0 0 1 0 0 ) = count(1 0 0 1 0 ) + 0 % 2

class Solution:
    def countBits(self, num: int) -> List[int]:
        count = [0] * (1+num)
        for i in range(num+1):
            add1 = count[i//2]
            add2 = i % 2
            count[i] = add1 + add2
        return count

Last updated

Was this helpful?