forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
53 lines (40 loc) · 944 Bytes
/
main.cpp
File metadata and controls
53 lines (40 loc) · 944 Bytes
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
52
53
/// Source : https://leetcode.com/problems/di-string-match/
/// Author : liuyubobobo
/// Time : 2018-11-17
#include <iostream>
#include <vector>
using namespace std;
/// Generating the result from 0
/// Adding the offset at last
///
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
vector<int> diStringMatch(string S) {
int n = S.size();
vector<int> res(n + 1, 0);
int minv = 0, maxv = 0;
for(int i = 0; i < S.size(); i ++){
int x;
if(S[i] == 'I')
x = ++maxv;
else
x = --minv;
res[i + 1] = x;
}
for(int& e: res)
e += -minv;
return res;
}
};
void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
string S1 = "IDID";
print_vec(Solution().diStringMatch(S1));
return 0;
}