专栏首页Reck ZhangLeetCode 0198 - House Robber

LeetCode 0198 - House Robber

House Robber

Desicription

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.

Solution

class Solution {
public:
    int rob(vector<int>& nums) {
        int a = 0;
        int b = 0;
        for(int i = 0; i < nums.size(); i++) {
            if(i & 1)
                a = max(a + nums[i], b);
            else
                b = max(b + nums[i], a);
        }
        return max(a, b);
    }
};

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Baozi Training Leetcode solution 198. House Robber

    You are a professional robber planning to rob houses along a street. Each house ...

    包子面试培训
  • 包子君top 5油管中文up主 & Leetcode solution 213. House Robber II

    咳咳,标题党一下。。。仅仅是包子君(行者)的最近喜好,不代表包子其他老师。哎,书是读的越来越少了,youtube看的越来越多了,应该是快废了。。。

    包子面试培训
  • 198. 打家劫舍

    你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小...

    lucifer210
  • 【Leetcode 198】关关的刷题日记69 – Leetcode 198 House Robber

    关关的刷题日记69 – Leetcode 198 House Robber 题目 You are a professional robber planning ...

    WZEARW
  • Leetcode: House Robber

    题目: You are a professional robber planning to rob houses along a street. Each ...

    卡尔曼和玻尔兹曼谁曼
  • Leetcode 198 House Robber

    You are a professional robber planning to rob houses along a street. Each house...

    triplebee
  • leetcode 198 House Robber

    You are a professional robber planning to rob houses along a street. Each house ...

    流川疯
  • 动态规划算法练习(5)--medium

    中文链接:https://leetcode-cn.com/problems/predict-the-winner/ 英文链接:https://leetcode...

    生信编程日常
  • Leetcode 213 House Robber II

    Note: This is an extension of House Robber. After robbing those houses on that...

    triplebee
  • Leetcode 213. House Robber II

    版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn....

    Tyan
  • LeetCode 198. House Robber(DP)

    题意:给你一个数组表示一个街道,每个数字表示一个房子里有多少钱,你可以打劫获得这些钱,但是不能同时打劫相邻的房子,问你最多可以得到多少钱,

    ShenduCC
  • LeetCode 0337 - House Robber III

    The thief has found himself a new place for his thievery again. There is only on...

    Reck Zhang
  • LeetCode 0213 - House Robber II

    You are a professional robber planning to rob houses along a street. Each house ...

    Reck Zhang
  • Golang Leetcode 213. House Robber II.go

    版权声明:原创勿转 https://blog.csdn.net/anakinsun/article/details/89043336

    anakinsun
  • Golang Leetcode 337. House Robber III.go

    版权声明:原创勿转 https://blog.csdn.net/anakinsun/arti...

    anakinsun
  • 动态规划|相邻约束下的最优解(House Robber II )

    01 House Robber II This time, all houses at this place are arranged in a circl...

    double
  • 经典动态规划:打家劫舍系列问题

    有好几位读者私下问我 LeetCode 「打家劫舍」系列问题(英文版叫 House Robber)怎么做,我发现这一系列题目的点赞非常之高,是比较有代表性和技巧...

    labuladong
  • House Robber

    Tyan
  • House Robber

    ou are a professional robber planning to rob houses along a street. Each house h...

    Tyan

扫码关注云+社区

领取腾讯云代金券