当前位置:网站首页>Sword finger offer II 091 Paint the house
Sword finger offer II 091 Paint the house
2022-06-28 07:02:00 【Base-Case】
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 the two adjacent houses not the same color .
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 calculate the minimum cost of painting all the houses .
Example 1:
Input : costs = [[17,2,17],[16,16,5],[14,3,19]]
Output : 10
explain : take 0 No. 1 house painted blue ,1 No. 1 house painted green ,2 No. 1 house painted blue .
Minimum cost : 2 + 5 + 3 = 10.
Example 2:
Input : costs = [[7,6,2]]
Output : 2
Tips :
costs.length == n
costs[i].length == 3
1 <= n <= 100
1 <= costs[i][j] <= 20
source : Power button (LeetCode)
link :https://leetcode.cn/problems/JEj789
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
1. Recurrence of violence ( Overtime )
int min(int a,int b){
return a<b?a:b;
}
//k What was the number of the last house painted
int f(int** costs,int row,int col,int idx,int k)
{
if(idx==row){
return 0;
}
int p1,p2,p3;
p1=p2=p3=100000000;
if(k!=0){
p1=costs[idx][0]+f(costs,row,col,idx+1,0);
}
if(k!=1){
p2=costs[idx][1]+f(costs,row,col,idx+1,1);
}
if(k!=2){
p3=costs[idx][2]+f(costs,row,col,idx+1,2);
}
return min(p1,min(p2,p3));
}
int minCost(int** costs, int costsSize, int* costsColSize){
return f(costs,costsSize,*costsColSize,0,-1);
}2. Dynamic programming (
Execution time :4 ms, In all C Defeated in submission 98.11% Users of
Memory consumption :5.9 MB, In all C Defeated in submission 100.00% Users of
)
int min(int a,int b){
return a<b?a:b;
}
int minCost(int** costs, int costsSize, int* costsColSize){
int dp[costsSize][*costsColSize];
memset(dp,0,sizeof(dp));
dp[0][0]=costs[0][0];
dp[0][1]=costs[0][1];
dp[0][2]=costs[0][2];
for(int idx=1;idx<costsSize;idx++){
for(int k=0;k<*costsColSize;k++){
if(k==0){
dp[idx][k]=min(dp[idx-1][1],dp[idx-1][2])+costs[idx][k];
}
if(k==1){
dp[idx][k]=min(dp[idx-1][0],dp[idx-1][2])+costs[idx][k];
}
if(k==2){
dp[idx][k]=min(dp[idx-1][0],dp[idx-1][1])+costs[idx][k];
}
}
}
int ans = min(dp[costsSize-1][0],min(dp[costsSize-1][1],dp[costsSize-1][2]));
return ans;
}3. Dynamic programming ( For many colors )
int min(int a,int b){
return a<b?a:b;
}
int minCost(int** costs, int costsSize, int* costsColSize){
int dp[costsSize][*costsColSize];
memset(dp,0,sizeof(dp));
dp[0][0]=costs[0][0];
dp[0][1]=costs[0][1];
dp[0][2]=costs[0][2];
for(int idx=1;idx<costsSize;idx++){
for(int k=0;k<*costsColSize;k++){
int m = 10000000;
for(int t=0;t<*costsColSize;t++){
if(t!=k){
m=min(m,dp[idx-1][t]);
}
}
dp[idx][k]=costs[idx][k]+m;
}
}
int ans = dp[costsSize-1][0];
for(int i=1;i<*costsColSize;i++){
ans = min(ans,dp[costsSize-1][i]);
}
return ans;
}边栏推荐
- Call interface event API common event methods
- [rust daily] published on rust 1.43.0 on April 23, 2020
- LeetCode+ 66 - 70 高精度、二分专题
- Wechat applets - basics takes you to understand the life cycle of applets (I)
- 【C语言】详解 C 语言获取数组长度
- From the beginning of redis learning to take-off, this article is all for you
- 实时数据库 - 笔记
- 饿久了,大脑会进入“省电模式”!感官被削弱,还看不清东西丨爱丁堡大学...
- Freeswitch使用originate转dialplan
- 小小一款代码编辑器竟然也可以有程序运行之功能——Sublime Text3运行各种语言程序的总结
猜你喜欢

FPGA - 7 Series FPGA selectio -08- oserdese2 of advanced logic resources

"Jay bear" plummeted by 96.6%. Why is NFT with star goods cold?

FPGA - 7 Series FPGA selectio -09- io of advanced logic resources_ FIFO

三极管驱动无刷电机

js正则表达式系统讲解(全面的总结)

推荐几款0代码、免费、现学现用的可视化工具

VM332 WAService. js:2 Error: _ vm. Changetabs is not a function

LeetCode+ 51 - 55 回溯、动态规划专题

Floating and positioning

JS regular expression system explanation (comprehensive summary)
随机推荐
Mise en œuvre de l'actionneur asynchrone d'exécution à partir de zéro
MySQL installation steps - Linux configuration file JDK installation (II)
Freeswitch sets the maximum call duration
小小一款代码编辑器竟然也可以有程序运行之功能——Sublime Text3运行各种语言程序的总结
推荐10个好用到爆的Jupyter Notebook插件,让你效率飞起
redis的入门学习到起飞,就这一篇搞定
fpm工具安装
服务器正文18:UDP可靠传输的理解和思考(读云凤博客有感)
Comprehensive analysis of real enterprise software testing process
编译配置in文件
VM332 WAService. js:2 Error: _ vm. Changetabs is not a function
代码没写错,渲染页面不显示原因
Iphone6plus enters the list of antique products netizen: I'm still using it
Techo day Tencent technology open day, June 28 online waiting for you!
Note that JPA uses a custom VO to receive jpql query results
MySQL installation steps - installing MySQL on Linux (3)
Recommend several 0 code, free, learning and using visualization tools
It will cost 700 yuan to realize this issue. Does anyone do it?
Reinforcement learning - grid world
How to open UMD, KMD log and dump diagrams in CAMX architecture