-5

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;
    }
}
2
  • 2
    In the future, make your questions self-contained. Don't just provide a link; copy the relevant parts of that link into your question. That way, if the linked page changes or goes away in the future, your question will still have value to readers. Commented Nov 15 at 23:14
  • Understood, thank you! Commented Nov 17 at 16:01

1 Answer 1

3

I found the error. It is in the case of left-justifying the last line. My available spaces are getting to negative, and I'm getting stuck there until memory exhausts.

updated portion of the code:

                if(isLastLine){
                    for(String word: mapping.words){
                        sb.append(word);
                        currentWordCount++;
                        if(currentWordCount < wordcount){
                            sb.append(" ");
                        }
                      }
                      int remaining = availableSpaces - minSpaces;  
                      while(remaining > 0){
                        sb.append(" ");
                        remaining -= 1;
                      }
                }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.