摘要:在原數(shù)組上動規(guī),每一行對應(yīng)一個(gè)房子,每一個(gè)元素代表從第一行的房子到這一行的房子選擇這一種顏色所花的最小開銷。所以每個(gè)元素該元素的值上一行兩個(gè)與該元素不同列元素的值的較小者。不過這次要記錄三個(gè)變量本行最小值,本行第二小值,本行最小值下標(biāo)。
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.
ExampleGiven 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在原數(shù)組上動規(guī),每一行對應(yīng)一個(gè)房子,每一個(gè)元素代表從第一行的房子到這一行的房子選擇這一種顏色所花的最小開銷。所以每個(gè)元素 = 該元素的值 + 上一行兩個(gè)與該元素不同列元素的值的較小者。
動規(guī)到
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.
ExampleGiven 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
ChallengeCould you solve it in O(nk)?
Note和Paint House的思路一樣,依然是在cost數(shù)組上更新最小花銷之和。不過這次要記錄三個(gè)變量:本行最小值,本行第二小值,本行最小值下標(biāo)。這三個(gè)變量每行更新一次,同時(shí)也要存儲上一次循環(huán)的三個(gè)變量。這樣做的目的是當(dāng)前一行最小值和之前一行最小值在同一列時(shí),取之前一行的次小值,也就避免了相鄰兩行取同種顏色的問題。
Solutionpublic 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; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/66190.html
摘要:動態(tài)規(guī)劃復(fù)雜度時(shí)間空間思路直到房子,其最小的涂色開銷是直到房子的最小涂色開銷,加上房子本身的涂色開銷。我們在原數(shù)組上修改,可以做到不用空間。代碼找出最小和次小的,最小的要記錄下標(biāo),方便下一輪判斷 Paint House There are a row of n houses, each house can be painted with one of the three colors...
摘要:題目解答利用的思路,只不過把三種顏色換成了種顏色,所以是如何把它的復(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 w...
摘要:題目解答這類題還是先找臨時(shí)的結(jié)果,由臨時(shí)的結(jié)果最終推出最終解。比如說用存到個(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...
摘要:因?yàn)槿×岁?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...
摘要:代碼如下表示跟前面不一樣顏色,表示跟前面一樣顏色跟前面不一樣顏色的話,在這輪有種可能性跟前面一樣顏色的話,在這輪有種可能性,且前一輪不能與前前一輪一樣顏色因?yàn)檫@個(gè)的解法里,我們只用到變量和,所以我們可以進(jìn)定步把空間復(fù)雜度降為 題目:There is a fence with n posts, each post can be painted with one of the k colo...
閱讀 2232·2021-09-04 16:40
閱讀 1540·2021-08-13 15:07
閱讀 3670·2019-08-30 15:53
閱讀 3269·2019-08-30 13:11
閱讀 1149·2019-08-29 17:22
閱讀 1866·2019-08-29 12:47
閱讀 1540·2019-08-29 11:27
閱讀 2338·2019-08-26 18:42