Skip to content

Commit ef557fc

Browse files
ADD: More leetcode problems
1 parent 1b893b0 commit ef557fc

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/
2+
3+
/**
4+
* Logic: It's a interval question
5+
* After sorting using the end point...
6+
* .. we need to start picking each intervals from the first and not end
7+
* ... as picking from first and comparing will give us result in minimum arrows
8+
*
9+
*/
10+
class Solution {
11+
public:
12+
int findMinArrowShots(vector<vector<int>>& points) {
13+
sort(points.begin(), points.end(), [](const vector<int>& x, const vector<int>& y){
14+
return y[1]>x[1];
15+
});
16+
17+
int result=0;
18+
for(int i=0;i<points.size();) {
19+
int currInd=i+1;
20+
result++;
21+
while(currInd<points.size() && points[currInd][0]<=points[i][1]) {
22+
currInd++;
23+
}
24+
i=currInd;
25+
}
26+
return result;
27+
}
28+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://leetcode.com/problems/daily-temperatures
2+
3+
/**
4+
* Undestand pattern - find next > so monotonic stack
5+
* what to keep in .second -> index so can relate and give answer
6+
*/
7+
class Solution {
8+
public:
9+
stack<pair<int, int>> store;
10+
11+
int getDayDiff(int item, int ind) {
12+
pair<int, int> newItem = {item, ind};
13+
int toReturn = 0;
14+
while(!store.empty() && store.top().first<=item) {
15+
store.pop();
16+
}
17+
int diff=0;
18+
if(!store.empty())
19+
diff = store.top().second - ind;
20+
store.push(newItem);
21+
return diff;
22+
}
23+
24+
vector<int> dailyTemperatures(vector<int>& temperatures) {
25+
vector<int> result;
26+
for(int i=temperatures.size()-1;i>=0;i--)
27+
result.push_back(getDayDiff(temperatures[i], i));
28+
reverse(result.begin(), result.end());
29+
return result;
30+
}
31+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// https://leetcode.com/problems/online-stock-span
2+
3+
/**
4+
* Pattern - find continuous last n item small or equal - monotonic stack
5+
* understanding what to keep in .second here....
6+
*/
7+
class StockSpanner {
8+
public:
9+
10+
stack<pair<int, int>> store;
11+
12+
StockSpanner() {
13+
}
14+
15+
int restructureStore() {
16+
pair<int,int> topItem = store.top();
17+
store.pop();
18+
while(!store.empty() && store.top().first<=topItem.first) {
19+
pair<int, int> restTopItem = store.top();
20+
topItem.second = topItem.second + restTopItem.second;
21+
store.pop();
22+
}
23+
store.push(topItem);
24+
return topItem.second;
25+
}
26+
27+
int next(int price) {
28+
store.push({price, 1});
29+
return restructureStore();
30+
}
31+
};
32+
33+
/**
34+
* Your StockSpanner object will be instantiated and called as such:
35+
* StockSpanner* obj = new StockSpanner();
36+
* int param_1 = obj->next(price);
37+
*/

0 commit comments

Comments
 (0)