I'm using Java to solve Leetcode 68: Text Justification problem (https://leetcode.com/problems/text-justification/description/)
Given 3 test cases are passing well but for the next text case, words = ["ask","not","what","your","country","can","do","for","you","ask","what","you","can","do","for","your","country"] maxWidth =16
I'm getting the Memory Limit Exceeded error. Can someone please help me debug this issue without changing the structure of my code:
class Dsa{
int availspaces;
List<String> words;
int minSpacesReq;
public Dsa(int maxWidth){
this.availspaces = maxWidth;
this.words = new ArrayList<>();
this.minSpacesReq = 0;
}
@Override
public String toString(){
return "words: "+ this.words + ", availspaces: "+ availspaces + ", min spaces: " + minSpacesReq;
}
}
class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
List<Dsa> mappings = new ArrayList<>();
// each line -> list of words fit in a line, available space
int currentLine = 0;
Dsa currentMapping = null;
for(String currentWord: words){
if(currentLine !=0 && (currentMapping.availspaces - currentMapping.minSpacesReq >= currentWord.length())){
// if(currentLine !=0 && (currentMapping.availspaces - 1 >= currentWord.length())){
currentMapping.words.add(currentWord);
currentMapping.availspaces -= currentWord.length();
currentMapping.minSpacesReq = currentMapping.words.size();
}else{
// System.out.println("word:"+ currentWord);
currentMapping = new Dsa(maxWidth);
currentMapping.words.add(currentWord);
currentMapping.availspaces = maxWidth-currentWord.length();
currentMapping.minSpacesReq = 1;
mappings.add(currentMapping);
currentLine++;
}
}
/*for(Dsa mapping: mappings){
System.out.println(mapping.toString());
}*/
List<String> result = new ArrayList<>();
for(Dsa mapping: mappings){
StringBuilder sb = new StringBuilder();
int minSpaces = mapping.words.size()-1;
int availableSpaces = mapping.availspaces;
boolean isLastLine = false;
/*if(currentLine-- == 1 || currentLine == 0){
isLastLine = true;
}*/
if(mappings.indexOf(mapping) == mappings.size() - 1){
isLastLine = true;
}
String lastWord = mapping.words.get(minSpaces);
int wordcount = mapping.words.size();
int currentWordCount = 0;
if(minSpaces != 0){
int mod = availableSpaces%minSpaces;
int quotient = availableSpaces/minSpaces;
if(isLastLine){
for(String word: mapping.words){
sb.append(word);
availableSpaces--;
if(availableSpaces >0){
sb.append(" ");
}
}
while(availableSpaces != 0){
sb.append(" ");
availableSpaces--;
}
}
else{
if(mod == 0){
//quotient number of spaces after each word until availspaces expire
for(String word: mapping.words){
sb.append(word);
int quot = quotient;
currentWordCount++;
if(currentWordCount != wordcount){
while(quot > 0){
sb.append(" ");
quot--;
}
}
}
}else{
for(String word: mapping.words){
int spacestoadd = quotient ;
if(mod != 0){
spacestoadd += 1;
mod--;
}
sb.append(word);
currentWordCount++;
if(currentWordCount != wordcount){
while(spacestoadd >0){
sb.append(" ");
spacestoadd--;
}
}
}
}
}
}else{
sb.append(mapping.words.get(0));
while(availableSpaces >0 ){
sb.append(" ");
availableSpaces--;
}
}
result.add(sb.toString());
}
return result;
}
}