-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path418-SentenceScreenFitting.go
More file actions
118 lines (107 loc) · 3.15 KB
/
418-SentenceScreenFitting.go
File metadata and controls
118 lines (107 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
// 418. Sentence Screen Fitting
// Given a rows x cols screen and a sentence represented as a list of strings,
// return the number of times the given sentence can be fitted on the screen.
// The order of words in the sentence must remain unchanged,
// and a word cannot be split into two lines.
// A single space must separate two consecutive words in a line.
// Example 1:
// Input: sentence = ["hello","world"], rows = 2, cols = 8
// Output: 1
// Explanation:
// hello---
// world---
// The character '-' signifies an empty space on the screen.
// Example 2:
// Input: sentence = ["a", "bcd", "e"], rows = 3, cols = 6
// Output: 2
// Explanation:
// a-bcd-
// e-a---
// bcd-e-
// The character '-' signifies an empty space on the screen.
// Example 3:
// Input: sentence = ["i","had","apple","pie"], rows = 4, cols = 5
// Output: 1
// Explanation:
// i-had
// apple
// pie-i
// had--
// The character '-' signifies an empty space on the screen.
// Constraints:
// 1 <= sentence.length <= 100
// 1 <= sentence[i].length <= 10
// sentence[i] consists of lowercase English letters.
// 1 <= rows, cols <= 2 * 10^4
import "fmt"
// 模拟
func wordsTyping(sentence []string, rows int, cols int) int {
res := 0
for i, j := 0, cols; rows > 0; {
if len(sentence[i]) <= j {
j -= len(sentence[i]) + 1
i++
} else {
rows--
j = cols
}
if i == len(sentence) {
res++
i = 0
}
}
return res
}
// dp
func wordsTyping1(sentence []string, rows int, cols int) int {
res, dp := 0, make([][]int, len(sentence))
for i := range sentence {
wordIdx, sumLen, cnt := i, len(sentence[i]), 0
for sumLen <= cols {
wordIdx++
if wordIdx == len(sentence) {
cnt++
wordIdx = 0
}
sumLen += len(sentence[wordIdx]) + 1
}
dp[i] = []int{cnt, wordIdx}
}
for i, idx := 0, 0; i < rows; i++ {
res, idx = res + dp[idx][0], dp[idx][1]
}
return res
}
func main() {
// Example 1:
// Input: sentence = ["hello","world"], rows = 2, cols = 8
// Output: 1
// Explanation:
// hello---
// world---
// The character '-' signifies an empty space on the screen.
fmt.Println(wordsTyping([]string{"hello","world"},2,8)) // 1
// Example 2:
// Input: sentence = ["a", "bcd", "e"], rows = 3, cols = 6
// Output: 2
// Explanation:
// a-bcd-
// e-a---
// bcd-e-
// The character '-' signifies an empty space on the screen.
fmt.Println(wordsTyping([]string{"a", "bcd", "e"},3,6)) // 2
// Example 3:
// Input: sentence = ["i","had","apple","pie"], rows = 4, cols = 5
// Output: 1
// Explanation:
// i-had
// apple
// pie-i
// had--
// The character '-' signifies an empty space on the screen.
fmt.Println(wordsTyping([]string{"i","had","apple","pie"},4,5)) // 1
fmt.Println(wordsTyping1([]string{"hello","world"},2,8)) // 1
fmt.Println(wordsTyping1([]string{"a", "bcd", "e"},3,6)) // 2
fmt.Println(wordsTyping1([]string{"i","had","apple","pie"},4,5)) // 1
}