Leetcode - Paint House

My code:

public class Solution {
    public int minCost(int[][] costs) {
        if (costs == null || costs.length == 0 || costs[0].length != 3) {
            return 0;
        }
        
        int[][] cache = new int[costs.length][costs[0].length];
        int min = Integer.MAX_VALUE;
        min = Math.min(min, helper(0, 0, costs, cache));
        min = Math.min(min, helper(0, 1, costs, cache));
        min = Math.min(min, helper(0, 2, costs, cache));
        return min;
    }
    
    
    private int helper(int index, int currColor, int[][] costs, int[][] cache) {
        if (index >= costs.length) {
            return 0;
        }
        int cost = 0;
        if (cache[index][currColor] != 0) {
            return cache[index][currColor];
        }
        cost += costs[index][currColor];
        int min = Integer.MAX_VALUE;
        if (currColor != 0) {
            min = Math.min(min, helper(index + 1, 0, costs, cache));
        }
        if (currColor != 1) {
            min = Math.min(min, helper(index + 1, 1, costs, cache));
        }
        if (currColor != 2) {
            min = Math.min(min, helper(index + 1, 2, costs, cache));
        }
        cache[index][currColor] = cost + min;
        return cost + min;
    }
    
    public static void main(String[] args) {
        Solution test = new Solution();
        int[][] costs = new int[][]{{20, 18, 4}, {9, 9, 10}};
        int ret = test.minCost(costs);
        System.out.println(ret);
    }
}

看到這道題目,我第一個(gè)想到的是
** backtracking + memory cache **

然后寫了出來坏瞄。
速度還是比較慢的。

看了答案,原來有 Greedy 的做法.

My code:

public class Solution {
    public int minCost(int[][] costs) {
        if (costs == null || costs.length == 0 || costs[0].length != 3) {
            return 0;
        }
        
        int lastRed = costs[0][0];
        int lastBlue = costs[0][1];
        int lastGreen = costs[0][2];
        
        for (int i = 1; i < costs.length; i++) {
            int currRed = Math.min(lastBlue, lastGreen) + costs[i][0];
            int currBlue = Math.min(lastRed, lastGreen) + costs[i][1];
            int currGreen = Math.min(lastRed, lastBlue) + costs[i][2];
            
            lastRed = currRed;
            lastBlue = currBlue;
            lastGreen = currGreen;
        }
        
        return Math.min(lastRed, Math.min(lastBlue, lastGreen));
    }
    
    public static void main(String[] args) {
        Solution test = new Solution();
        int[][] costs = new int[][]{{20, 18, 4}, {9, 9, 10}};
        int ret = test.minCost(costs);
        System.out.println(ret);
    }
}

reference:
https://discuss.leetcode.com/topic/32408/share-my-very-simple-java-solution-with-explanation

lastRed 表示上一層涂紅色所造成的最小開銷
球匕。。帖烘。

然后一層層往下遞推亮曹,只需要最小的開銷。
時(shí)間復(fù)雜度 O(3 * N)
而我backtracking 的時(shí)間復(fù)雜是: O(M* N), 雖然有cache使得速度變快秘症,但無法改變他的時(shí)間復(fù)雜度照卦。

Anyway, Good luck, Richardo! -- 09/20/2016

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市历极,隨后出現(xiàn)的幾起案子窄瘟,更是在濱河造成了極大的恐慌,老刑警劉巖趟卸,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蹄葱,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡锄列,警方通過查閱死者的電腦和手機(jī)图云,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來邻邮,“玉大人竣况,你說我怎么就攤上這事⊥惭希” “怎么了丹泉?”我有些...
    開封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵情萤,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我摹恨,道長(zhǎng)筋岛,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任晒哄,我火速辦了婚禮睁宰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘寝凌。我一直安慰自己柒傻,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開白布较木。 她就那樣靜靜地躺著红符,像睡著了一般。 火紅的嫁衣襯著肌膚如雪劫映。 梳的紋絲不亂的頭發(fā)上违孝,一...
    開封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音泳赋,去河邊找鬼雌桑。 笑死,一個(gè)胖子當(dāng)著我的面吹牛祖今,可吹牛的內(nèi)容都是我干的校坑。 我是一名探鬼主播,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼千诬,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼耍目!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起徐绑,我...
    開封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤邪驮,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后傲茄,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體毅访,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年盘榨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了喻粹。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡草巡,死狀恐怖守呜,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤查乒,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布弥喉,位于F島的核電站,受9級(jí)特大地震影響侣颂,放射性物質(zhì)發(fā)生泄漏档桃。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一憔晒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蔑舞,春花似錦拒担、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至钧栖,卻和暖如春低零,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背拯杠。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工掏婶, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人潭陪。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓雄妥,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親依溯。 傳聞我的和親對(duì)象是個(gè)殘疾皇子老厌,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容