1. What I learned
bin()
It is used when a number is converted to a binary number.
print(bin(4))
print(bin(5))
# 100
# 101
2. Code
class Solution:
def countBits(self, num: int) -> List[int]:
dp = [0]
for i in range(1, num+1): # You have to find the rules by writing it yourself
if i%2 == 1:
dp.append(dp[i-1]+1)
else:
dp.append(dp[i//2])
return dp
3. Result
Runtime : 76 ms(89.95%), Memory usage : 20.8 MB(78.32%)
(Runtime can be different by a system even if it is a same code.)