[LC] 256. Paint House
There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.
The cost of painting each house with a certain color is represented by a n x 3 cost matrix. For example, costs[0][0] is the cost of painting house 0 with color red; costs[1][2] is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.
Note:
All costs are positive integers.
Example:
Input: [[17,2,17],[16,16,5],[14,3,19]]
Output: 10
Explanation: Paint house 0 into blue, paint house 1 into green, paint house 2 into blue.
Minimum cost: 2 + 5 + 3 = 10.
class Solution {
public int minCost(int[][] costs) {
if (costs == null || costs.length == 0 || costs[0].length == 0) {
return 0;
}
int curRed = costs[0][0];
int curBlue = costs[0][1];
int curGreen = costs[0][2];
int prevRed = curRed;
int prevBlue = curBlue;
int prevGreen = curGreen;
for (int i = 1; i < costs.length; i++) {
curRed = Math.min(prevBlue, prevGreen) + costs[i][0];
curBlue = Math.min(prevRed, prevGreen) + costs[i][1];
curGreen = Math.min(prevRed, prevBlue) + costs[i][2];
prevRed = curRed;
prevBlue = curBlue;
prevGreen = curGreen;
}
return Math.min(curRed, Math.min(curBlue, curGreen));
}
}
[LC] 256. Paint House的更多相关文章
- leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...
- 256. Paint House房屋染色
[抄题]: There are a row of n houses, each house can be painted with one of the three colors: red, blue ...
- 256. Paint House
题目: There are a row of n houses, each house can be painted with one of the three colors: red, blue o ...
- [LeetCode#256] Paint House
Problem: There are a row of n houses, each house can be painted with one of the three colors: red, b ...
- [leetcode]256. Paint House粉刷房子(三色可选)
There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...
- [LeetCode] 256. Paint House_Easy tag: Dynamic Programming
There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...
- [LeetCode] 256. Paint House 粉刷房子
There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...
- LC 265. Paint House II
There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...
- 【LeetCode】256. Paint House 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
随机推荐
- tensorflow笔记(北大网课实战)
1. tf.multiply(x,y1) # 对应元素相乘 tf.matmul(x,y2) # 矩阵相乘 2.会话:执行计算图中的节点运算的. with tf.Session() as sess: p ...
- Dijkstra与Floyd算法
1. Dijkstra算法 1.1 定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点 ...
- Maven--反应堆(Reactor)
在一个多模块的 Maven 项目中,反应堆是指所有模块组成的一个构建结构.对于单模块的项目,反应堆就是该模块本身.但对于多模块项目来说,反应堆就包含了各模块之间继承与依赖的关系,从而能够自动计算出合理 ...
- ERNIE:知识图谱结合BERT才是「有文化」的语言模型
自然语言表征模型最近受到非常多的关注,很多研究者将其视为 NLP 最重要的研究方向之一.例如在大规模语料库上预训练的 BERT,它可以从纯文本中很好地捕捉丰富的语义模式,经过微调后可以持续改善不同 N ...
- Python说文解字_计数器
from collections import Counter response = [ "vanilla", "chocolate", "vanil ...
- JQuery与JS比较
备注:原文转自脚本之家,原文地址:http://www.jb51.net/article/39494.htm 转载仅为方便统一整理收藏内容,别无他意 JQuery与JS的比较: jquery ...
- InnoDB和MyISAM区别总结
原来是MyISAM类型不支持事务处理等高级处理,而InnoDB类型支持. MyISAM类型的表强调的是性能,其执行数度比InnoDB类型更快,但是不提供事务支持,而InnoDB提供事务支持已经外部键等 ...
- 三阶平面魔方(BFS)
有一个 3×3 的平面魔方,在平面魔方中,每个格子里分别无重复地写上 1 - 9 这 9 个数字.一共有 4 种对平面魔方的操作: 选择某一行左移. 选择某一行右移. 选择某一列上移. 选择某一列下 ...
- if_while
import random secret=random.randint(1,10) tmp=input("请输入一个数") guess=int(tmp) while guess!= ...
- long型长整数字在前端页面显示异常及其解决方法
文章目录 1.引子 2.解决问题 (1)初试EL表达式取long型数值 (2)再探EL表达式取字符串格式long型数值 (3)最后一试---给EL表达式加引号 3.总结 1.引子 在做项目中,发现了一 ...