首页 > 其他分享 >[LeetCode][198]house-robber

[LeetCode][198]house-robber

时间:2023-08-25 09:49:37浏览次数:42  
标签:nums house rob houses amount money LeetCode robber

Content

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 systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

 

Example 1:

Input: nums = [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: nums = [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.

 

Constraints:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 400
Related Topics
  • 数组
  • 动态规划

  • 标签:nums,house,rob,houses,amount,money,LeetCode,robber
    From: https://www.cnblogs.com/shea24/p/17656035.html
  • 相关文章

    • 【LeetCode动态规划#15】最长公共子序列II
      最长公共子序列(二)描述给定两个字符串str1和str2,输出两个字符串的最长公共子序列。如果最长公共子序列为空,则返回"-1"。目前给出的数据,仅仅会存在一个最长的公共子序列数据范围:0≤∣���1∣,∣���2∣≤20000≤∣str1∣,∣str2∣≤2000要求:空间复杂度�(�2)O(n2),时间复杂度�(�2)O(n2)......
    • 【LeetCode1】统计参与通信的服务器
      【题目】这里有一幅服务器分布图,服务器的位置标识在m*n的整数矩阵网格grid中,1表示单元格上有服务器,0表示没有。如果两台服务器位于同一行或者同一列,我们就认为它们之间可以进行通信。请你统计并返回能够与至少一台其他服务器进行通信的服务器的数量。【示例一】......
    • Leetcode——1957、删除字符使字符串变好
      一个字符串如果没有 三个连续 相同字符,那么它就是一个 好字符串 。给你一个字符串 s ,请你从 s 删除 最少 的字符,使它变成一个 好字符串 。请你返回删除后的字符串。题目数据保证答案总是 唯一的 。 示例1:输入:s="leeetcode"输出:"leetcode"解释:从第一组'......
    • Leetcode 1. 两数之和(Two sum)
      题目链接......
    • Leetcode 1782. 统计点对的数目
      这两天实训比较忙,之后补TRANSLATEwithxEnglishArabicHebrewPolishBulgarianHindiPortugueseCatalanHmongDawRomanianChineseSimplifiedHungarianRussianChineseTraditionalIndonesianSlovakCzechItalianSlovenianDanishJapane......
    • leetcode 12
      算法介绍:哈希贪心实现代码如下classSolution{public:stringintToRoman(intnum){//哈希贪心 strings[]={"M","CM","D","CD","C","XC","L","XL","X","IX......
    • Leetcode605——种花问题
      假设有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花不能种植在相邻的地块上,它们会争夺水源,两者都会死去。给你一个整数数组 flowerbed 表示花坛,由若干 0 和 1 组成,其中 0 表示没种植花,1 表示种植了花。另有一个数 n ,能否在不打破种植规则的情况下种入 n......
    • [LeetCode][121]best-time-to-buy-and-sell-stock
      ContentYouaregivenanarraypriceswhereprices[i]isthepriceofagivenstockontheithday.Youwanttomaximizeyourprofitbychoosingasingledaytobuyonestockandchoosingadifferentdayinthefuturetosellthatstock.Returnthemaximu......
    • LeetCode 算法题解之 26 进制转换 All In One
      LeetCode算法题解之26进制转换AllInOne26进制转换171.ExcelSheetColumnNumber171.Excel工作表列号functiontitleToNumber(columnTitle:string):number{//如何动态生成字典✅26进制//A-Z->1-26conststrs='ABCDEFGHIJKLMNOPQRSTUVWXYZ';......
    • leetcode-1-two sum(brute force, hash table)
      Wecanusebruteforcetogetit,usetwoforloopiandj,whichi=1:nandj=i:n.However,thetimecomplexityisO(n^2),whichisnotefficient.Usehashtable,thefirstthingisfirststoreeveryelementtotable,thendotraverseagaintolookup......