-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path830.cpp
102 lines (95 loc) · 2.87 KB
/
830.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
__________________________________________________________________________________________________
sample 8 ms submission
/* Just use two pointers to find window of each group. If group has length no less than 3, add the range of group to result.
The time complexity is O(n) and space complexity is the same.
*/
class Solution {
public:
vector<vector<int>> largeGroupPositions(string S) {
vector<vector<int>> res;
int st = 0;
char pre = S[0];
for (int i = 1; i < S.length(); ++i) {
if (S[i] != pre) {
if (i - st >= 3) {
res.push_back({st, i - 1});
}
st = i;
pre = S[i];
}
}
if (S.length() - st >= 3) {
res.push_back({st, S.length() - 1});
}
return res;
}
};
__________________________________________________________________________________________________
sample 9476 kb submission
class Solution {
public:
vector<vector<int>> largeGroupPositions(string S) {
vector<vector<int>> results;
if(S.size()>=3){
int start = 0;
int count = 1;
for(int i=1;i<S.size();i++){
if(S[start]==S[i]){
count++;
}else{
if(count>=3){
vector<int> result = {start,start+count-1};
results.push_back(result);
}
start = i;
count = 1;
}
}
if(count>=3){
vector<int> result = {start,start+count-1};
results.push_back(result);
}
}
return results;
}
};
static auto speedup = [](){
ios::sync_with_stdio(false);
cin.tie(nullptr);
return nullptr;
}();
__________________________________________________________________________________________________
sample 9600 kb submission
static const int desync_io = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();
class Solution {
public:
vector<vector<int>> largeGroupPositions(string S) {
char a=S[0], b=S[0];
vector<vector<int>> v;
int cnt=1;
int st=0;
for(int i=1; i<S.length(); ++i) {
if (S[i]==a)
++cnt;
if ((S[i]!=a) || (i==S.length()-1)) {
vector<int> temp;
if (cnt>=3) {
temp.push_back(st);
if(S[i]!=a)
temp.push_back(i-1);
else
temp.push_back(i);
v.push_back(temp);
}
st=i;
a=S[i];
cnt=1;
}
}
return v;
}
};