Day 8 LeetCode
👨‍💻

Day 8 LeetCode

Tags
Data Structures
Python
Java
Computer Science
Published
Jan 24, 2021

Day 8 Task:

→ 338. Counting Bits:
 

Solutions:

Please review all the solutions and try implementing on your own if you haven’t gotten it.
 
class Solution:
    def countBits(self, n: int) -> List[int]:
        dp = [0] * (n + 1)
        offset = 1
        
        for i in range(1, n + 1):
            if offset * 2 == i:
                offset = i
            dp[i] = 1 + dp[i - offset]
        return dp