当前位置:网站首页>剑指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;
}边栏推荐
- 4. use MySQL shell to install and deploy Mgr clusters | explain Mgr in simple terms
- D3D11_ Chili_ Tutorial (3): design a bindable/drawable system
- @RequestParam
- 普歌 -- getOrDefault()方法理解
- 整型提昇和大小端字節序
- 【星海出品】 运维巡检合集
- C language tutorial
- Linux MySQL implements root user login without password
- Freeswitch uses origin to dialplan
- 华为云计算之物理节点CNA安装教程
猜你喜欢

Students who do not understand the code can also send their own token. The current universal dividend model can be divided into BSC and any generation B

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

Linked list (III) - reverse linked list

Freeswitch sets the maximum call duration

Floating and positioning

My MVVM open source project "travel epidemic prevention app" has been released

Paper recommendation: efficientnetv2 - get smaller models and faster training speed through NAS, scaling and fused mbconv

【网络教程】IPtables官方教程--学习笔记1
![[C#][转载]furion框架地址和教程地址](/img/b2/e1c30153c4237188b60e9523b0a5d8.png)
[C#][转载]furion框架地址和教程地址

整型提升和大小端字节序
随机推荐
饿久了,大脑会进入“省电模式”!感官被削弱,还看不清东西丨爱丁堡大学...
Eyebeam advanced settings
【Paper Reading-3D Detection】Fully Convolutional One-Stage 3D Object Detection on LiDAR Range Images
Recommend 10 popular jupyter notebook plug-ins to make you fly efficiently
Last 29 days
[rust daily] May 24, 2020 rush, rocket, Mun, caspin
2 startup, interrupt and system call
Reinforcement learning - grid world
最后的二十九天
JS regular expression system explanation (comprehensive summary)
[staff] arpeggio mark
Causes of wechat applet compilation page blank bug
Singleton singleton mode
CRC32概述以及实现和使用
VM332 WAService. js:2 Error: _ vm. Changetabs is not a function
Freeswitch使用originate转dialplan
浮动与定位
调接口事件API常用事件方法
Wechat applets - basics takes you to understand the life cycle of applets (I)
Extern "C" overview