File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 406
406
415|[ Add Strings] ( ./solutions/0415-add-strings.js ) |Easy|
407
407
416|[ Partition Equal Subset Sum] ( ./solutions/0416-partition-equal-subset-sum.js ) |Medium|
408
408
417|[ Pacific Atlantic Water Flow] ( ./solutions/0417-pacific-atlantic-water-flow.js ) |Medium|
409
+ 418|[ Sentence Screen Fitting] ( ./solutions/0418-sentence-screen-fitting.js ) |Medium|
409
410
419|[ Battleships in a Board] ( ./solutions/0419-battleships-in-a-board.js ) |Medium|
410
411
420|[ Strong Password Checker] ( ./solutions/0420-strong-password-checker.js ) |Hard|
411
412
421|[ Maximum XOR of Two Numbers in an Array] ( ./solutions/0421-maximum-xor-of-two-numbers-in-an-array.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 418. Sentence Screen Fitting
3
+ * https://leetcode.com/problems/sentence-screen-fitting/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given a rows x cols screen and a sentence represented as a list of strings, return the number
7
+ * of times the given sentence can be fitted on the screen.
8
+ *
9
+ * The order of words in the sentence must remain unchanged, and a word cannot be split into two
10
+ * lines. A single space must separate two consecutive words in a line.
11
+ */
12
+
13
+ /**
14
+ * @param {string[] } sentence
15
+ * @param {number } rows
16
+ * @param {number } cols
17
+ * @return {number }
18
+ */
19
+ var wordsTyping = function ( sentence , rows , cols ) {
20
+ const sentenceLength = sentence . length ;
21
+ const wordLengths = sentence . map ( word => word . length + 1 ) ;
22
+ let rowIndex = 0 ;
23
+ let colIndex = 0 ;
24
+ let wordIndex = 0 ;
25
+ let result = 0 ;
26
+
27
+ while ( rowIndex < rows ) {
28
+ if ( colIndex + wordLengths [ wordIndex ] - 1 <= cols ) {
29
+ colIndex += wordLengths [ wordIndex ] ;
30
+ wordIndex ++ ;
31
+ if ( wordIndex === sentenceLength ) {
32
+ result ++ ;
33
+ wordIndex = 0 ;
34
+ }
35
+ } else {
36
+ rowIndex ++ ;
37
+ colIndex = 0 ;
38
+ }
39
+ }
40
+
41
+ return result ;
42
+ } ;
You can’t perform that action at this time.
0 commit comments