Skip to content

Commit f3db81b

Browse files
committed
Feb 3 and added hint.
1 parent be5a901 commit f3db81b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ Solutions <a href = "https://github.com/aliasvishnu/leetcode/tree/master/Solutio
148148
| 277 | Array | Consider P1, P2. If P1 knows P2, P1 can't be the answer and vice versa. If both know each other or both don't know each other, both can't be the answer. Extend this idea. | O(n) |
149149
| 278 | Binary Search | BF | o(log(n)) |
150150
| 279 | DP | F(n) = 1 + min{i = 1..sqrt(N)} (F(n-i*i)) | O(n sqrt(n)) |
151+
| 316 | Stack | First count number of instances of each character. Go through left to right. If the stack is empty, insert. If the character is already in the stack, continue. While the character in the stack.top is larger than current character and has more instances later, remove it. Finally add current char to the stack. Core idea is that you are trying to push the smaller characters to the left as much as possible. You only force yourself to add a larger character if it never appears again. | O(n) |
151152
| 413 | Array | [1, 2, 3, 4, 5, 7, 9, 11]. can be written as [5, 3] i.e. 5 sequence of difference 1 and 3 sequence of difference 2, you need to figure out how many parts you can split 5 and 3 into. | O(n) |
152153
| 694 | DFS | Keep track of the directions in which DFS proceeds in some form, maybe a string like ddr for down down right. | O(rows*cols) |
153154
| 738 | Array | Find the first time Xi > Xi+1, Xi -= 1 and turn all Xi+k = 9, For eg, 321 becomes 299. Figure out cases like 33332. | O(n) |
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
string removeDuplicateLetters(string s) {
4+
int len = s.size();
5+
vector<int> count (26, 0);
6+
vector<bool> inS (26, false);
7+
string stk = "";
8+
9+
for(char c: s) count[c-'a']++;
10+
11+
for(int c: s){
12+
count[c-'a']--;
13+
14+
if(stk.empty()){
15+
stk.push_back(c);
16+
inS[c-'a'] = true;
17+
continue;
18+
}
19+
20+
if(inS[c-'a']) continue;
21+
22+
while (!stk.empty() && stk.back() > c && count[stk.back()-'a'] > 0){
23+
char back = stk.back();
24+
inS[back-'a'] = false;
25+
stk.pop_back();
26+
}
27+
28+
stk.push_back(c);
29+
inS[c-'a'] = true;
30+
}
31+
32+
return stk;
33+
}
34+
};

0 commit comments

Comments
 (0)