AliAbdulKareem

LeetCode Daily Temperatures

Jan 20th, 2022 (edited)
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. typedef int* Stack; // just a fancier name for an array
  2.  
  3. class Solution {
  4. public:
  5.     vector<int> dailyTemperatures(vector<int>& temperatures) {
  6.      const int N = temperatures.size();    
  7.         vector<int> sol(N,0);
  8.         Stack stack_top = (Stack) alloca(sizeof(int) * (N-1));
  9.         Stack stack_base = stack_top;
  10.         for(long i = 0; i < N - 1;++i)
  11.         {
  12.             if(temperatures[i] < temperatures[i+1])
  13.             {
  14.                 sol[i] = 1;
  15.                  while(stack_base != stack_top && temperatures[*(stack_top-1)] < temperatures[i+1])
  16.                 {
  17.                     sol[*(stack_top -1 )] = i + 1 - *(stack_top - 1);
  18.  
  19.                     if( stack_base != stack_top) --stack_top;
  20.                 }
  21.             }
  22.             else
  23.             {
  24.                 *stack_top++ = i;
  25.             }
  26.         }    
  27.         return sol;
  28.     }      
  29. };
Add Comment
Please, Sign In to add comment