[Leetcode]120 Triangle

Source: Internet
Author: User

https://oj.leetcode.com/problems/triangle/

http://blog.csdn.net/linhuanmars/article/details/23230657

public class solution {        public int  Minimumtotal (List<list<integer>> triangle)     {         // solution a:        // return  minimumtotal_maintainsums (triangle);                 // solution b:        return  minimumtotal_noextraspace (triangle);    }         /////////////////////////    // Solution B: NoExtraSpace     //    // use extra o (n)  to maintain current  row.    // Use triangle to maintain sums.      private iNt minimumtotal_noextraspace (List<list<integer>> triangle)     {         list<integer> extra = new arraylist <> (Triangle.get (Triangle.size ()  - 1). Size ());         for  (Int i = 1 ; i < triangle.size ()  ; i ++)          {             // Copy this row to extra memory for next iteration  Using.            extra.clear ();             for  (Int j : triangle.get (i))                  extra.add (j);                          // Calculating this row, change it to sums.             for  (int j = 0 ; j <  triangle.get (i). Size ()  ; j ++)              {                 int v = math.min (Val (triangle, i - 1, j - 1),  val ( TRIANGLE,&NBSP;I&NBSP;-&NBSP;1,&NBSP;J));                 v = v += extra.get (j);                 triangle.get (i). Set (J,&NBSP;V);             }        }                 List<Integer> lastlevel =  Triangle.get (Triangle.size ()  - 1);        int min  = integer.max_value;        for  (int i :  Lastlevel)         {             if  (i < min)                  min = i;        }         return min;    }             /////////////////////////    //  solution a: maintainsums    //    // maintain sums in another  list<list<integer>>    //    private int  Minimumtotal_maintainsums (List<list<integer>> triangle)     {         // assumes        // not  null.        // triangle.size >= 1         list<list<integer>> sums = new arraylist <> ();         sums.add (collections.<integer>  Singletonlist (Triangle.get (0). Get (0)));                 for  (Int i = 1 ; i < triangle.size ()  ; i ++) &NBSP;&NBSP;&Nbsp;     {            //  Level size            int  levelsize = i + 1;                         list<integer> newsum =  new ArrayList<> (levelsize);             for  (int j = 0 ; j < levelsize ; j ++)              {                 int v = math.min (Val (sums, i -  1, j - 1),  val (sums, i - 1, j));          &Nbsp;      v = v += triangle.get (i). Get (j);                 newsum.add (v);             }             sums.add (newsum);        }                 List<Integer>  Lastlevel = sums.get (Sums.size ()  - 1);         int  min = Integer.MAX_VALUE;        for  (int i  : lastlevel)         {             if  (i < min)                  min = i;        }         return min;    }         private int val (List<list<integer>> t, int row, int  index)     {        List<Integer>  Level = t.get (Row);        if  (index < 0  | |  index >= level.size ())              return integer.max_value;        return level.get ( index);     }}


[Leetcode]120 Triangle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.