当前位置:网站首页>剑指offer II 091.粉刷房子
剑指offer II 091.粉刷房子
2022-06-28 06:55:00 【Base-Case】
假如有一排房子,共 n 个,每个房子可以被粉刷成红色、蓝色或者绿色这三种颜色中的一种,你需要粉刷所有的房子并且使其相邻的两个房子颜色不能相同。
当然,因为市场上不同颜色油漆的价格不同,所以房子粉刷成不同颜色的花费成本也是不同的。每个房子粉刷成不同颜色的花费是以一个 n x 3 的正整数矩阵 costs 来表示的。
例如,costs[0][0] 表示第 0 号房子粉刷成红色的成本花费;costs[1][2] 表示第 1 号房子粉刷成绿色的花费,以此类推。
请计算出粉刷完所有房子最少的花费成本。
示例 1:
输入: costs = [[17,2,17],[16,16,5],[14,3,19]]
输出: 10
解释: 将 0 号房子粉刷成蓝色,1 号房子粉刷成绿色,2 号房子粉刷成蓝色。
最少花费: 2 + 5 + 3 = 10。
示例 2:
输入: costs = [[7,6,2]]
输出: 2
提示:
costs.length == n
costs[i].length == 3
1 <= n <= 100
1 <= costs[i][j] <= 20
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/JEj789
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
1.暴力递归(超时)
int min(int a,int b){
return a<b?a:b;
}
//k为上一个粉刷的房子是几号
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.动态规划(
执行用时:4 ms, 在所有 C 提交中击败了98.11%的用户
内存消耗:5.9 MB, 在所有 C 提交中击败了100.00%的用户
)
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.动态规划(针对很多种颜色)
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;
}边栏推荐
- Freeswitch sets the maximum call duration
- VM332 WAService. js:2 Error: _ vm. Changetabs is not a function
- 编译原理期末复习
- 图片按日期批量导入WPS表格
- AttributeError: 'callable_iterator' object has no attribute 'next'
- JDBC learning (I) -- implementing simple CRUD operations
- 华为云计算之物理节点CNA安装教程
- js正则表达式系统讲解(全面的总结)
- 炒股开户在手机上安全吗?
- fpm工具安装
猜你喜欢

三极管驱动无刷电机
![[online tutorial] official iptables tutorial -- learning notes 1](/img/b9/8f94caa46eb46dab581c713494f36d.png)
[online tutorial] official iptables tutorial -- learning notes 1

Techo day Tencent technology open day, June 28 online waiting for you!

选拔赛题目代码

FPGA - 7 Series FPGA selectio -07- iserdese2 of advanced logic resources

饿久了,大脑会进入“省电模式”!感官被削弱,还看不清东西丨爱丁堡大学...

编译原理期末复习

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

Some habits of it veterans in the workplace

D3D11_ Chili_ Tutorial (3): design a bindable/drawable system
随机推荐
ImportError: cannot import name 'ensure_ dir_ Possible solutions for exists'
实现这个 issue 得700块钱人民币,有人做嘛?
【Paper Reading-3D Detection】Fully Convolutional One-Stage 3D Object Detection on LiDAR Range Images
5-minute NLP: summary of time chronology from bag of words to transformer
Compilation principles final review
Create a gson object that formats the time zone. JSON parsing time formatting zoneddatetime
强化学习——格子世界
Server body 18: understanding and thinking of UDP reliable transmission (feeling from reading Yunfeng blog)
金山云团队分享 | 5000字读懂Presto如何与Alluxio搭配
Promotion intégrale et ordre des octets de fin de taille
FPGA - 7系列 FPGA SelectIO -07- 高级逻辑资源之ISERDESE2
JS regular expression system explanation (comprehensive summary)
Mise en œuvre de l'actionneur asynchrone d'exécution à partir de zéro
CMAKE小知识
Iframe switching in Web Automation
《微信小程序-基础篇》带你了解小程序中的生命周期(一)
ImportError: cannot import name 'ensure_dir_exists'的可解决办法
什么是一致性哈希?可以应用在哪些场景?
4~20mA输入/0~5V输出的I/V转换电路
The code is correct, and the rendering page does not display the reason