zoukankan      html  css  js  c++  java
  • 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.

    Example 1:

    Input: [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 2:

    Input: [2,7,9,3,1]
    Output: 12
    Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
                 Total amount you can rob = 2 + 9 + 1 = 12.

    题目大意:

    数组中每个元素代表一家的财产数量,相邻两家不能都抢,求可抢到的财产的最大数量。

    递归解决:

     1 class Solution {
     2 public:
     3     
     4     vector<int> result; //消除冗余计算
     5     int solve(vector<int>& nums, int idx) { //当前下标及以前可抢到的最大财产
     6         if (idx < 0)
     7             return 0;
     8         if (result[idx] >= 0)
     9             return result[idx];
    10         result[idx] = max(nums[idx] + solve(nums, idx - 2),
    11                   solve(nums, idx - 1));
    12         return result[idx];
    13     }
    14     
    15     int rob(vector<int>& nums) {
    16         result.resize(nums.size(), -1);
    17         return solve(nums, nums.size() - 1);
    18     }
    19 };

    迭代解决:

     1 class Solution {
     2 public:
     3     int rob(vector<int>& nums) {
     4         if (nums.size() == 0)
     5             return 0;
     6         vector<int> result(nums.size());
     7         result[0] = nums[0];
     8         if (nums.size() == 1)
     9             return nums[0];
    10         //result[i]为数组中下标从0到i能抢到的最多财产
    11         result[1] = max(nums[0], nums[1]);
    12         for (int i = 2; i < nums.size(); i++)
    13             result[i] = max(result[i - 1], nums[i] + result[i - 2]);
    14         return result[nums.size() - 1];
    15     }
    16 };

    或者不用数组:

     1 class Solution {
     2 public:
     3     int rob(vector<int>& nums) {
     4         if (nums.size() == 0)
     5             return 0;
     6         if (nums.size() == 1)
     7             return nums[0];
     8         int cur, two_back, one_back;
     9         two_back = nums[0];
    10         cur = one_back = max(nums[0], nums[1]);
    11         for (int i = 2; i < nums.size(); i++) {
    12             cur = max(nums[i] + two_back, one_back);
    13             two_back = one_back;
    14             one_back = cur;
    15         }
    16         return cur;
    17     }
    18 };
  • 相关阅读:
    Unity--截取屏幕任意区域
    IOS 提交审核,遇到Missing Push Notification Entitlement 问题。
    VSync Count 垂直同步
    unity3d 自动保存
    首次发布App,In-App Purchase 无法submit for review 问题的解决方案
    国内银行CNAPS CODE 查询
    苹果开发——App内购以及验证store的收据(二)
    C#
    AJAX
    SQLite连接C#笔记
  • 原文地址:https://www.cnblogs.com/lxc1910/p/10482734.html
Copyright © 2011-2022 走看看