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

    提示:

    此题是一道动态规划的题目。

    代码:

    class Solution {
    public:
        int rob(vector<int>& nums) {
            int a = 0, b = 0;
            for (int i = 0; i < nums.size(); ++i) {
                if (i % 2 == 0) a = max(a + nums[i], b);
                else b = max(a, b + nums[i]);
            }
            return max(a, b);
        }
    };
  • 相关阅读:
    Spring----Day03
    Spring----Day02
    python
    python
    python
    python
    python
    python
    python
    python
  • 原文地址:https://www.cnblogs.com/jdneo/p/4778712.html
Copyright © 2011-2022 走看看