码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Paint House II

时间:2015-12-01 08:26:19      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

public class Solution {
    public int minCostII(int[][] costs) {
        int n = costs.length;
        if (n == 0) {
            return 0;
        }
        int k = costs[0].length;
        if (k == 0) {
            return 0;
        }
        if (k == 1) {
            return costs[0][0];
        }
        int[] record = new int[k];
        for (int i = 0; i < n; i++) {
            int index = record[0] < record[1] ? 0 : 1;
            int min = Math.min(record[0], record[1]);
            int sec_min = Math.max(record[0], record[1]);
            for (int j = 2; j < k; j++) {
                if (record[j] < min) {
                    index = j;
                    sec_min = min;
                    min = record[j];
                } else if (record[j] < sec_min) {
                    sec_min = record[j];
                }
            }
            for (int j = 0; j < k; j++) {
                if (j == index) {
                    record[j] = sec_min + costs[i][j];
                } else {
                    record[j] = min + costs[i][j];
                }
            }
        }
        int result = Integer.MAX_VALUE;
        for (int num : record) {
            result = Math.min(result, num);
        }
        return result;
    }
}

 

[LeetCode]Paint House II

标签:

原文地址:http://www.cnblogs.com/vision-love-programming/p/5009009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!