-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path418.cpp
More file actions
39 lines (37 loc) · 1.2 KB
/
418.cpp
File metadata and controls
39 lines (37 loc) · 1.2 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
class Solution {
public:
int wordsTyping(vector<string>& sentence, int rows, int cols) {
string temp;
for (auto word : sentence) temp += (word + " ");
int n = temp.size();
int start = 0;
for (int i = 0; i < rows; ++i) {
// every time, start points to the first letter
// xxxxx_xxx_xxx
// ^
start += cols;
// plus cols -> point to the "next" row's header
// [xxxxx_xx]x_xxx
// ^
// three conditions:
// (a) (b) (c)
// xxx_xxx xxx_xxx xxx_xxx
// ^ ^ ^
// should become
// xxx_xxx xxx_xxx xxx_xxx
// ^ same ^
// (a)
if (temp[start % n] == ' ') {
start++;
}
// (c) cause condition (b) will not go into while loop
else {
while (start > 0 && temp[(start - 1) % n] != ' ') {
start--;
}
}
}
// cuase next position is the actual length
return start / n;
}
};