Given a non negative integer number **num**. For every numbers i in the range **0 ≤ i ≤ num** calculate the number of 1's in their binary representation and return them as an array.

```
class Solution:
def countBits(self, num: int) -> List[int]:
if num < 2:
return [0, 1][:num+1]
bits = [0, 1]
i = 2
while i <= num:
bits.append(bits[i//2] + i % 2)
i += 1
return bits
```

]]>Given a `m * n`

matrix `mat`

and an integer `K`

, return a matrix `answer`

where each `answer[i][j]`

is the sum of all elements `mat[r][c]`

for `i - K <= r <= i + K, j - K <= c <= j + K`

, and `(r, c)`

is a valid position in the matrix.

```
class Solution:
def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]:
if not mat:
return mat
iMax = len(mat) - 1
jMax = len(mat[0]) - 1
prifix = [ [ 0 for _ in range(jMax + 1) ] for _ in range(iMax + 1)]
for i, row in enumerate(prifix):
for j, _ in enumerate(row):
if not i and not j:
prifix[i][j] = mat[i][j]
elif not i:
prifix[i][j] = prifix[i][j-1] + mat[i][j]
elif not j:
prifix[i][j] = prifix[i-1][j] + mat[i][j]
else:
prifix[i][j] = prifix[i-1][j] + prifix[i][j-1] - prifix[i-1][j-1] + mat[i][j]
for i, row in enumerate(mat):
for j, _ in enumerate(row):
rMin, rMax = max(i - K, 0), min(i + K, iMax)
cMin, cMax = max(j - K, 0), min(j + K, jMax)
tmp1 = prifix[rMin-1][cMin-1] if rMin and cMin else 0
tmp2 = prifix[rMin-1][cMax] if rMin else 0
tmp3 = prifix[rMax][cMin-1] if cMin else 0
mat[i][j] = prifix[rMax][cMax] + tmp1 - tmp2 - tmp3
return mat
```

]]>You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and **it will automatically contact the police if two adjacent houses were broken into on the same night**.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight **without alerting the police**.

```
class Solution:
def rob(self, nums: List[int]) -> int:
if not nums:
return 0
dp0 = 0
dp1 = nums[0]
for d in range(1, len(nums)):
tmp = dp1
dp1 = dp0 + nums[d]
dp0 = max(dp0, tmp)
return max(dp1, dp0)
```

```
class Solution:
def rob(self, nums: List[int]) -> int:
# if we have no houses to rob, we can't get any money
if not nums:
return 0
# if we only have 1 or 2 houses to rob, we take the max
if len(nums) <= 2:
return max(nums)
# else we create a state array to hold the max amount
# of money that we can accumulat at each house
state = [0 for _ in range(len(nums))]
# we can memoize our first two houses
state[0] = nums[0]
state[1] = nums[1]
# for each house after our first 2, the max amount we can take
# is equal to the max amount found in our state array, plus the
# amount of the current house
for i in range(2, len(nums)):
# we use i-1 because we cannot rob houses adjacent to the
# one we are currently robbing
state[i] = max(state[:i-1]) + nums[i]
return max(state)
```

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are **arranged in a circle**. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and **it will automatically contact the police if two adjacent houses were broken into on the same night**.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight **without alerting the police**.

```
class Solution:
def rob(self, nums: List[int]) -> int:
n = len(nums)
if not nums:
return 0
if n < 4:
return max(nums)
dp0 = [nums[1], nums[0]]
dp1 = [nums[2], nums[0] + nums[2]]
for d in range(3, n):
tmp1 = dp1[0]
tmp2 = dp1[1]
if d < n - 1:
dp1[1] = dp0[1] + nums[d]
else:
dp1[1] = dp0[1]
dp1[0] = dp0[0] + nums[d]
dp0[0] = max(dp0[0], tmp1)
dp0[1] = max(dp0[1], tmp2)
return max(dp1 + dp0)
```

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.

Determine the maximum amount of money the thief can rob tonight without alerting the police.

```
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def rob(self, root: TreeNode) -> int:
dp = [0, 0]
return max(self.calRob(root, dp))
def calRob(self, root, dp):
if not root:
return (0, 0)
dp[0], dp[1] = max(dp[0], dp[1]), dp[0] + root.val
left = self.calRob(root.left, dp)
right = self.calRob(root.right, dp)
return (max(left) + max(right) ,root.val + left[0] + right[0])
```

]]>