1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00

cpp: add «739. Daily Temperatures»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-01-31 11:55:10 +01:00
parent b71f3d7640
commit 058016d144
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,34 @@
#include <cassert>
#include <vector>
class Solution {
public:
std::vector<int> dailyTemperatures(const std::vector<int> &temperatures) {
std::vector<int> result(temperatures.size(), 0);
std::vector<int> st;
for (auto i = 0u; i < temperatures.size(); ++i) {
while (!st.empty() && temperatures[st.back()] < temperatures[i]) {
result[st.back()] = i - st.back();
st.pop_back();
}
st.push_back(i);
}
return result;
}
};
int main() {
Solution s;
assert((s.dailyTemperatures(std::vector{73, 74, 75, 71, 69, 72, 76, 73}) ==
std::vector{1, 1, 4, 2, 1, 1, 0, 0}));
assert((s.dailyTemperatures(std::vector{30, 40, 50, 60}) ==
std::vector{1, 1, 1, 0}));
assert(
(s.dailyTemperatures(std::vector{30, 60, 90}) == std::vector{1, 1, 0}));
return 0;
}