From 0706ab98773838fa14997646ff43930d44f0e930 Mon Sep 17 00:00:00 2001 From: Arpit Kumar Jain <46723473+arpitkekri@users.noreply.github.com> Date: Sat, 13 Nov 2021 09:18:06 +0530 Subject: [PATCH] Time: 140 ms (83.34%), Space: 85.1 MB (70.13%) - LeetHub --- daily-temperatures/daily-temperatures.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 daily-temperatures/daily-temperatures.cpp diff --git a/daily-temperatures/daily-temperatures.cpp b/daily-temperatures/daily-temperatures.cpp new file mode 100644 index 0000000..7007da6 --- /dev/null +++ b/daily-temperatures/daily-temperatures.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector dailyTemperatures(vector& temperatures) { + stack> stk; + int n = temperatures.size(); + vector 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; + } +}; \ No newline at end of file