国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

265. Paint House II

mylxsw / 3380人閱讀

摘要:題目解答利用的思路,只不過(guò)把三種顏色換成了種顏色,所以是如何把它的復(fù)雜度降到那么就是如果將顏色的部分只掃一遍。參考的里只需要記錄下每個(gè)的最小的兩個(gè)顏色。

題目:
There are a row of n houses, each house can be painted with one of the k colors. 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 k cost matrix. For example, costs0 is the cost of painting house 0 with color 0; costs1 is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.

Note:
All costs are positive integers.

Follow up:
Could you solve it in O(nk) runtime?

解答:
利用paint house的思路,只不過(guò)把三種顏色換成了k種顏色,所以:

//State: f[i][j] is the minimum cost of painting i houses with color j
    //Function: f[i][j] = Math.min(f[i - 1][k(except j)]) + costs[i][j];
    //Initialize: f[0][j] = costs[0][j];
    //Result: Math.min(f[costs.length - 1][j]);
    public int minCostII(int[][] costs) {
        if (costs == null || costs.length == 0 || costs[0].length == 0) {
            return 0;
        }
        int house = costs.length, color = costs[0].length;
        int[][] f = new int[house][color];
        for (int i = 0; i < color; i++) {
            f[0][i] = costs[0][i];
        }
        
        for (int i = 1; i < house; i++) {
            for (int j = 0; j < color; j++) {
                f[i][j] = Integer.MAX_VALUE;
                for (int k = 0; k < color; k++) {
                    if (k == j) continue;
                    f[i][j] = Math.min(f[i][j], f[i - 1][k] + costs[i][j]);
                }
            }
        }
        
        int result = Integer.MAX_VALUE;
        for (int i = 0; i < color; i++) {
            result = Math.min(result, f[house - 1][i]);
        }
        
        return result;

followup是如何把它的復(fù)雜度降到o(nk), 那么就是如果將顏色的部分只掃一遍。參考leetcode的discuss里most voted answer, 只需要記錄下每個(gè)house的最小的兩個(gè)顏色。如果下一個(gè)顏色跟這個(gè)顏色不一樣,就取最小的這個(gè)顏色加上這次所選的顏色,并找出最小值;如果下一個(gè)顏色跟這個(gè)顏色一樣,那么我們不可以取最小的這個(gè)顏色,所以我們?nèi)〉诙〉念伾由线@次所選的顏色。最后把最小的顏色輸出就可以了。

//Only the first two minimum costs count, so we keep track on min1 and min2 for each house
    public int minCostII(int[][] costs) {
        if (costs == null || costs.length == 0 || costs[0].length == 0) {
            return 0;
        }
        
        int house = costs.length, color = costs[0].length;
        
        int min1 = -1, min2 = -1;
        for (int i = 0; i < house; i++) {
            int last1 = min1, last2 = min2;
            min1 = -1; min2 = -1;
            
            for (int j = 0; j < color; j++) {
                if (j != last1) {
                    costs[i][j] += last1 < 0 ? 0 : costs[i - 1][last1];
                } else {
                    costs[i][j] += last2 < 0 ? 0 : costs[i - 1][last2];
                }
                
                if (min1 < 0 || costs[i][j] < costs[i][min1]) {
                    min2 = min1;
                    min1 = j;
                } else if (min2 < 0 || costs[i][j] < costs[i][min2]) {
                    min2 = j;
                }
            }
        }
        
        return costs[house - 1][min1];
    }

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/64829.html

相關(guān)文章

  • [Leetcode] Paint House 房子涂色

    摘要:動(dòng)態(tài)規(guī)劃復(fù)雜度時(shí)間空間思路直到房子,其最小的涂色開(kāi)銷是直到房子的最小涂色開(kāi)銷,加上房子本身的涂色開(kāi)銷。我們?cè)谠瓟?shù)組上修改,可以做到不用空間。代碼找出最小和次小的,最小的要記錄下標(biāo),方便下一輪判斷 Paint House There are a row of n houses, each house can be painted with one of the three colors...

    SunZhaopeng 評(píng)論0 收藏0
  • [LintCode/LeetCode] Paint House I & Paint Hous

    摘要:在原數(shù)組上動(dòng)規(guī),每一行對(duì)應(yīng)一個(gè)房子,每一個(gè)元素代表從第一行的房子到這一行的房子選擇這一種顏色所花的最小開(kāi)銷。所以每個(gè)元素該元素的值上一行兩個(gè)與該元素不同列元素的值的較小者。不過(guò)這次要記錄三個(gè)變量本行最小值,本行第二小值,本行最小值下標(biāo)。 Paint House Problem There are a row of n houses, each house can be painted ...

    ChristmasBoy 評(píng)論0 收藏0
  • 256. Paint House

    摘要:題目解答這類題還是先找臨時(shí)的結(jié)果,由臨時(shí)的結(jié)果最終推出最終解。比如說(shuō)用存到個(gè)的時(shí)候最小的但是到第個(gè)的時(shí)候,有三種情況涂當(dāng)我涂紅的時(shí)候,前面一個(gè)只能涂藍(lán)或者綠,所以我只能加上這兩種情況的最小值,作為此次計(jì)算的最小值,以此類推。 題目:here are a row of n houses, each house can be painted with one of the three co...

    Miracle 評(píng)論0 收藏0
  • [LintCode/LeetCode] House Robber II

    摘要:因?yàn)槿×岁?duì)首就不能取隊(duì)尾,所以對(duì)數(shù)組循環(huán)兩次,一個(gè)從取到,一個(gè)從取到,比較兩次循環(huán)后隊(duì)尾元素,取較大者。注意,要先討論當(dāng)原數(shù)組位數(shù)小于時(shí)的情況。 Problem After robbing those houses on that street, the thief has found himself a new place for his thievery so that he wi...

    OnlyLing 評(píng)論0 收藏0
  • [LeetCode] House Robber I II

    摘要:注意對(duì)邊界條件的判斷,是否非空,是否長(zhǎng)度為 House Robber I Problem You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping y...

    qpal 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<