[LeetCode] HouseRobber 动态规划

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.

思路:这是一道动态规划的题目,先列出动态方程max[i]=Max{max[i-2]+nums[i],max[i-1]},根据动态方程写程序

时间复杂度:O(n)

代码:

    public int rob(int[] nums) {
        if(nums==null || nums.length<1) return 0;
        if(nums.length==1) return nums[0];
        if(nums.length==2) return Math.max(nums[0], nums[1]);
        int [] max=new int[nums.length];
        max[0]=nums[0];
        max[1]=Math.max(nums[0], nums[1]);
        for(int i=2;i<nums.length;i++)
        {
            max[i]=Math.max(max[i-2]+nums[i], max[i-1]);
        }
        return max[nums.length-1];
    }

优化:

扩展:

时间: 2024-10-12 23:28:43

[LeetCode] HouseRobber 动态规划的相关文章

Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 现在考虑网格中有障碍物.那么从左上角到右下角将会有多少条不同的路径? 网格中的障碍物和空位置分别用 1 和 0 来表示. 说明:m 和 n 的值均不超过 100. 示例 1: 输入: [   [0,0,0],  

【Leetcode】动态规划问题详解(持续更新)

1.动态规划算法步骤(Dynamic Programming) 动态规划算法一般用来求解最优化问题,当问题有很多可行解,而题目要求寻找这些解当中的"最大值"/"最小值"时,通常可以采用DP. 动态规划算法与分治法相似,都是通过组合子问题的解来求解原问题.所不同的是,动态规划应用于子问题重叠的情况,在递归求解子问题的时候,一些子子问题可能是相同的,这种情况下,分治法会反复地计算同样的子问题,而动态规划对于相同的子问题只计算一次. 动态规划算法的设计步骤: 1.刻画最优

LeetCode之“动态规划”:Unique Binary Search Trees &amp;&amp; Unique Binary Search Trees II

1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题目分析参考自一博

LeetCode之“动态规划”:Longest Valid Parentheses

题目链接 题目要求: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another e

LeetCode之动态规划

62. Unique Paths QuestionEditorial Solution Total Accepted: 86710 Total Submissions: 239084 Difficulty: Medium A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or rig

LeetCode之“动态规划”:Dungeon Game

题目链接 题目要求: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must f

LeetCode之“动态规划”:Maximum Subarray

题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If yo

LeetCode之“动态规划”:Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III &amp;&amp; IV

1. Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), desi

LeetCode之“动态规划”:Distinct Subsequences

题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing th