From ac57b13edba6363222d2168bc22a3246809e3f68 Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Fri, 16 Dec 2022 15:08:10 +0800 Subject: [PATCH] s0739 --- include/s0739_daily_temperatures.hpp | 14 ++++++++++++++ notes/src/stack_and_queue.md | 12 ++++++++++++ src/s0739_daily_temperatures.cpp | 27 +++++++++++++++++++++++++++ tests/s0739_daily_temperatures.cpp | 24 ++++++++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 include/s0739_daily_temperatures.hpp create mode 100644 src/s0739_daily_temperatures.cpp create mode 100644 tests/s0739_daily_temperatures.cpp diff --git a/include/s0739_daily_temperatures.hpp b/include/s0739_daily_temperatures.hpp new file mode 100644 index 0000000..96e60c6 --- /dev/null +++ b/include/s0739_daily_temperatures.hpp @@ -0,0 +1,14 @@ +#ifndef S0739_DAILY_TEMPERATURES_HPP +#define S0739_DAILY_TEMPERATURES_HPP + +#include +#include + +using namespace std; + +class S0739 { + public: + vector dailyTemperatures(vector& temperatures); +}; + +#endif diff --git a/notes/src/stack_and_queue.md b/notes/src/stack_and_queue.md index 3d18945..7a0b669 100644 --- a/notes/src/stack_and_queue.md +++ b/notes/src/stack_and_queue.md @@ -13,6 +13,18 @@ [1047. 删除字符串中的所有相邻重复项](https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/) +### 变体:单调栈 + +栈中的元素单调递增或单调递减。 + +使用场景:通常是一维数组,要寻找任一个元素的右边或者左边第一个比自己大或者小的元素的位置。 + +时间复杂度:O(n) + +经典题目: + +[739. 每日温度](https://leetcode.com/problems/daily-temperatures/) + ## 队列 使用场景: diff --git a/src/s0739_daily_temperatures.cpp b/src/s0739_daily_temperatures.cpp new file mode 100644 index 0000000..819e6dc --- /dev/null +++ b/src/s0739_daily_temperatures.cpp @@ -0,0 +1,27 @@ +#include "s0739_daily_temperatures.hpp" + +vector S0739::dailyTemperatures(vector& temperatures) { + int len = temperatures.size(); + if (len == 0) { + return vector {}; + } else if (len == 1) { + return vector {0}; + } + // 栈中的元素为向量的下标 + // 向量下标对应的元素从栈底到栈顶单调递增 + stack s; + s.push(0); + // 初始化 ans 中的所有元素为 0 + vector ans(len, 0); + for (int i{1}; i < len; ++i) { + while (temperatures[i] > temperatures[s.top()]) { + ans[s.top()] = i - s.top(); + s.pop(); + if (s.empty()) { + break; + } + } + s.push(i); + } + return ans; +} diff --git a/tests/s0739_daily_temperatures.cpp b/tests/s0739_daily_temperatures.cpp new file mode 100644 index 0000000..c8e1f31 --- /dev/null +++ b/tests/s0739_daily_temperatures.cpp @@ -0,0 +1,24 @@ +#include "s0739_daily_temperatures.hpp" + +#include + +TEST(Problem739, Case1) { + vector temperatures{73, 74, 75, 71, 69, 72, 76, 73}; + vector expected{1, 1, 4, 2, 1, 1, 0, 0}; + S0739 solution; + EXPECT_EQ(solution.dailyTemperatures(temperatures), expected); +} + +TEST(Problem739, Case2) { + vector temperatures{30, 40, 50, 60}; + vector expected{1, 1, 1, 0}; + S0739 solution; + EXPECT_EQ(solution.dailyTemperatures(temperatures), expected); +} + +TEST(Problem739, Case3) { + vector temperatures{30, 60, 90}; + vector expected{1, 1, 0}; + S0739 solution; + EXPECT_EQ(solution.dailyTemperatures(temperatures), expected); +}