Skip to content

Commit f732b24

Browse files
committed
0942 added.
1 parent e920d7d commit f732b24

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
project(C)
3+
4+
set(CMAKE_CXX_STANDARD 11)
5+
6+
add_executable(C main2.cpp)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/// Source : https://leetcode.com/problems/di-string-match/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-11-17
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Generating the result from 0
12+
/// Adding the offset at last
13+
///
14+
/// Time Complexity: O(n)
15+
/// Space Complexity: O(1)
16+
class Solution {
17+
public:
18+
vector<int> diStringMatch(string S) {
19+
20+
int n = S.size();
21+
vector<int> res(n + 1, 0);
22+
23+
int minv = 0, maxv = 0;
24+
for(int i = 0; i < S.size(); i ++){
25+
int x;
26+
if(S[i] == 'I')
27+
x = ++maxv;
28+
else
29+
x = --minv;
30+
31+
res[i + 1] = x;
32+
}
33+
34+
for(int& e: res)
35+
e += -minv;
36+
return res;
37+
}
38+
};
39+
40+
41+
void print_vec(const vector<int>& vec){
42+
for(int e: vec)
43+
cout << e << " ";
44+
cout << endl;
45+
}
46+
47+
int main() {
48+
49+
string S1 = "IDID";
50+
print_vec(Solution().diStringMatch(S1));
51+
52+
return 0;
53+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/// Source : https://leetcode.com/problems/di-string-match/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-11-17
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <cassert>
8+
9+
using namespace std;
10+
11+
12+
/// Generating the result from min value 0 and max value n - 1
13+
///
14+
/// Time Complexity: O(n)
15+
/// Space Complexity: O(1)
16+
class Solution {
17+
public:
18+
vector<int> diStringMatch(string S) {
19+
20+
int n = S.size();
21+
vector<int> res(n + 1, 0);
22+
23+
int l = 0, h = n;
24+
for(int i = 0; i < S.size(); i ++){
25+
if(S[i] == 'I')
26+
res[i] = l ++;
27+
else
28+
res[i] = h --;
29+
}
30+
31+
assert(l == h);
32+
res.back() = l;
33+
return res;
34+
}
35+
};
36+
37+
38+
void print_vec(const vector<int>& vec){
39+
for(int e: vec)
40+
cout << e << " ";
41+
cout << endl;
42+
}
43+
44+
int main() {
45+
46+
string S1 = "IDID";
47+
print_vec(Solution().diStringMatch(S1));
48+
49+
return 0;
50+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,4 +597,5 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
597597
| 939 | [Minimum Area Rectangle](https://leetcode.com/problems/minimum-area-rectangle/) | [solution](https://leetcode.com/problems/minimum-area-rectangle/solution/) | [C++](0939-Minimum-Area-Rectangle/cpp-0939/) | | |
598598
| 940 | [Distinct Subsequences II](https://leetcode.com/problems/distinct-subsequences-ii/) | [solution](https://leetcode.com/problems/distinct-subsequences-ii/solution/) | [C++](0940-Distinct-Subsequences-II/cpp-0940/) | | |
599599
| 941 | [Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/) | [solution](https://leetcode.com/problems/valid-mountain-array/solution/) | [C++](0941-Valid-Mountain-Array/cpp-0941/) | | |
600+
| 942 | [DI String Match](https://leetcode.com/problems/di-string-match/) | [solution](https://leetcode.com/problems/di-string-match/solution/) | [C++](0942-DI-String-Match/cpp-0942/) | | |
600601
| | | | | | |

0 commit comments

Comments
 (0)