public ArrayList<String> fullJustify(String[] words, int L) {
        ArrayList<String> res = new ArrayList<String>();
        
        int length, end;
        for(int i = 0; i < words.length;){
        	length = words[i].length();
        	end = i + 1;
        	// try to get as many as words in
        	while(end < words.length && length + words[end].length() + end - i <= L){
        		length += words[end++].length();
        	}
        	String ans = "";
        	if(end != words.length){ // not the last line
        		if(end == i + 1){ // only one word
        			ans = words[i] + getBlks(L - words[i].length());
        		} else { // several words
        			for(int j = i, extra = (L - length) % (end - i - 1); j < end - 1; j++){
        				ans += words[j] + getBlks((L - length) / (end - i - 1));
        				if(extra > 0){
        					ans += " ";
        					extra--;
        				}
        			}
        			ans += words[end - 1];
        		}
        	} else { // last line, don't perform left-justified
        		for(int j = i; j < end - 1; j++) ans += words[j] + " ";
        		ans += words[end - 1];
        		for(int j = ans.length(); j < L; j++) ans += " ";
        	}
        	res.add(ans);
        	i = end;
        }
        return res;
    }