cpp: add «739. Daily Temperatures»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
b71f3d7640
commit
058016d144
1 changed files with 34 additions and 0 deletions
34
cpp/daily-temperatures.cpp
Normal file
34
cpp/daily-temperatures.cpp
Normal 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;
|
||||
}
|
Loading…
Reference in a new issue