house_robber

 1from functools import cache
 2
 3
 4# @leet start
 5class Solution:
 6    def rob(self, nums: list[int]) -> int:
 7        """
 8        In this question, you are given an array of payoffs, where you can either
 9        take the current payoff, or skip it. The reason why you would skip it
10        is because if you take the current number, you can't take the next number.
11
12        So, we can create a dp array, where you can either choose the current
13        number + 2 indexes, or you can choose to skip and just take the result
14        of the next index.
15
16        We can turn this into code, and then return the maximum of the array.
17        """
18        n = len(nums)
19
20        @cache
21        def dp(i):
22            if 0 <= i < n:
23                return max(nums[i] + dp(i + 2), dp(i + 1))
24            else:
25                return 0
26
27        return max(dp(i) for i in range(n))
28
29
30# @leet end
31
32
33def test():
34    assert 2 + 2 == 4
class Solution:
 6class Solution:
 7    def rob(self, nums: list[int]) -> int:
 8        """
 9        In this question, you are given an array of payoffs, where you can either
10        take the current payoff, or skip it. The reason why you would skip it
11        is because if you take the current number, you can't take the next number.
12
13        So, we can create a dp array, where you can either choose the current
14        number + 2 indexes, or you can choose to skip and just take the result
15        of the next index.
16
17        We can turn this into code, and then return the maximum of the array.
18        """
19        n = len(nums)
20
21        @cache
22        def dp(i):
23            if 0 <= i < n:
24                return max(nums[i] + dp(i + 2), dp(i + 1))
25            else:
26                return 0
27
28        return max(dp(i) for i in range(n))
def rob(self, nums: list[int]) -> int:
 7    def rob(self, nums: list[int]) -> int:
 8        """
 9        In this question, you are given an array of payoffs, where you can either
10        take the current payoff, or skip it. The reason why you would skip it
11        is because if you take the current number, you can't take the next number.
12
13        So, we can create a dp array, where you can either choose the current
14        number + 2 indexes, or you can choose to skip and just take the result
15        of the next index.
16
17        We can turn this into code, and then return the maximum of the array.
18        """
19        n = len(nums)
20
21        @cache
22        def dp(i):
23            if 0 <= i < n:
24                return max(nums[i] + dp(i + 2), dp(i + 1))
25            else:
26                return 0
27
28        return max(dp(i) for i in range(n))

In this question, you are given an array of payoffs, where you can either take the current payoff, or skip it. The reason why you would skip it is because if you take the current number, you can't take the next number.

So, we can create a dp array, where you can either choose the current number + 2 indexes, or you can choose to skip and just take the result of the next index.

We can turn this into code, and then return the maximum of the array.

def test():
34def test():
35    assert 2 + 2 == 4