LeetCode 256. Paint House
来源:互联网 发布:股票模拟练习软件 编辑:程序博客网 时间:2024/04/29 14:18
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.
思路:
1. 这代表一类题,遍历所有可能性,且遵循一定的约束条件。最直接的方法是,遍历所有可能性,找出最小cost,但这样的复杂度就是指数增长,o(2^n)。
2. 如何简化复杂度,减少不必要的运算?可以把问题看成是有层次的问题,这样就可以分割成小的问题,小的问题继续分成更小的问题,直到问题不能分割为止。和直接遍历把所有可能性平等的对待相比,这样有层次的方式,是把小问题解决了,大问题的解决就容易了,减少了遍历的可能性!因此,怎么看待或怎么建模这个问题,决定了采用什么方法解决这个问题。
3. 用DP,二维dp。找递推关系,小问题和大问题之间的逻辑联系!
2. 突然有个想法,应该设法总结一下,很多问题用简单粗暴的方法做,复杂度为什么很差?因此,需要借助一些辅助手段来回避这一类问题!
int minCost(vector<vector<int>>& costs) { int n=costs.size(); vector<vector<int>> dp(n+1,vector<int>(3,0));//写完这一行,意识到是否可以不用新建array,直接在给定的2D array上做这个dp呢? for(int i=1;i<n;i++){ vector<int> cur=costs[i-1]; for(int j=0;i<3;j++){ dp[i][j]=dp[i][j]+min(cur[(j+1)%3],cur[(j+2)%3]); } } return min(min(costs.back()[0],costs.back()[1]),costs.back()[2]);}
- LeetCode 256. Paint House
- LeetCode 256. Paint House
- Leetcode 256. Paint House & 265. Paint House II
- [leetcode] 256. Paint House 解题报告
- LeetCode 256. Paint House(房子涂色)
- Leetcode 256. Paint House (Medium) (cpp)
- [Leetcode] 256. Paint House 解题报告
- Leetcode: Paint House
- Leetcode: Paint House II
- [Leetcode]Paint House
- [Leetcode]Paint House II
- *LeetCode-Paint House
- [LeetCode] Paint House
- [leetcode 256] Paint House
- [Leetcode 256]Paint House
- Leetcode Paint House
- leetcode Paint House II
- LeetCode 题解(258) : Paint House
- (二)Nginx-负载均衡
- Zookeeper伪分布式集群环境搭建过程
- VIM Python编译安装
- Node.js的本质
- linux ping域名很慢的其中一个原因
- LeetCode 256. Paint House
- 嵌入式平台,软路由器-OSPF移植总结
- ubuntu配置java开发环境和安装AndroidStudio
- 从http到https简介,tomcat和nginx的https配置,单向认证和双向认证简介,对称加密和非对称加密简介,RSA算法简介
- php应该遵循什么样的编码规范?PHP符合PSR编程规范示例
- Linux命令——ls命令用来显示目录中文件的名字
- 1-Openwrt clone and bulid
- tools:text和android:text
- Java中的关键字