LeetCode 198

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.

思路:动态规划问题,只需要记录上一个和上上个的最优解就可以了。

public int rob(int[] nums) {
        if (nums.length == 0)
            return 0;

        if(nums.length == 1)
            return nums[0];

        int I_2 = nums[0];
        int I_1 = Math.max(nums[0], nums[1]);
        int temp;

        for (int i = 2; i < nums.length; i++) {
            temp = I_1;
            I_1 = Math.max(I_2 + nums[i], I_1);
            I_2 = temp;
        }

        return Math.max(I_1, I_2);
    }
时间: 2024-11-06 07:46:27

LeetCode 198的相关文章

[LeetCode] 198. 打家劫舍II ☆☆☆(动态规划)

描述 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额. 示例 1: 输入: [2,3,2]输出: 3解释: 你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的.示例

leetCode 198. House Robber | 动态规划

198. House Robber 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

LeetCode 198, 213 House Robber

198 House Robber DP 0~n-1     ans=dp[n-1] dp[i] = max(dp[i-2]+nums[i], dp[i-1])  i>=2 213 House Robber II Since we cannot rob nums[0] and nums[n-1] at the same time, we can divide this problem into two cases: not rob nums[0] not rob nums[n-1] and the

leetcode 198. House Robber (Easy)

https://leetcode.com/problems/house-robber/ 题意: 一维数组,相加不相邻的数组,返回最大的结果. 思路: 一开始思路就是DP,用一维数组保存dp[i]保存如果偷第i间,此时可偷到多少.DP的方向不太好,所以效率很低. Runtime: 4 ms, faster than 17.53% class Solution { public: int rob(vector<int> &nums) { int res = 0; int len = num

[LeetCode]198. 打家劫舍(DP)

题目 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额. 示例 1: 输入: [1,2,3,1] 输出: 4 解释: 偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3). ? 偷窃到的最高金额 = 1 + 3 = 4 . 示例 2: 输入:

LeetCode 198. House Robber

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 autom

[LeetCode] 198. House Robber Java

题目: 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 a

leetcode 198. House Robber 求抢劫的最大金额 ---------- java

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 autom

Java for LeetCode 198 House Robber

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 autom