My solution in O(k) (?)


  • 0
    L

    O(N^2) time tho..

        public class Solution {
    public List<Integer> getRow(int rowIndex) {
        
        ArrayList<Integer> pascal = new ArrayList<Integer>();
        ArrayList<Integer> lastRow = new ArrayList<Integer>();
        lastRow.add(1);
                
        for ( int i = 0; i <= rowIndex; i++) {
            pascal.clear();
    
            for (int j = 0; j <= i; j++) {
                if (j == 0) pascal.add(lastRow.get(0));
                else if (j == i) pascal.add(lastRow.get(j-1));
                else pascal.add(lastRow.get(j-1) + lastRow.get(j));
            }
            
            lastRow = (ArrayList<Integer>) pascal.clone();
        }      
        return pascal;       
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.