/338. Counting Bits

338. Counting Bits

Easy
Dynamic Programming80.5% acceptance

Given a non-negative integer max_value, return a list bit_counts of length max_value + 1 such that for each integer idx (0 <= idx <= max_value), bit_counts[idx] is the number of set bits (1's) in the binary representation of idx.

Example 1

Input: max_value=3

Output: [0, 1, 2, 1]

Explanation: 0 -> 0 (0 ones), 1 -> 1 (1 one), 2 -> 10 (1 one), 3 -> 11 (2 ones)

Example 2

Input: max_value=6

Output: [0, 1, 1, 2, 1, 2, 2]

Explanation: 0->0, 1->1, 2->10, 3->11, 4->100, 5->101, 6->110

Constraints

  • 0 <= max_value <= 105
Python (current runtime)

Case 1

Input: max_value=4

Expected: [0, 1, 1, 2, 1]

Case 2

Input: max_value=7

Expected: [0, 1, 1, 2, 1, 2, 2, 3]

Case 3

Input: max_value=1

Expected: [0, 1]

Case 4

Input: max_value=0

Expected: [0]