Skip to content

Commit 6d55179

Browse files
committed
480/520 problems solved. Can't access some problems due to expiration of premium membership
1 parent 30ed7c4 commit 6d55179

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

Teemo_Attacking.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
4+
int result = 0;
5+
int n = (int)timeSeries.size();
6+
for(int i = 0; i < n; i++) {
7+
int start = timeSeries[i];
8+
int end = start + duration - 1;
9+
while(i + 1 < n and timeSeries[i + 1] <= end) {
10+
end = timeSeries[i + 1] + duration - 1;
11+
i++;
12+
}
13+
result += (end - start + 1);
14+
}
15+
16+
return result;
17+
}
18+
};

UTF-8_Validation.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
const int MSB = 7;
3+
public:
4+
bool validUtf8(vector<int>& data) {
5+
int i = 0, n = (int)data.size();
6+
while(i < n) {
7+
int nbyte = 0;
8+
for(int k = 0; k < 4 and (data[i] & (1 << (MSB - k))); k++) {
9+
nbyte++;
10+
}
11+
if(nbyte == 1) {
12+
return false;
13+
}
14+
if(nbyte == 0) nbyte++;
15+
if(nbyte > 1 and (bool)(data[i] & (1 << (MSB - nbyte))) ) {
16+
return false;
17+
}
18+
if(i + nbyte - 1 >= n) {
19+
return false;
20+
}
21+
for(int k = 1; k < nbyte; k++) {
22+
bool on = data[i + k] & (1 << MSB);
23+
bool off = data[i + k] & (1 << (MSB - 1));
24+
if(!on or off) {
25+
return false;
26+
}
27+
}
28+
i += nbyte;
29+
}
30+
return true;
31+
}
32+
};

Zuma_Game.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class Solution {
2+
const int MAX_STEP = 6;
3+
4+
void shrink(string& board) {
5+
if(board.empty()) {
6+
return;
7+
}
8+
int left = 0;
9+
for(int i = 0; i <= board.size(); i++) {
10+
if (i == board.size() || board[i] != board[left]) {
11+
if(i - left >= 3) {
12+
board = board.substr(0, left) + board.substr(i);
13+
shrink(board);
14+
return;
15+
}
16+
left = i;
17+
}
18+
}
19+
}
20+
21+
int solve(string board, unordered_map<char, int>& handMap) {
22+
if (board.empty()) return 0;
23+
if (handMap.empty()) return MAX_STEP;
24+
int ret = MAX_STEP;
25+
int n = (int)board.size();
26+
int i = 0;
27+
while (i < n) {
28+
if(!handMap.count(board[i])) {
29+
i++;
30+
continue;
31+
}
32+
if(i + 1 < n and board[i] == board[i + 1]) {
33+
string nextBoard = board.substr(0, i) + board.substr(i + 2);
34+
shrink(nextBoard);
35+
handMap[board[i]]--;
36+
if(!handMap[board[i]]) {
37+
handMap.erase(board[i]);
38+
}
39+
ret = min(ret, 1 + solve(nextBoard, handMap));
40+
handMap[board[i]]++;
41+
i++;
42+
} else if(handMap[board[i]] >= 2) {
43+
string nextBoard = board.substr(0, i) + board.substr(i + 1);
44+
shrink(nextBoard);
45+
handMap[board[i]] -= 2;
46+
if(!handMap[board[i]]) {
47+
handMap.erase(board[i]);
48+
}
49+
ret = min(ret, 2 + solve(nextBoard, handMap));
50+
handMap[board[i]] += 2;
51+
}
52+
i++;
53+
}
54+
return ret;
55+
}
56+
57+
public:
58+
int findMinStep(string board, string hand) {
59+
unordered_map<char, int> handMap;
60+
for(int i = 0; i < hand.size(); i++) {
61+
handMap[hand[i]]++;
62+
}
63+
int result = solve(board, handMap);
64+
return result >= MAX_STEP ? -1 : result;
65+
}
66+
};

0 commit comments

Comments
 (0)