当前位置:网站首页>Sword finger offer II 091 Paint the house
Sword finger offer II 091 Paint the house
2022-06-26 19:53:00 【North_ dust】
Looked at the leetcode Today's A daily topic , Topic link : The finger of the sword Offer II 091. Paint the house
The title is as follows :
If there is a row of houses , common n individual , Every house can be painted red 、 One of the three colors blue or green , You need to paint all the houses and make them adjacent The two house colors Cannot be the same .
Of course , Because the prices of different colors of paint on the market are different , So the cost of painting the house in different colors is different . The cost of painting each house in a different color is one n x 3 Positive integer matrix of costs To represent the .
for example ,costs[0][0] It means the first one 0 The cost of painting the house red ;costs[1][2] It means the first one 1 The cost of painting the house green , And so on .
Please figure out that all the houses have been painted least The cost of .
I use the dynamic programming method here , The equation is as follows :
1. initialization dp Array
dp[0][0] = costs[0][0];
dp[0][1] = costs[0][1];
dp[0][2] = costs[0][2];
2. State transition equation :i The range of [1,n)
dp[i][0] = Math.min(dp[i-1][1],dp[i-1][2])+costs[i][0];
dp[i][1] = Math.min(dp[i-1][0],dp[i-1][2])+costs[i][1];
dp[i][2] =Math.min(dp[i-1][0],dp[i-1][1])+costs[i][2];
The overall code is as follows :
public int minCost(int[][] costs) {
int n = costs.length;
int len = costs[0].length;
int[][] dp = new int[n][len];
dp[0][0] = costs[0][0];
dp[0][1] = costs[0][1];
dp[0][2] = costs[0][2];
for(int i = 1;i < n;i++){
dp[i][0] = Math.min(dp[i-1][1],dp[i-1][2])+costs[i][0];
dp[i][1] = Math.min(dp[i-1][0],dp[i-1][2])+costs[i][1];
dp[i][2] = Math.min(dp[i-1][0],dp[i-1][1])+costs[i][2];
}
return Math.min(dp[n-1][0],Math.min(dp[n-1][1],dp[n-1][2]));
}
Of course, you can also change the ternary operator to try .
Minor code changes , Try to save some space , The code is as follows :
public int minCost(int[][] costs) {
int n = costs.length;
int len = costs[0].length;
int[][] dp = new int[2][len];
dp[0][0] = costs[0][0];
dp[0][1] = costs[0][1];
dp[0][2] = costs[0][2];
for(int i = 1;i < n;i++){
dp[1][0] = Math.min(dp[0][1],dp[0][2])+costs[i][0];
dp[1][1] = Math.min(dp[0][0],dp[0][2])+costs[i][1];
dp[1][2] = Math.min(dp[0][0],dp[0][1])+costs[i][2];
dp[0][0] = dp[1][0];
dp[0][1] = dp[1][1];
dp[0][2] = dp[1][2];
dp[1][0] = 0;
dp[1][0] = 0;
dp[1][0] = 0;
}
return Math.min(dp[0][0],Math.min(dp[0][1],dp[0][2]));
}
边栏推荐
- 抖音实战~搜索页面~视频详情
- Feign remote call
- On the escape of inequality value
- Résumé des points de connaissance
- Tiktok practice ~ search page ~ scan QR code
- Current limiting design and Implementation
- Some cold knowledge about QT database development
- Tiktok practice ~ homepage video ~ pull-down refresh
- Analysis on development technology of NFT meta universe chain game system
- The two files are merged into a third file.
猜你喜欢

Preliminary analysis of serial port printing and stack for arm bare board debugging

Project practice 5: build elk log collection system

ARM裸板调试之串口打印及栈初步分析

Feign remote call

Refresh the strong pointer assignment problem in the HP-UX system of Sanguan

Convex hull problem

商品秒杀系统

uni-app使用canvas绘制二维码

Current limiting design and Implementation

Yujun product methodology
随机推荐
Unit test of boot
数据库SQL语句撰写
Boot indicator monitoring
C primer plus学习笔记 —— 3、字符的IO(输入/输出)
读书笔记:《过程咨询 III》
限流设计及实现
710. 黑名单中的随机数
JSONUtils工具类(基于alibaba fastjson)
Analysis on development technology of NFT meta universe chain game system
When does the mobile phone video roll off?
微服务架构
On the origin of the dispute between the tradition and the future of database -- AWS series column
剑指 Offer II 091. 粉刷房子
Refresh the strong pointer assignment problem in the HP-UX system of Sanguan
Wechat applet uniapp left slide delete with Delete Icon
Current limiting design and Implementation
Development of NFT for digital collection platform
西瓜书重温(七): 贝叶斯分类器(手推+代码demo)
Successfully solved the problem of garbled microservice @value obtaining configuration file
The king of Internet of things protocol: mqtt