forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sentence-screen-fitting.cpp
35 lines (31 loc) · 982 Bytes
/
sentence-screen-fitting.cpp
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
// Time: O(r + n * c)
// Space: O(n)
class Solution {
public:
int wordsTyping(vector<string>& sentence, int rows, int cols) {
vector<int> wc(sentence.size());
for (int i = 0; i < sentence.size(); ++i) {
wc[i] = wordsFit(sentence, i, cols);
}
int words = 0, start = 0;
for (int i = 0; i < rows; ++i) {
words += wc[start];
start = (start + wc[start]) % sentence.size();
}
return words / sentence.size();
}
private:
int wordsFit(const vector<string>& sentence, int start, int cols) {
if (sentence[start].length() > cols) {
return 0;
}
int sum = sentence[start].length(), count = 1;
for (int i = (start + 1) % sentence.size();
sum + 1 + sentence[i].length() <= cols;
i = (i + 1) % sentence.size()) {
sum += 1 + sentence[i].length();
++count;
}
return count;
}
};