aboutsummaryrefslogtreecommitdiff
path: root/other/daily_temps.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'other/daily_temps.cpp')
-rw-r--r--other/daily_temps.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/other/daily_temps.cpp b/other/daily_temps.cpp
new file mode 100644
index 0000000..e244252
--- /dev/null
+++ b/other/daily_temps.cpp
@@ -0,0 +1,28 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ vector<int> dailyTemperatures(vector<int> &temperatures) {
+ int n = temperatures.size();
+ vector<int> nge(n, 0);
+ stack<int> stk{};
+ for (int i = n - 1; i >= 0; --i) {
+ while (!stk.empty() && temperatures[stk.top()] <= temperatures[i])
+ stk.pop();
+ if (!stk.empty())
+ nge[i] = stk.top() - i;
+ stk.push(i);
+ }
+ return nge;
+ }
+};
+
+int main() {
+ Solution obj;
+ vector<int> t1 = {73, 74, 75, 71, 69, 72, 76, 73};
+ vector<int> t2 = {30, 40, 50, 60};
+ vector<int> t3 = {30, 60, 90};
+ obj.dailyTemperatures(t1); // expect 1,1,4,2,1,1,0,0
+ obj.dailyTemperatures(t2); // expect 1,1,1,0
+ obj.dailyTemperatures(t3); // expect 1,1,0
+}