当前位置:网站首页>Leetcode refers to offer II 089 House theft
Leetcode refers to offer II 089 House theft
2022-06-24 04:11:00 【freesan44】
subject
A professional thief , Plan to steal houses along the street . There is a certain amount of cash in every room , The only constraint on thieves' theft is that adjacent houses are equipped with interconnected anti-theft systems , If two adjacent houses are broken into by thieves on the same night , The system will automatically alarm .
Given an array of non negative integers representing the storage amount of each house nums , Please calculate Without triggering the alarm , The maximum amount that can be stolen overnight .
Example 1: Input :nums = [1,2,3,1] Output :4 explain : Steal 1 House No ( amount of money = 1) , And then steal 3 House No ( amount of money = 3). Maximum amount stolen = 1 + 3 = 4 . Example 2: Input :nums = [2,7,9,3,1] Output :12 explain : Steal 1 House No ( amount of money = 2), Steal 3 House No ( amount of money = 9), Then steal 5 House No ( amount of money = 1). Maximum amount stolen = 2 + 9 + 1 = 12 .
Tips :
1 <= nums.length <= 100
0 <= numsi <= 400
Their thinking
class Solution:
def rob(self, nums: [int]) -> int:
dp = [0]*len(nums)
# The boundary conditions
if len(nums) == 1:
return nums[0]
elif len(nums) == 2:
return max(nums[0],nums[1])
dp[0] = nums[0]
dp[1] = max(nums[0],nums[1])
for i in range(2, len(nums)):
dp[i] = max(dp[i-2]+nums[i],dp[i-1])
# print(dp)
return dp[-1]
if __name__ == "__main__":
nums = [2,7,9,3,1]
nums = [1, 2, 3, 1]
nums = [2,1,1,2]
ret = Solution().rob(nums)
print(ret)边栏推荐
- Easyplayer consumes traffic but does not play video and reports an error libdecoder Wasm404 troubleshooting
- How to remote server is the price of the server expensive
- How to monitor multiple platforms simultaneously when easydss/easygbs platform runs real-time monitoring?
- Discussion on the introduction principle and practice of fuzzy testing
- High availability architecture design to deal with network failure of operators
- 3. go deep into tidb: perform optimization explanation
- Analysis of grafana SSO authentication process based on keyloak
- After 20 years of development, is im still standing still?
- Making a Chatbot based on gpt2
- What is FTP? What is the FTP address of the ECS?
猜你喜欢

Kubernetes 资源拓扑感知调度优化

应用实践 | Apache Doris 整合 Iceberg + Flink CDC 构建实时湖仓一体的联邦查询分析架构

web技术分享| 【地图】实现自定义的轨迹回放

Application practice | Apache Doris integrates iceberg + Flink CDC to build a real-time federated query and analysis architecture integrating lake and warehouse

黑帽SEO实战之通用301权重pr劫持

开源之夏2022中选结果公示,449名高校生将投入开源项目贡献

618 promotion: mobile phone brand "immortal fight", high-end market "who dominates the ups and downs"?

Idea 1 of SQL injection bypassing the security dog

openEuler Kernel 技术分享第 20 期 | 执行实体创建与切换

Changjiang Dayong, director of openeuler community: jointly promote the new open source model of Euler and jointly build a new open source system
随机推荐
"The first share of Chinese member e-commerce" gathered in the anti reptile attack and defense war | talk with industrial security experts
Kubernetes 资源拓扑感知调度优化
Tsingsee Qingxi video easycvr integrated Dahua face recognition equipment
15+ urban road element segmentation application, this segmentation model is enough
openEuler社区理事长江大勇:共推欧拉开源新模式 共建开源新体系
Clickhouse (02) Clickhouse architecture design introduction overview and Clickhouse data slicing design
What should I pay attention to when choosing a data center?
Black hat SEO actual combat directory wheel chain generates millions of pages in batch
LeetCode 1281. Difference of sum of bit product of integer
How much space does structure variable occupy in C language
Mac CentOS installation phpredis
Difference and efficiency between get winevent and get eventlog
Changjiang Dayong, director of openeuler community: jointly promote the new open source model of Euler and jointly build a new open source system
Use the fluxbox desktop as your window manager
Student information management system user manual
Received status code 502 from server: Bad Gateway
How to select the application of the server?
黑帽SEO实战之通用301权重pr劫持
Demonstration of the processing of divisor 0 in SQL
Jointly build Euler community and share Euler ecology | join hands with Kirin software to create a digital intelligence future