-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcandy_crsuh.cpp
72 lines (57 loc) · 1.89 KB
/
candy_crsuh.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
#include<bits/stdc++.h>
using namespace std;
void extract(stack<int> & s) {
vector<int> ext;
while(s.size() && ext.size() < 3) ext.push_back(s.top()), s.pop();
if(ext.size() != 3 || ext[0] != ext[2] || ext[1] != ext[0]) {
while(ext.size()) s.push(ext.back()), ext.pop_back();
return;
}
int cur = ext[0];
while(s.size() && cur == s.top()) s.pop();
}
vector<int> getReducedGame(const vector<int> & v) {
stack<int> s;
int n = v.size();
for(int i = 0; i <= n; ++i) {
if(s.size() && (i == n || s.top() != v[i])) {
extract(s);
}
if(i < n) s.push(v[i]);
}
vector<int> ret(s.size());
while(s.size()) ret[s.size() - 1] = s.top(), s.pop();
return ret;
}
vector<int> candyCrush(const vector<int> & nums) {
int endPtr = nums.length - 1;
int curPtr = endPtr;
while (curPtr >=0) {
int nextPtr = curPtr-1;
while (nextPtr >= 0 && nums[nextPtr] == nums[curPtr]) nextPtr--;
int diff = curPtr - nextPtr;
if (diff >= 3) {
// if curPtr is not the last one,
// shift diff position forward for all elements after the curPtr up to the endPtr
if (curPtr < endPtr) {
for (int shiftFwdPtr = curPtr + 1; shiftFwdPtr <= endPtr; shiftFwdPtr++) {
nums[shiftFwdPtr - diff] = nums[shiftFwdPtr];
}
}
endPtr -= diff;
curPtr = nextPtr+1;
while (curPtr < endPtr && nums[curPtr] == nums[curPtr+1]) curPtr++;
}
else {
curPtr = nextPtr;
}
}
int length = endPtr + 1;
int[] ans = new int[length];
System.arraycopy(nums, 0, ans, 0, length);
return ans;
}
int main() {
vector<int> sol = getReducedGame({1, 3, 3, 3, 2, 2, 2, 3, 1});
copy(sol.begin(), sol.end(), ostream_iterator<int>(cout, " "));
}