【LeetCode 面试经典150题】198. House Robber 房屋偷盗者

文章讲述了如何使用动态规划方法解决一个模拟偷窃问题,给定一个房子金钱数组,目标是在不触动警报的情况下,计算最大可能偷窃总金额,考虑相邻房屋的安全系统约束。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

中文释义

你是一个专业的窃贼,计划沿街偷窃房屋。每个房屋都藏有一定数量的金钱,唯一阻止你偷窃每个房屋的限制是,相邻的房屋有连接的安全系统,如果同一夜两个相邻的房屋被闯入,它将自动联系警察。

给定一个整数数组 nums,代表每个房屋的金钱数量,返回今晚你可以在不触动警报的情况下偷窃的最大金钱量。

示例

  • 示例 1:
    • 输入:nums = [1,2,3,1]
    • 输出:4
    • 解释:偷窃第 1 个房屋 (金钱 = 1) 然后偷窃第 3 个房屋 (金钱 = 3)。
      你可以偷窃的总金额 = 1 + 3 = 4。
  • 示例 2:
    • 输入:nums = [2,7,9,3,1]
    • 输出:12
    • 解释:偷窃第 1 个房屋 (金钱 = 2), 第 3 个房屋 (金钱 = 9) 和第 5 个房屋 (金钱 = 1)。
      你可以偷窃的总金额 = 2 + 9 + 1 = 12。

限制条件

  • 1 <= n
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值