LeetCode之“动态规划”:House Robber && House Robber II

  House Robber题目链接

  House Robber II题目链接

  1. 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 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.

  Credits:
  Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.

   该题相当于从一个数组中取出一个或多个不相邻的数,使其和最大。具体代码如下:

 1 class Solution {
 2 public:
 3     int rob(vector<int>& nums) {
 4         int sz = nums.size();
 5         int * dp = new int[sz];
 6         for(int i = 0; i < sz; i++)
 7         {
 8             if(i == 0)
 9                 dp[0] = nums[0];
10             else if(i == 1)
11                 dp[1] = max(nums[1], nums[0]);
12             else
13                 dp[i] = max(nums[i] + dp[i - 2], dp[i - 1]);
14         }
15         return dp[sz - 1];
16     }
17 };

  2. House Robber II

  题目要求:

  Note: This is an extension of House Robber.

  After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one.   Meanwhile, the security system for these houses remain the same as for those in the previous street.

  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.

  Credits:
  Special thanks to @Freezen for adding this problem and creating all test cases.

  对于环形,主要考虑两种情况:

  1) 第一个房子被偷:那么此时第二个房子和最后一个房子都不能被偷了,即我们可以从第三个房子到倒数最后一个房子之间用线性的动态规划求解。

  2) 第一个房子没有被偷:那么此时我们可以从第二个房子到最后一个房子之间用线性的动态规划求解。

  具体代码如下:

 1 class Solution {
 2 public:
 3     int simpleRob(vector<int>& nums, int start, int end) {
 4         int sz = end - start + 1;
 5         int * dp = new int[sz];
 6         for(int i = 0; i < sz; i++)
 7         {
 8             if(i == 0)
 9                 dp[0] = nums[start];
10             else if(i == 1)
11                 dp[1] = max(nums[start + 1], nums[start]);
12             else
13                 dp[i] = max(nums[start + i] + dp[i - 2], dp[i - 1]);
14         }
15         return dp[sz - 1];
16     }
17
18     int rob(vector<int>& nums) {
19         int sz = nums.size();
20         if(sz == 0)
21             return 0;
22         else if(sz == 1)
23             return nums[0];
24         else
25             return max(simpleRob(nums, 0, sz - 2), simpleRob(nums, 1, sz - 1));
26     }
27 };

时间: 2025-01-02 16:17:01

LeetCode之“动态规划”:House Robber && House Robber II的相关文章

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 -day11 Clone Graph &amp; Palindrome Partitioning I II

 1.Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node lab

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memor

Leetcode | Search in Rotated Sorted Array I &amp; II

Search in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise re

[leetcode笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

【leetcode刷题笔记】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的

leetcode -day28 Unique Binary Search Trees I II

1.  Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 /

【Leetcode】Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

leetcode第一刷_Combination Sum Combination Sum II

啊啊啊啊,好怀念这种用递归保存路径然后打印出来的题目啊,好久没遇到了. 分了两种,一种是可以重复使用数组中数字的,一种是每个数字只能用一次的.其实没有多大区别,第一种每次进入递归的时候都要从头开始尝试,第二种要找一个标记的数组,把已经用到过的排除掉,就像生成全排列时的做法一样.跟我一样用引用保存中间结果的话,要注意回退的情况.第二种回退时,要把用到的那个数也恢复为可用,就完全像全排列时做的一样.破例贴两个题的代码,因为他们是在是不值得用两片文章来写. class Solution { publi

Leetcode | Remove Duplicates from Sorted List I &amp;&amp; II

Remove Duplicates from Sorted List I Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 如果下一个节点和当前节点的值一样,就继续往后.这