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

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] Paint House I & Paint Hous

ChristmasBoy / 844人閱讀

摘要:在原數組上動規,每一行對應一個房子,每一個元素代表從第一行的房子到這一行的房子選擇這一種顏色所花的最小開銷。所以每個元素該元素的值上一行兩個與該元素不同列元素的值的較小者。不過這次要記錄三個變量本行最小值,本行第二小值,本行最小值下標。

Paint House Problem

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, costs0 is the cost of painting house 0 with color red; costs1 is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.

Example

Given costs = [[14,2,11],[11,14,5],[14,3,10]] return 10

house 0 is blue, house 1 is green, house 2 is blue, 2 + 5 + 3 = 10

Note

在原數組上動規,每一行對應一個房子,每一個元素代表從第一行的房子到這一行的房子選擇這一種顏色所花的最小開銷。所以每個元素 = 該元素的值 + 上一行兩個與該元素不同列元素的值的較小者。
動規到

Solution
public class Solution {
    public int minCost(int[][] cost) {
        if (cost == null || cost.length == 0) return 0;
        int n = cost.length - 1;
        for (int i = 1; i <= n; i++) {
            cost[i][0] += Math.min(cost[i-1][1], cost[i-1][2]);
            cost[i][1] += Math.min(cost[i-1][0], cost[i-1][2]);
            cost[i][2] += Math.min(cost[i-1][0], cost[i-1][1]);
        }
        return Math.min(cost[n][0], Math.min(cost[n][1], cost[n][2]));
    }
}
Paint House II Problem

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.

Example

Given n = 3, k = 3, costs = [[14,2,11],[11,14,5],[14,3,10]] return 10

house 0 is color 2, house 1 is color 3, house 2 is color 2, 2 + 5 + 3 = 10

Challenge

Could you solve it in O(nk)?

Note

和Paint House的思路一樣,依然是在cost數組上更新最小花銷之和。不過這次要記錄三個變量:本行最小值,本行第二小值,本行最小值下標。這三個變量每行更新一次,同時也要存儲上一次循環的三個變量。這樣做的目的是當前一行最小值和之前一行最小值在同一列時,取之前一行的次小值,也就避免了相鄰兩行取同種顏色的問題。

Solution
public class Solution {
    public int minCostII(int[][] cost) {
        if (cost == null || cost.length == 0) return 0;
        int preMin = 0, preSec = 0, preIndex = -1;
        int n = cost.length, k = cost[0].length;
        for (int i = 0; i < n; i++) {
            int curMin = Integer.MAX_VALUE, curSec = Integer.MAX_VALUE, curIndex = 0;
            for (int j = 0; j < k; j++) {
                cost[i][j] += preIndex == j ? preSec : preMin;
                if (cost[i][j] < curMin) {
                    curSec = curMin;
                    curMin = cost[i][j];
                    curIndex = j;
                }
                else if (cost[i][j] < curSec) {
                    curSec = cost[i][j];
                }
            }
            preMin = curMin;
            preSec = curSec;
            preIndex = curIndex;
        }
        return preMin;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/66190.html

相關文章

  • [Leetcode] Paint House 房子涂色

    摘要:動態規劃復雜度時間空間思路直到房子,其最小的涂色開銷是直到房子的最小涂色開銷,加上房子本身的涂色開銷。我們在原數組上修改,可以做到不用空間。代碼找出最小和次小的,最小的要記錄下標,方便下一輪判斷 Paint House There are a row of n houses, each house can be painted with one of the three colors...

    SunZhaopeng 評論0 收藏0
  • 265. Paint House II

    摘要:題目解答利用的思路,只不過把三種顏色換成了種顏色,所以是如何把它的復雜度降到那么就是如果將顏色的部分只掃一遍。參考的里只需要記錄下每個的最小的兩個顏色。 題目:There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house w...

    mylxsw 評論0 收藏0
  • 256. Paint House

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

    Miracle 評論0 收藏0
  • [LintCode/LeetCode] House Robber II

    摘要:因為取了隊首就不能取隊尾,所以對數組循環兩次,一個從取到,一個從取到,比較兩次循環后隊尾元素,取較大者。注意,要先討論當原數組位數小于時的情況。 Problem After robbing those houses on that street, the thief has found himself a new place for his thievery so that he wi...

    OnlyLing 評論0 收藏0
  • 276. Paint Fence

    摘要:代碼如下表示跟前面不一樣顏色,表示跟前面一樣顏色跟前面不一樣顏色的話,在這輪有種可能性跟前面一樣顏色的話,在這輪有種可能性,且前一輪不能與前前一輪一樣顏色因為這個的解法里,我們只用到變量和,所以我們可以進定步把空間復雜度降為 題目:There is a fence with n posts, each post can be painted with one of the k colo...

    zhjx922 評論0 收藏0

發表評論

0條評論

ChristmasBoy

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<