Paint House II
来源:互联网 发布:网络舆情分析师报名 编辑:程序博客网 时间:2024/05/21 00:16
参考:点击打开链接
第一层for是对所有房子的遍历,第二层则是对某一个房子所有颜色的遍历。所以如果,第二个房子看到此时颜色与第一个房子的颜色相同(最小的cost),那就选择另一颜色(次小的)。每个房子都要找到自身和上一个房子最小的cost和次小的cost。
理解:
if (prevIdx == k) { costs[i][k] = costs[i][k] + prevSec; } else { costs[i][k] = costs[i][k] + prevMin; }
public class Solution { public int minCostII(int[][] costs) { if (costs == null) { throw new IllegalArgumentException("haha"); } if (costs.length == 0 || costs[0].length == 0) { return 0; } int prevMin = 0, prevSec = 0, prevIdx = -1; for (int i = 0; i < costs.length; i++) { int curMin = Integer.MAX_VALUE, curSec = Integer.MAX_VALUE, curIdx = -1; for (int k = 0; k < costs[0].length; k++) { if (prevIdx == k) { costs[i][k] = costs[i][k] + prevSec; } else { costs[i][k] = costs[i][k] + prevMin; } if (costs[i][k] < curMin) { curSec = curMin; curMin = costs[i][k]; curIdx = k; } else if (costs[i][k] < curSec) { curSec = costs[i][k]; } } prevMin = curMin; prevSec = curSec; prevIdx = curIdx; } return prevMin; }}
0 0
- Leetcode: Paint House II
- [Leetcode]Paint House II
- [LeetCode265]Paint House II
- LintCode: Paint House II
- Paint House II
- LintCode_516 Paint House II
- leetcode Paint House II
- Paint House II
- [LintCode]Paint House II
- 265. Paint House II
- [Leetcode 265]Paint House II
- LeetCode 265. Paint House II
- Leetcode 265. Paint House II
- leetcode 265. Paint House II
- Leetcode 256. Paint House & 265. Paint House II
- LeetCode 题解(260) : Paint House II
- [leetcode] 265. Paint House II 解题报告
- [Leetcode] 265. Paint House II 解题报告
- 测试
- ln 用法,知识(软链接和硬链接)
- HTML 代码复用——gulp-file-include
- struts2 excel导入
- java中怎样将JSON字符串还原成MAP
- Paint House II
- Mysql my.cnf 优化
- CollapsingToolbarLayout用法总结
- JAVA读取网页里超链接资源到本地
- suite0001关于路由器
- ZQUOJ1951Fibonacci数列的快速幂算法
- SQL []; ORA-01843: 无效的月份
- Android无源码调试APK(使用ApkTool和Eclipse ADT)
- 天才小毒妃 > 第917章 深藏不露大财主