Advertisement
vaibhav1906

Daily Temperature

Dec 3rd, 2021
1,425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<int> dailyTemperatures(vector<int>& t) {
  4.        
  5.         int n = t.size();
  6.         vector<int> ans(n,0);
  7.         stack<pair<int,int>> st;
  8.        
  9.         for(int i = 0; i<n; i++){
  10.            
  11.             if(st.size()!=0 && t[i]> st.top().first){
  12.                
  13.                 while(st.size()!=0 && t[i]>st.top().first){
  14.                     ans[st.top().second] = i - st.top().second;
  15.                     st.pop();
  16.                 }
  17.             }
  18.            
  19.             st.push({t[i],i});
  20.            
  21.         }
  22.        
  23.         return ans;
  24.        
  25.     }
  26. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement