Skip to content

Commit d373308

Browse files
authored
Create number-of-lines-to-write-string.cpp
1 parent 2ef8994 commit d373308

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<int> numberOfLines(vector<int>& widths, string S) {
7+
vector<int> result(2);
8+
result.front() = 1;
9+
for (const auto& c : S) {
10+
const auto& w = widths[c - 'a'];
11+
result.back() += w;
12+
if (result.back() > 100) {
13+
++result.front();
14+
result.back() = w;
15+
}
16+
}
17+
return result;
18+
}
19+
};

0 commit comments

Comments
 (0)