当前位置:网站首页>213. 打家劫舍 II-动态规划
213. 打家劫舍 II-动态规划
2022-07-24 17:31:00 【Mr Gao】
213. 打家劫舍 II-动态规划
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警 。
给定一个代表每个房屋存放金额的非负整数数组,计算你 在不触动警报装置的情况下 ,今晚能够偷窃到的最高金额。
示例 1:
输入:nums = [2,3,2]
输出:3
解释:你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的。
示例 2:
输入:nums = [1,2,3,1]
输出:4
解释:你可以先偷窃 1 号房屋(金额 = 1),然后偷窃 3 号房屋(金额 = 3)。
偷窃到的最高金额 = 1 + 3 = 4 。
示例 3:
输入:nums = [1,2,3]
输出:3
解题代码如下:
int rob(int* nums, int numsSize){
int dp[numsSize+1];
dp[0]=0;
dp[1]=nums[0];
int r[numsSize+1];
r[1]=1;
if(numsSize==1){
return nums[0];
}
r[0]=0;
if(numsSize==2){
return fmax(nums[0],nums[1]);
}
for(int i=2;i<=numsSize;i++){
if(dp[i-1]>nums[i-1]+dp[i-2]){
dp[i]=dp[i-1];
if(r[i-1]==1){
r[i]=1;
}
else{
r[i]=0;
}
}
else{
dp[i]=nums[i-1]+dp[i-2];
if(r[i-2]==1){
r[i]=1;
}
else{
r[i]=0;
}
}
if(i==numsSize&&r[i]==1){
dp[i]=dp[i]-nums[0];
}
}
int a=fmax(dp[numsSize],dp[numsSize-1]);
dp[1]=0;
dp[2]=nums[1];
if(numsSize==2){
return fmax(nums[0],nums[1]);
}
for(int i=3;i<=numsSize;i++){
dp[i]=0;
if(dp[i-1]>nums[i-1]+dp[i-2]){
dp[i]=dp[i-1];
}
else{
dp[i]=nums[i-1]+dp[i-2];
}
}
return fmax(a,dp[numsSize]);
}
边栏推荐
- Development Series III of GaN (lapgan, srgan)
- TCP协议调试工具TcpEngine V1.3.0使用教程
- ShardingSphere数据库读写分离
- wallys/IPQ8074A 4x4 2.4G 8x8 5G 802.11ax
- Demonstration experiment of scrollbar for adjusting image brightness
- Trends of semiconductor industry
- 别再到处乱放配置文件了!试试我司使用 7 年的这套解决方案,稳的一秕
- Analyze the capabilities and scenarios of Apache pulsar, a cloud native message flow system
- AI opportunities for operators: expand new tracks with large models
- Atcoder Beginner 202 E - Count Descendants(离线查询 重链剖分树上启发式合并)
猜你喜欢

UFW port forwarding

Scept: consistent and strategy based trajectory prediction for planned scenarios

Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection

Is computer monitoring true? Four experiments to find out

Practical application cases of digital Twins - Smart Park

使用matplotlib模拟线性回归

HCNP Routing&Switching之DHCP中继

List of stringutils and string methods

The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced

The orders in the same city are delivered in the same city, and the order explosion is still handy!
随机推荐
Kyligence attended the Huawei global smart finance summit to accelerate the expansion of the global market
Three.js (7): local texture refresh
nc 端口转发
二维卷积——torch.nn.conv2d的使用
One article of quantitative framework backtrader: understand indicator indicators
Tensorflow introductory tutorial (37) -- DC Vnet
Memory allocation and recycling strategy
Scroll bar adjust brightness and contrast
电脑监控是真的吗?4个实验一探究竟
Array learning navigation
JS & TS learning summary
Exception handling - a small case that takes you to solve NullPointerException
Practical application cases of digital Twins - Smart Park
【GNN报告】腾讯AI lab 徐挺洋:图生成模型及其在分子生成中的应用
JS image conversion Base64 Base64 conversion to file object
TCP protocol debugging tool tcpengine v1.3.0 tutorial
Analyze the capabilities and scenarios of Apache pulsar, a cloud native message flow system
微信朋友圈的高性能复杂度分析
pinia 入门及使用
wallys/IPQ8074A 4x4 2.4G 8x8 5G 802.11ax