Skip to content

Commit

Permalink
Time: 140 ms (83.34%), Space: 85.1 MB (70.13%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
arpitkekri committed Nov 13, 2021
1 parent 42fe79b commit 0706ab9
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions daily-temperatures/daily-temperatures.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
stack<pair<int, int>> stk;
int n = temperatures.size();
vector<int> waitDays(n, 0);
for(int i = n-1; i >= 0; i--) {
if(stk.empty())
stk.push({temperatures[i], i});
else {
while(!stk.empty() && stk.top().first <= temperatures[i])
stk.pop();
if(!stk.empty())
waitDays[i] = stk.top().second - i;
stk.push({temperatures[i], i});
}
}
return waitDays;
}
};

0 comments on commit 0706ab9

Please sign in to comment.