-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy path0338-counting-bits.py
More file actions
22 lines (15 loc) · 874 Bytes
/
Copy path0338-counting-bits.py
File metadata and controls
22 lines (15 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""
Problem: LeetCode 338 - Counting Bits
Key Idea:
To count the number of 1 bits for each number in the range [0, num], we can use dynamic programming. We observe that the number of 1 bits in a number x is equal to the number of 1 bits in x // 2 plus the value of the least significant bit (x % 2).
Time Complexity:
- We iterate through each number in the range [0, num] and perform constant-time operations for each number. Therefore, the time complexity is O(n), where n is the value of 'num'.
Space Complexity:
- The space complexity is O(n), as we use an array of size 'num + 1' to store the count of 1 bits for each number in the range.
"""
class Solution:
def countBits(self, num: int) -> List[int]:
bits_count = [0] * (num + 1)
for i in range(1, num + 1):
bits_count[i] = bits_count[i // 2] + (i % 2)
return bits_count