Skip to content

Latest commit

 

History

History
191 lines (156 loc) · 4.35 KB

File metadata and controls

191 lines (156 loc) · 4.35 KB

中文文档

Description

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 a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

 

Example 1:

Input: nums = [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.

Example 2:

Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.

Example 3:

Input: nums = [1,2,3]
Output: 3

 

Constraints:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 1000

Solutions

Python3

class Solution:
    def rob(self, nums: List[int]) -> int:
        def robRange(nums, l, r):
            a, b = 0, nums[l]
            for num in nums[l + 1 : r + 1]:
                a, b = b, max(num + a, b)
            return b

        n = len(nums)
        if n == 1:
            return nums[0]
        s1, s2 = robRange(nums, 0, n - 2), robRange(nums, 1, n - 1)
        return max(s1, s2)

Java

class Solution {
    public int rob(int[] nums) {
        int n = nums.length;
        if (n == 1) {
            return nums[0];
        }
        int s1 = robRange(nums, 0, n - 2);
        int s2 = robRange(nums, 1, n - 1);
        return Math.max(s1, s2);
    }

    private int robRange(int[] nums, int l, int r) {
        int a = 0, b = nums[l];
        for (int i = l + 1; i <= r; ++i) {
            int c = Math.max(nums[i] + a, b);
            a = b;
            b = c;
        }
        return b;
    }
}

C++

class Solution {
public:
    int rob(vector<int>& nums) {
        int n = nums.size();
        if (n == 1) return nums[0];
        int s1 = robRange(nums, 0, n - 2);
        int s2 = robRange(nums, 1, n - 1);
        return max(s1, s2);
    }

    int robRange(vector<int>& nums, int l, int r) {
        int a = 0, b = nums[l];
        for (int i = l + 1; i <= r; ++i) {
            int c = max(nums[i] + a, b);
            a = b;
            b = c;
        }
        return b;
    }
};

Go

func rob(nums []int) int {
	n := len(nums)
	if n == 1 {
		return nums[0]
	}
	s1, s2 := robRange(nums, 0, n-2), robRange(nums, 1, n-1)
	return max(s1, s2)
}

func robRange(nums []int, l, r int) int {
	a, b := 0, nums[l]
	for i := l + 1; i <= r; i++ {
		a, b = b, max(nums[i]+a, b)
	}
	return b
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

TypeScript

function rob(nums: number[]): number {
    const n = nums.length;
    if (n === 1) {
        return nums[0];
    }
    const robRange = (left: number, right: number) => {
        const dp = [0, 0];
        for (let i = left; i < right; i++) {
            [dp[0], dp[1]] = [dp[1], Math.max(dp[1], dp[0] + nums[i])];
        }
        return dp[1];
    };
    return Math.max(robRange(0, n - 1), robRange(1, n));
}

Rust

impl Solution {
    pub fn rob(nums: Vec<i32>) -> i32 {
        let n = nums.len();
        if n == 1 {
            return nums[0];
        }
        let rob_range = |left, right| {
            let mut dp = [0, 0];
            for i in left..right {
                dp = [dp[1], dp[1].max(dp[0] + nums[i])];
            }
            dp[1]
        };
        rob_range(0, n - 1).max(rob_range(1, n))
    }
}

...