Skip to content

Commit 1e46c9a

Browse files
authored
feat: add solutions to lc problem: No.0418 (doocs#1323)
No.0418.Sentence Screen Fitting
1 parent f87593e commit 1e46c9a

File tree

18 files changed

+314
-23
lines changed

18 files changed

+314
-23
lines changed

solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,8 @@ class Solution {
113113
List<String> stk = new ArrayList<>();
114114
for (String s : preorder.split(",")) {
115115
stk.add(s);
116-
while (stk.size() >= 3
117-
&& stk.get(stk.size() - 1).equals("#")
118-
&& stk.get(stk.size() - 2).equals("#")
119-
&& !stk.get(stk.size() - 3).equals("#")) {
116+
while (stk.size() >= 3 && stk.get(stk.size() - 1).equals("#")
117+
&& stk.get(stk.size() - 2).equals("#") && !stk.get(stk.size() - 3).equals("#")) {
120118
stk.remove(stk.size() - 1);
121119
stk.remove(stk.size() - 1);
122120
stk.remove(stk.size() - 1);

solution/0300-0399/0331.Verify Preorder Serialization of a Binary Tree/README_EN.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,8 @@ class Solution {
8383
List<String> stk = new ArrayList<>();
8484
for (String s : preorder.split(",")) {
8585
stk.add(s);
86-
while (stk.size() >= 3
87-
&& stk.get(stk.size() - 1).equals("#")
88-
&& stk.get(stk.size() - 2).equals("#")
89-
&& !stk.get(stk.size() - 3).equals("#")) {
86+
while (stk.size() >= 3 && stk.get(stk.size() - 1).equals("#")
87+
&& stk.get(stk.size() - 2).equals("#") && !stk.get(stk.size() - 3).equals("#")) {
9088
stk.remove(stk.size() - 1);
9189
stk.remove(stk.size() - 1);
9290
stk.remove(stk.size() - 1);

solution/0400-0499/0418.Sentence Screen Fitting/README.md

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,132 @@ had--
7979

8080
<!-- 这里可写通用的实现逻辑 -->
8181

82+
**方法一:贪心**
83+
84+
我们将句子的每个单词拼接上一个空格,然后把句子拼接起来,得到字符串 $s$。例如,对于句子 `["hello", "world"]`,得到的字符串为 `"hello world "`。记 $s$ 的长度为 $m$。
85+
86+
接下来,我们使用贪心的方法,找到最大的可显示句子数。定义一个变量 $cur$,表示当前已经在屏幕上显示的字符串的长度,初始时 $cur=0$。
87+
88+
我们循环 $rows$ 次,每次循环中,我们首先将 $cur$ 增加 $cols$,如果 $s[cur \bmod m]$ 是一个空格,说明我们可以将完整的若干个单词放置到当前行,因此我们将 $cur$ 增加一个额外的 $1$;否则,说明我们需要回退 $cur$,直到 $cur$ 指向的字符是一个空格为止。然后继续下一次循环。
89+
90+
循环结束,返回 $\lfloor \frac{cur}{m} \rfloor$ 即可。
91+
92+
时间复杂度 $O(rows \times M)$,空间复杂度 $O(L)$。其中 $M$ 是单词的最大长度,而 $L$ 是单词的总长度。
93+
8294
<!-- tabs:start -->
8395

8496
### **Python3**
8597

8698
<!-- 这里可写当前语言的特殊实现逻辑 -->
8799

88100
```python
89-
101+
class Solution:
102+
def wordsTyping(self, sentence: List[str], rows: int, cols: int) -> int:
103+
s = " ".join(sentence) + " "
104+
m = len(s)
105+
cur = 0
106+
for _ in range(rows):
107+
cur += cols
108+
if s[cur % m] == " ":
109+
cur += 1
110+
while cur and s[(cur - 1) % m] != " ":
111+
cur -= 1
112+
return cur // m
90113
```
91114

92115
### **Java**
93116

94117
<!-- 这里可写当前语言的特殊实现逻辑 -->
95118

96119
```java
120+
class Solution {
121+
public int wordsTyping(String[] sentence, int rows, int cols) {
122+
String s = String.join(" ", sentence) + " ";
123+
int m = s.length();
124+
int cur = 0;
125+
while (rows-- > 0) {
126+
cur += cols;
127+
if (s.charAt(cur % m) == ' ') {
128+
++cur;
129+
} else {
130+
while (cur > 0 && s.charAt((cur - 1) % m) != ' ') {
131+
--cur;
132+
}
133+
}
134+
}
135+
return cur / m;
136+
}
137+
}
138+
```
139+
140+
### **C++**
141+
142+
```cpp
143+
class Solution {
144+
public:
145+
int wordsTyping(vector<string>& sentence, int rows, int cols) {
146+
string s;
147+
for (auto& t: sentence) {
148+
s += t;
149+
s += " ";
150+
}
151+
int m = s.size();
152+
int cur = 0;
153+
while (rows--) {
154+
cur += cols;
155+
if (s[cur % m] == ' ') {
156+
++cur;
157+
} else {
158+
while (cur && s[(cur - 1) % m] != ' ') {
159+
--cur;
160+
}
161+
}
162+
}
163+
return cur / m;
164+
}
165+
};
166+
```
167+
168+
### **Go**
169+
170+
```go
171+
func wordsTyping(sentence []string, rows int, cols int) int {
172+
s := strings.Join(sentence, " ") + " "
173+
m := len(s)
174+
cur := 0
175+
for i := 0; i < rows; i++ {
176+
cur += cols
177+
if s[cur%m] == ' ' {
178+
cur++
179+
} else {
180+
for cur > 0 && s[(cur-1)%m] != ' ' {
181+
cur--
182+
}
183+
}
184+
}
185+
return cur / m
186+
}
187+
```
97188

189+
### **TypeScript**
190+
191+
```ts
192+
function wordsTyping(sentence: string[], rows: number, cols: number): number {
193+
const s = sentence.join(' ') + ' ';
194+
let cur = 0;
195+
const m = s.length;
196+
for (let i = 0; i < rows; ++i) {
197+
cur += cols;
198+
if (s[cur % m] === ' ') {
199+
++cur;
200+
} else {
201+
while (cur > 0 && s[(cur - 1) % m] !== ' ') {
202+
--cur;
203+
}
204+
}
205+
}
206+
return Math.floor(cur / m);
207+
}
98208
```
99209

100210
### **...**

solution/0400-0499/0418.Sentence Screen Fitting/README_EN.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,111 @@ The character &#39;-&#39; signifies an empty space on the screen.
6262
### **Python3**
6363

6464
```python
65-
65+
class Solution:
66+
def wordsTyping(self, sentence: List[str], rows: int, cols: int) -> int:
67+
s = " ".join(sentence) + " "
68+
m = len(s)
69+
cur = 0
70+
for _ in range(rows):
71+
cur += cols
72+
if s[cur % m] == " ":
73+
cur += 1
74+
while cur and s[(cur - 1) % m] != " ":
75+
cur -= 1
76+
return cur // m
6677
```
6778

6879
### **Java**
6980

7081
```java
82+
class Solution {
83+
public int wordsTyping(String[] sentence, int rows, int cols) {
84+
String s = String.join(" ", sentence) + " ";
85+
int m = s.length();
86+
int cur = 0;
87+
while (rows-- > 0) {
88+
cur += cols;
89+
if (s.charAt(cur % m) == ' ') {
90+
++cur;
91+
} else {
92+
while (cur > 0 && s.charAt((cur - 1) % m) != ' ') {
93+
--cur;
94+
}
95+
}
96+
}
97+
return cur / m;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int wordsTyping(vector<string>& sentence, int rows, int cols) {
108+
string s;
109+
for (auto& t: sentence) {
110+
s += t;
111+
s += " ";
112+
}
113+
int m = s.size();
114+
int cur = 0;
115+
while (rows--) {
116+
cur += cols;
117+
if (s[cur % m] == ' ') {
118+
++cur;
119+
} else {
120+
while (cur && s[(cur - 1) % m] != ' ') {
121+
--cur;
122+
}
123+
}
124+
}
125+
return cur / m;
126+
}
127+
};
128+
```
129+
130+
### **Go**
131+
132+
```go
133+
func wordsTyping(sentence []string, rows int, cols int) int {
134+
s := strings.Join(sentence, " ") + " "
135+
m := len(s)
136+
cur := 0
137+
for i := 0; i < rows; i++ {
138+
cur += cols
139+
if s[cur%m] == ' ' {
140+
cur++
141+
} else {
142+
for cur > 0 && s[(cur-1)%m] != ' ' {
143+
cur--
144+
}
145+
}
146+
}
147+
return cur / m
148+
}
149+
```
71150

151+
### **TypeScript**
152+
153+
```ts
154+
function wordsTyping(sentence: string[], rows: number, cols: number): number {
155+
const s = sentence.join(' ') + ' ';
156+
let cur = 0;
157+
const m = s.length;
158+
for (let i = 0; i < rows; ++i) {
159+
cur += cols;
160+
if (s[cur % m] === ' ') {
161+
++cur;
162+
} else {
163+
while (cur > 0 && s[(cur - 1) % m] !== ' ') {
164+
--cur;
165+
}
166+
}
167+
}
168+
return Math.floor(cur / m);
169+
}
72170
```
73171

74172
### **...**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int wordsTyping(vector<string>& sentence, int rows, int cols) {
4+
string s;
5+
for (auto& t: sentence) {
6+
s += t;
7+
s += " ";
8+
}
9+
int m = s.size();
10+
int cur = 0;
11+
while (rows--) {
12+
cur += cols;
13+
if (s[cur % m] == ' ') {
14+
++cur;
15+
} else {
16+
while (cur && s[(cur - 1) % m] != ' ') {
17+
--cur;
18+
}
19+
}
20+
}
21+
return cur / m;
22+
}
23+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func wordsTyping(sentence []string, rows int, cols int) int {
2+
s := strings.Join(sentence, " ") + " "
3+
m := len(s)
4+
cur := 0
5+
for i := 0; i < rows; i++ {
6+
cur += cols
7+
if s[cur%m] == ' ' {
8+
cur++
9+
} else {
10+
for cur > 0 && s[(cur-1)%m] != ' ' {
11+
cur--
12+
}
13+
}
14+
}
15+
return cur / m
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int wordsTyping(String[] sentence, int rows, int cols) {
3+
String s = String.join(" ", sentence) + " ";
4+
int m = s.length();
5+
int cur = 0;
6+
while (rows-- > 0) {
7+
cur += cols;
8+
if (s.charAt(cur % m) == ' ') {
9+
++cur;
10+
} else {
11+
while (cur > 0 && s.charAt((cur - 1) % m) != ' ') {
12+
--cur;
13+
}
14+
}
15+
}
16+
return cur / m;
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def wordsTyping(self, sentence: List[str], rows: int, cols: int) -> int:
3+
s = " ".join(sentence) + " "
4+
m = len(s)
5+
cur = 0
6+
for _ in range(rows):
7+
cur += cols
8+
if s[cur % m] == " ":
9+
cur += 1
10+
while cur and s[(cur - 1) % m] != " ":
11+
cur -= 1
12+
return cur // m
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function wordsTyping(sentence: string[], rows: number, cols: number): number {
2+
const s = sentence.join(' ') + ' ';
3+
let cur = 0;
4+
const m = s.length;
5+
for (let i = 0; i < rows; ++i) {
6+
cur += cols;
7+
if (s[cur % m] === ' ') {
8+
++cur;
9+
} else {
10+
while (cur > 0 && s[(cur - 1) % m] !== ' ') {
11+
--cur;
12+
}
13+
}
14+
}
15+
return Math.floor(cur / m);
16+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
func angleClock(hour int, minutes int) float64 {
2-
h := 30*float64(hour) + 0.5*float64(minutes)
3-
m := 6 * float64(minutes)
4-
diff := math.Abs(h - m)
5-
return math.Min(diff, 360-diff)
1+
func angleClock(hour int, minutes int) float64 {
2+
h := 30*float64(hour) + 0.5*float64(minutes)
3+
m := 6 * float64(minutes)
4+
diff := math.Abs(h - m)
5+
return math.Min(diff, 360-diff)
66
}

0 commit comments

Comments
 (0)