当前位置:网站首页>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;
}边栏推荐
猜你喜欢

Rn7302 three-phase electric quantity detection (based on STM32 single chip microcomputer)

Recommend 10 popular jupyter notebook plug-ins to make you fly efficiently

「杰伦熊」暴跌96.6% 明星带货NFT为何遇冷?

Libuv framework echo server C source code explanation (TCP part)

BACnet/IP網關如何采集樓宇集中控制系統數據

How bacnet/ip gateway collects data of building centralized control system

Wechat applets - basics takes you to understand the life cycle of applets (I)
![[c #] [reprint]furion frame address and tutorial address](/img/b2/e1c30153c4237188b60e9523b0a5d8.png)
[c #] [reprint]furion frame address and tutorial address

RN7302三相电量检测(基于STM32单片机)

YOLOv5增加小目标检测层
随机推荐
全方位透析真实企业软件测试流程
Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known
Techo Day 腾讯技术开放日,6月28日线上等你!
@RequestParam
This keyword details
Libuv framework echo server C source code explanation (TCP part)
Call interface event API common event methods
小程序页面设置100%高度还是留白怎么办?
【C语言】详解 C 语言获取数组长度
华为云计算之物理节点CNA安装教程
Recommend several 0 code, free, learning and using visualization tools
From the beginning of redis learning to take-off, this article is all for you
[C#][转载]furion框架地址和教程地址
【星海出品】 运维巡检合集
7-2 芬兰木棋 结构体排序
Compilation principles final review
[interval DP] stone consolidation
普歌 -- 单例模式
实现这个 issue 得700块钱人民币,有人做嘛?
What if the applet page is set to 100% height or left blank?