摘要:題目解答這類題還是先找臨時(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 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.
Note:
All costs are positive integers.
解答:
這類題還是先找臨時(shí)的結(jié)果,由臨時(shí)的結(jié)果最終推出最終解。比如說用f(i, j)存到i個(gè)house的時(shí)候最小的cost.但是到第i個(gè)house的時(shí)候,有三種情況:涂red, blue or green.當(dāng)我涂紅的時(shí)候,前面一個(gè)只能涂藍(lán)或者綠,所以我只能加上這兩種情況的最小值,作為此次計(jì)算的最小值,以此類推。
public class Solution { //State: f[i][j] is the minimum cost of painting i houses with color j -> (red, blue, green) //Function: f[i][0] = Math.min(f[i - 1][1], f[i - 1][2]) + costs[i][0] // f[i][1] = Math.min(f[i - 1][0], f[i - 1][2]) + costs[i][1] // f[i][2] = Math.min(f[i - 1][0], f[i - 1][1]) + costs[i][2] //Initialize: f[0][0] = costs[0][0], f[0][1] = costs[0][1], f[0][2] = costs[0][2]; //Result: Math.min(f[costs.length - 1][0], f[costs.length - 1][1], f[costs.length - 1][2]); public int minCost(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]; //Initialize f[0][0] = costs[0][0]; f[0][1] = costs[0][1]; f[0][2] = costs[0][2]; //Function for (int i = 1; i < house; i++) { f[i][0] = Math.min(f[i - 1][1], f[i - 1][2]) + costs[i][0]; f[i][1] = Math.min(f[i - 1][0], f[i - 1][2]) + costs[i][1]; f[i][2] = Math.min(f[i - 1][0], f[i - 1][1]) + costs[i][2]; } return Math.min(f[house - 1][0], Math.min(f[house - 1][1], f[house - 1][2])); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/64830.html
摘要:在原數(shù)組上動(dòng)規(guī),每一行對(duì)應(yīng)一個(gè)房子,每一個(gè)元素代表從第一行的房子到這一行的房子選擇這一種顏色所花的最小開銷。所以每個(gè)元素該元素的值上一行兩個(gè)與該元素不同列元素的值的較小者。不過這次要記錄三個(gè)變量本行最小值,本行第二小值,本行最小值下標(biāo)。 Paint House Problem There are a row of n houses, each house can be painted ...
摘要:動(dòng)態(tài)規(guī)劃復(fù)雜度時(shí)間空間思路直到房子,其最小的涂色開銷是直到房子的最小涂色開銷,加上房子本身的涂色開銷。我們?cè)谠瓟?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...
摘要:代碼如下表示跟前面不一樣顏色,表示跟前面一樣顏色跟前面不一樣顏色的話,在這輪有種可能性跟前面一樣顏色的話,在這輪有種可能性,且前一輪不能與前前一輪一樣顏色因?yàn)檫@個(gè)的解法里,我們只用到變量和,所以我們可以進(jìn)定步把空間復(fù)雜度降為 題目:There is a fence with n posts, each post can be painted with one of the k colo...
摘要:簡(jiǎn)介是一個(gè)高性能的數(shù)據(jù)庫(kù),把數(shù)據(jù)存在內(nèi)存中,并在磁盤中記錄數(shù)據(jù)的變化。因?yàn)閷?shù)據(jù)存在內(nèi)存中,所以數(shù)據(jù)操作非??臁T谥惺褂檬紫?,安裝驅(qū)動(dòng)支持多種數(shù)據(jù)類型,常用的有鍵值對(duì),哈希表,鏈表,集合等。鍵值對(duì)運(yùn)行,結(jié)果如下哈希表哈希表有點(diǎn)類似中的。 Redis簡(jiǎn)介 Redis是一個(gè)高性能的key-value數(shù)據(jù)庫(kù),Redis把數(shù)據(jù)存在內(nèi)存中,并在磁盤中記錄數(shù)據(jù)的變化。因?yàn)閷?shù)據(jù)存在內(nèi)存中,所以數(shù)據(jù)...
閱讀 3052·2021-11-24 09:39
閱讀 3692·2021-11-22 13:54
閱讀 3475·2021-11-16 11:45
閱讀 2549·2021-09-09 09:33
閱讀 3273·2019-08-30 15:55
閱讀 1354·2019-08-29 15:40
閱讀 991·2019-08-29 15:19
閱讀 3483·2019-08-29 15:14