当前位置:网站首页>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]));
}
边栏推荐
- Wechat applet uniapp left slide delete with Delete Icon
- MySQL recharge
- 回溯思路详解
- Nftgamefi chain game system development detailed solution - chain game system development principle analysis
- 关于Qt数据库开发的一些冷知识
- Three basic backup methods of mongodb
- 【推荐收藏】这8个常用缺失值填充技巧一定要掌握
- Feign远程调用
- 抖音实战~首页视频~下拉刷新
- Arduino UNO + DS1302利用31字节静态RAM存储数据并串口打印
猜你喜欢

Some basic mistakes

Database SQL statement writing

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

mysql的充值问题

Tiktok practice ~ sharing module ~ short video download (save to photo album)

Project practice 6: distributed transaction Seata

Three basic backup methods of mongodb

Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port

Solidity - 合约继承子合约包含构造函数时报错 及 一个合约调用另一合约view函数收取gas费用

Xlua get button registration click event of ugui
随机推荐
微服务版单点登陆系统(SSO)
Solve com mysql. jdbc. exceptions. jdbc4.MySQLNonTransientConnectionException: Could not create connection
Solidity - contract inheritance sub contract contains constructor errors and one contract calls the view function of another contract to charge gas fees
Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
Can I open an account online? Is it safe?
Résolution du problème: la machine virtuelle n'a pas pu copier et coller le fichier
项目实战五:搭建ELk日志收集系统
[MySQL series] collection of common working SQL (continuous update)
MySQL recharge
郭明錤:苹果 AR / MR 头显是其有史以来设计最复杂的产品,将于 2023 年 1 月发布
Logstash安装及使用
What are the specific steps for opening a stock account? Is it safe to open an account online?
C exercise. Class list plus records, display records and clear records
MySQL - subquery usage
Nftgamefi chain game system development detailed solution - chain game system development principle analysis
超分之VRT
Arduino UNO + DS1302利用31字节静态RAM存储数据并串口打印
Pinda general permission system (day 3~day 4)
Development of NFT for digital collection platform
股票开户的具体步骤是什么?网上开户安全吗?