forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
51 lines (39 loc) · 1.17 KB
/
main.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/// Source : https://leetcode.com/problems/number-of-lines-to-write-string/description/
/// Author : liuyubobobo
/// Time : 2018-03-24
#include <iostream>
#include <vector>
using namespace std;
/// Ad-Hoc
/// Time Complexity: O(len(S))
/// Space Complexity : O(1)
class Solution {
public:
vector<int> numberOfLines(vector<int>& widths, string S) {
int lines = 0, cur = 0;
for(char c: S){
int w = widths[c - 'a'];
if(cur + w > 100){
lines ++;
cur = w;
}
else
cur += w;
}
return vector<int>{lines + 1, cur};
}
};
void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
vector<int> widths1 = {10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10};
string S1 = "abcdefghijklmnopqrstuvwxyz";
print_vec(Solution().numberOfLines(widths1, S1));
vector<int> widths2 = {4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10};
string S2 = "bbbcccdddaaa";
print_vec(Solution().numberOfLines(widths2, S2));
return 0;
}