LeetCode 265: Paint House II

Posted keepshuatishuati

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 265: Paint House II相关的知识,希望对你有一定的参考价值。

class Solution {
    public int minCostII(int[][] costs) {
        if (costs.length == 0) {
            return 0;
        }
        
        int prevMin = 0;
        int prevSecMin = 0;
        int prevMinIndex = -1;
        for (int i = 0; i < costs.length; i++) {
            int min = Integer.MAX_VALUE;
            int secMin = Integer.MAX_VALUE;
            int minIndex = -1;
            for (int j = 0; j < costs[i].length; j++) {
                int value = costs[i][j] + (j == prevMinIndex ? prevSecMin : prevMin);
                if (minIndex == -1 || value < min) {
                    secMin = min;
                    min = value;
                    minIndex = j;
                } else if (value < secMin) {
                    secMin = value;
                }
            }
            
            prevMin = min;
            prevSecMin = secMin;
            prevMinIndex = minIndex;
        }
        
        return prevMin;
    }
}

 

以上是关于LeetCode 265: Paint House II的主要内容,如果未能解决你的问题,请参考以下文章

265. Paint House II

265. Paint House II

265. Paint House II

java 265. Paint House II(O(n * k * k))。java

java 265. Paint House II(O(n * k * k))。java

java 265. Paint House II(O(n * k * k))。java