Leetcode - Triangle

Paste_Image.png

My code:

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        if (triangle == null || triangle.size() == 0)
            return 0;
        if (triangle.size() == 1)
            return triangle.get(0).get(0);
        
        int[] currDis = new int[triangle.size()];
        currDis[0] = triangle.get(0).get(0);
        for (int i = 1; i < triangle.size(); i++) {
            currDis[i] = currDis[i - 1] + triangle.get(i).get(i);
            for (int j = i - 1; j > 0; j--)
                currDis[j] = Math.min(currDis[j - 1], currDis[j]) + triangle.get(i).get(j);
            currDis[0] = currDis[0] + triangle.get(i).get(0);
        }
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < currDis.length; i++) {
            if (min > currDis[i])
                min = currDis[i];
        }
        return min;
    }
    
    public static void main(String[] args) {
        Solution test = new Solution();
        ArrayList<Integer> a0 = new ArrayList<Integer>();
        ArrayList<Integer> a1 = new ArrayList<Integer>();
        ArrayList<Integer> a2 = new ArrayList<Integer>();
        a0.add(-1);
        a1.add(2);
        a1.add(3);
        a2.add(1);
        a2.add(-1);
        a2.add(-3);
        ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();
        result.add(a0);
        result.add(a1);
        result.add(a2);
        System.out.println(test.minimumTotal(result));
    }
}

My test result:

Paste_Image.png

這次題目一開始想偏了,覺得應該用最短路徑算法來做。后來發現沒有這么復雜。
首先,到每一層的最短距離,通過簡單的算法,我們是無法確定的。
但是,我們可以確定的是,到每一個結點的最短距離。這個是很容易確定的。類似于上面那道題目,設置一個數組,不斷刷新就行。
然后出現一個問題。該如何遍歷上一層呢?
我一開始設置了兩個數組,pastDis[] and currDis[]
分別用來記錄上一層和這一層的數據,然后不斷刷新。
后來查看代碼發現不需要這么復雜,直接一個數組就可以完工。并且可以解決因為覆蓋原數據導致的錯誤問題。方法就是從右往左遍歷。然后可以避免使用了已被覆蓋的數據。
差不多就這樣啦。

**
總結: Array
**

Anyway, Good luck, Richardo!

My code:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        if (triangle == null || triangle.size() == 0)
            return 0;
        int[] ret = new int[triangle.size()]; // record data
        /** scan from right to left, store minimum value to every element from upper to down */
        ret[0] = triangle.get(0).get(0);
        for (int i = 1; i < triangle.size(); i++) {
            for (int j = triangle.get(i).size() - 1; j >= 0; j--) {
                if (j == triangle.get(i).size() - 1)
                    ret[j] = triangle.get(i).get(j) + ret[j - 1];
                else if (j == 0)
                    ret[j] = triangle.get(i).get(j) + ret[j];
                else
                    ret[j] = triangle.get(i).get(j) + Math.min(ret[j - 1], ret[j]);
            }
        }
        
        /** find the minimum element in the ret array which means the minimum sum */
        Arrays.sort(ret);
        return ret[0];
    }
    
    public static void main(String[] args) {
        Solution test = new Solution();
        ArrayList<Integer> a0 = new ArrayList<Integer>();
        ArrayList<Integer> a1 = new ArrayList<Integer>();
        ArrayList<Integer> a2 = new ArrayList<Integer>();
        ArrayList<Integer> a3 = new ArrayList<Integer>();
        a0.add(2);
        a1.add(3);
        a1.add(4);
        a2.add(6);
        a2.add(5);
        a2.add(7);
        a3.add(4);
        a3.add(1);
        a3.add(8);
        a3.add(3);
        ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();
        result.add(a0);
        result.add(a1);
        result.add(a2);
        result.add(a3);
        System.out.println(test.minimumTotal(result));
    }
}
Screenshot from 2016-01-11 11:41:31.png

My code:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        if (triangle == null || triangle.size() == 0)
            return 0;
        int[] ret = new int[triangle.size()]; // record data
        /** scan from right to left, store minimum value to every element from upper to down */
        ret[0] = triangle.get(0).get(0);
        for (int i = 1; i < triangle.size(); i++) {
            int size = i + 1;
            ret[size - 1] = triangle.get(i).get(size - 1) + ret[size - 2]; // for right end
            for (int j = size - 2; j >= 1; j--) {
                ret[j] = triangle.get(i).get(j) + Math.min(ret[j - 1], ret[j]);
            }
            ret[0] = triangle.get(i).get(0) + ret[0]; // for left end
        }
        
        /** find the minimum element in the ret array which means the minimum sum */
        Arrays.sort(ret);
        return ret[0];
    }
    
    public static void main(String[] args) {
        Solution test = new Solution();
        ArrayList<Integer> a0 = new ArrayList<Integer>();
        ArrayList<Integer> a1 = new ArrayList<Integer>();
        ArrayList<Integer> a2 = new ArrayList<Integer>();
        ArrayList<Integer> a3 = new ArrayList<Integer>();
        a0.add(2);
        a1.add(3);
        a1.add(4);
        a2.add(6);
        a2.add(5);
        a2.add(7);
        a3.add(4);
        a3.add(1);
        a3.add(8);
        a3.add(3);
        ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();
        result.add(a0);
        result.add(a1);
        result.add(a2);
        result.add(a3);
        System.out.println(test.minimumTotal(result));
    }
}
Screenshot from 2016-01-11 15:14:51.png

這道題木我沒有做出來,因為,我想復雜了。
我覺得,既然要找出最小值,就得找出他們的路徑,會很復雜,需要全部計算,而這個很難用代碼實現,而且可能違背了簡潔的思想。
但關鍵在于,題目只要求我們求出最小值,而不需要把路徑找出來。
那么就可以只計算到每個結點的最短距離,其實就是全部算一遍,然后在最后,找出那個數組的最小值。
同樣的是從右到左掃描。

第一段代碼,我用if else 實現,來處理corner case。時間為7ms
第二段代碼,我想辦法取消掉了if else,少了判斷,時間一下短了, 變成了4ms,效率提升了近100%.

最近太懶惰了,
蘇秦遭受了那么大的挫折,最后還是站了起來,即使毫無希望,依然堅持自己。
很令人感動。雖然可能只是大秦帝國這本小說戲劇描繪的。
我不能放棄!
還有十天。

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        if (triangle == null || triangle.size() == 0) {
            return 0;
        }
        
        int[] level = new int[triangle.size()];
        level[0] = triangle.get(0).get(0);
        for (int i = 1; i < triangle.size(); i++) {
            List<Integer> list = triangle.get(i);
            for (int j = list.size() - 1; j >= 0; j--) {
                if (j == list.size() - 1) {
                    level[j] = list.get(list.size() - 1) + level[j - 1];
                }
                else if (j == 0) {
                    level[j] = list.get(0) + level[0];
                }
                else {
                    level[j] = Math.min(level[j - 1], level[j]) + list.get(j);
                }
            }
        }
        
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < level.length; i++) {
            min = Math.min(min, level[i]);
        }
        return min;
    }
}

這一遍做,終于做出來了。因為我一直記得這道題目,我以前的錯誤在于,想找出路徑。其實只用找到最小值,不用找到路徑。

Anyway, Good luck, Richardo! --- 08/05/2016

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,461評論 6 532
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,538評論 3 417
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,423評論 0 375
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,991評論 1 312
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,761評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,207評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,268評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,419評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,959評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,782評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,983評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,528評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,222評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,653評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,901評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,678評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,978評論 2 374

推薦閱讀更多精彩內容