Leetcode:sentence Screen Fitting

Source: Internet
Author: User

Given a rows x cols screens and a sentence represented by a list of words, find how many times the Given sentence can B E fitted on the screen. Note:a Word cannot is split into the lines. The order of words in the sentence must remain unchanged. The consecutive words in a line must is separated by a single space. Total words in the sentence won' t exceed 100.Length of each word won ' t exceed 10.1≤rows, cols≤20,000. Example1: Input:rows= 2, cols = 8, sentence = ["Hello", "World"]output:1Explanation:hello--- World---The character‘-‘signifies an empty space in the screen. Example2: Input:rows= 3, cols = 6, sentence = ["A", "BCD", "E"]output:2explanation:a-bcd-e-A---BCD-e-The character‘-‘signifies an empty space in the screen. Example3: Input:rows= 4, cols = 5, sentence = ["I", "had", "apple", "pie"]output:1explanation:i-Hadapplepie-Ihad--The character'-' signifies an empty space in the screen.

First a brute force, similar to the text adjustment

1  Public classSolution {2      Public intWordstyping (string[] sentence,intRowsintcols) {3         if(sentence==NULL|| sentence.length==0 | | Sentence.length>rows*cols | | rows<=0 | | Cols<=0)4             return0;5         intres = 0;6         intj = 0;//indicate the index of string in sentence that's currently trying to being inserted to current row7         introw = 0;//Current Row8         intcol = 0;//Current Col9         Ten          while(Row <rows) { One              while(col + sentence[j].length ()-1 <cols) { ACol = col + sentence[j].length () + 1; -J + +; -                 if(J = =sentence.length) { theres++; -j = 0; -                 } -             } +row++; -Col = 0; +         } A         returnRes; at     } -}

But in a slightly larger case, the tle, for example:

["A", "B", "E"] 20000 20000, spent 465ms

So think about how to save time,

Hint is can DP, think how to reuse, refer to:https://discuss.leetcode.com/topic/62364/java-optimized-solution-17ms

If this line starts with a string in sentence, then which string begins the next line, which is OK, and if the bank will not reach the end of sentence, if so, to a few times, this is a definite

These two points can be exploited because we have found the DP's multiplexing relationship

Sub-problem:if there ' s a new line which is starting with certain index in sentence, what is the starting index of next Li NE (nextindex[]). BTW, we compute how many times the "pointer in" on the current line passes over the last index (times[]).

Time Complexity:o (n (cols/lenaverage)) + O (rows), where n is the length of sentence array, lenaverage is the average Len Gth of the words in the input array.

1  Public classSolution {2      Public intWordstyping (string[] sentence,intRowsintcols) {3         int[] Nextint =New int[sentence.length];4         int[] times =New int[sentence.length];5          for(inti=0; i<sentence.length; i++) {6             intcur = i;//try to insert string with index cur in sentence to current row7             intcol = 0;//Current Col8             intTime = 0;9              while(col + sentence[cur].length ()-1 <cols) {TenCol = col + sentence[cur++].length () + 1; One                 if(cur = =sentence.length) { ACur = 0; -time++; -                 } the             } -Nextint[i] =cur; -Times[i] =Time ; -         } +          -         intres = 0; +         intCur = 0; A          for(inti=0; i<rows; i++) { atRes + =Times[cur]; -Cur =Nextint[cur]; -         } -         returnRes; -     } -}

Leetcode:sentence Screen Fitting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.