leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

pascals-triangle-ii.dart (675B)


      1 //Given a zero indexed row of pascal's triangle return
      2 //the values in that row. I solved this using DP and recursion
      3 //to find the first row then second and so on until returning the requested row.
      4 //The time complexity of this is O(n^2).
      5 //Time: 260ms Beats: 63.64%
      6 //Memory: 140.7MB Beats: 36.36%
      7 
      8 class Solution {
      9   List<int> getRow(int rowIndex) {
     10       if(rowIndex <= 0){
     11           return [1];
     12       }
     13       List<int> last_row = getRow(rowIndex - 1);
     14       List<int> this_row = [];
     15       this_row.add(1);
     16       for(int i = 1 ; i < last_row.length ; ++i){
     17           this_row.add(last_row[i-1] + last_row[i]); 
     18       }
     19       this_row.add(1);
     20       return this_row;
     21   }
     22 }