Skip to content

Commit ec53e86

Browse files
authored
Merge pull request #88 from InflixOP/patch1
Create #901. Online Stock Span.cpp
2 parents 5ad0fe5 + 2e62eb2 commit ec53e86

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
string clearStars(string s) {
4+
unordered_map<char,priority_queue<int>> mp;
5+
priority_queue<char,vector<char>,greater<char>> pq;
6+
int n=s.length();
7+
for(int i=0;i<n;i++)
8+
{
9+
if(s[i]=='*')
10+
{
11+
s[i]='{';
12+
int idx=mp[pq.top()].top();
13+
if(mp[pq.top()].size())
14+
{
15+
mp[pq.top()].pop();
16+
s[idx]='{';
17+
}
18+
pq.pop();
19+
}
20+
else
21+
{
22+
pq.push(s[i]);
23+
mp[s[i]].push(i);
24+
}
25+
}
26+
string ans="";
27+
for(int i=0;i<n;i++)
28+
if(s[i]!='{')
29+
ans+= s[i];
30+
return ans;
31+
}
32+
};

#901. Online Stock Span.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class StockSpanner {
2+
public:
3+
StockSpanner() {
4+
}
5+
stack<pair<int,int>> stk;
6+
int next(int price) {
7+
int idx = 1;
8+
while(!stk.empty() && stk.top().first<=price) {
9+
idx += stk.top().second;
10+
stk.pop();
11+
}
12+
stk.push({price,idx});
13+
return idx;
14+
}
15+
};
16+
17+
/**
18+
* Your StockSpanner object will be instantiated and called as such:
19+
* StockSpanner* obj = new StockSpanner();
20+
* int param_1 = obj->next(price);
21+
*/

0 commit comments

Comments
 (0)