Didn't see any C# solutions


  • 0
    C
    public class Solution {
        public IList<string> FullJustify(string[] words, int maxWidth) {
            List<string> lines = new List<string>();
            List<string> line = new List<string>();
            int budget = maxWidth;
            for (int i = 0; i < words.Length; i++) {
                if (words[i].Length > budget) {
                    lines.Add( JustifyLine(line, budget + line.Count) );
                    line = new List<string>();
                    budget = maxWidth;
                }
                line.Add(words[i]);
                budget -= words[i].Length + 1;
            }
            lines.Add( JustifyLastLine(line, budget + line.Count) );
            
            return lines;
        }
        
        public string JustifyLine (List<string> words, int numSpaces) {
            string output = "";
            for (int i = 0; i < words.Count; i++) {
                output += words[i];
                int space = CalculateSpaces (words.Count - 1, i, numSpaces);
                output += WhiteSpace(space);
            }
            return output;
        }
        
        public string JustifyLastLine (List<string> words, int numSpaces) {
            string output = "";
            for (int i = 0; i < words.Count - 1; i++) {
                output += words[i] + " ";
                numSpaces --;
            }
            output += words[words.Count - 1] + WhiteSpace(numSpaces);
            return output;
        }
        
        public int CalculateSpaces (int slots, int wordIndex, int numSpaces) {
            slots = Math.Max(slots, 1)
            if (wordIndex >= slots) {   return 0;   }
            int output = numSpaces / slots;
            if (numSpaces % slots > 0 && wordIndex < numSpaces % slots) {
                output += 1;
            }
            return output;
        }
    
        
        public string WhiteSpace (int length) {
            string output = "";
            for (int i = 0; i < length; i++) {
                output += " ";
            }
            return output;
        }
    }
    

Log in to reply
 

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