public static void justifyText(String[] words, int maxLength) {

       int[] justifyCost = new int[words.length+1];
       int[] justificationPoints = new int[words.length +1];
       List<String> result = new ArrayList<>();

       justifyCost[words.length] = 0;

       for(int i = words.length-1; i >= 0; i--) {
           // assign justify cost to infinite for now
           justifyCost[i] = Integer.MAX_VALUE;
           for( int j = i+1; j<words.length+1; j++) {
               int length = 0;
               for( int k = i; k<j;k++) {
                   length += words[k].length();
               }
               //add spaces to the length as well
               length += j-i-1;

               int cost = Integer.MAX_VALUE;

               if(length <= maxLength) {
                   cost = (maxLength - length) * (maxLength - length);
               }
               // find the minimum cost
               if(cost != Integer.MAX_VALUE && justifyCost[i] > justifyCost[j] + cost) {
                    justifyCost[i] = justifyCost[j] + cost;
                    justificationPoints[i] = j;
               }

           }
       }





    }