LeetCode 338: Counting Bits by Reusing Smaller Results
Problem Requirement Given a non-negative integer n, return an array answer of length n + 1. For every index: answer[i] = the number of 1 bits in the binary representation of i The requested range includes both 0 and n. LeetCode provides this method contract: countBits(n: int) -> List[int] Example 1 Input: n = 2 Output: [0,1,1] 0 -> 0 -> 0 set bits 1 -> 1 -> 1 set bit 2 -> 10 -> 1 set bit Example 2 Input: n = 5 Output: [0,1,1,2,1,2] Constraints 0 <= n <= 10^5 Step 1: This Time We Need Every Answer From 0 to n LeetCode 191 asks for one integer’s set-bit count. For n = 5, this problem asks for all six related answers: ...