当前位置:网站首页>Write down the daily temperature
Write down the daily temperature
2022-04-22 04:59:00 【Xiaoqiang~】
Writing to remember ~ Daily temperature
0、 Preface
This blog is to buckle 739. Daily temperature Problem solving , The problem is not difficult , I little interesting , But in coding There was a little problem in the process !
Github The relevant content of this blog can be Click here to To view the !
1、 Title Description

2、 Their thinking
2.1 Method 1~ Violent solution
2.1.1 Ideas
This method starts from the first number in the target array , Compare it with the following elements , Just find the subscript difference of the first number larger than him .
This method is easy to implement , But the problem is that my following program will show timeout in some cases ~
So we need to change the way !
2.1.2 Program code
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
vector<int> ans(temperatures.size());
for(int i=0;i<temperatures.size()-1;i++)
{
for(int j=i+1;j<temperatures.size();j++)
{
if(temperatures[j]>temperatures[i])
{
ans[i] = j-i;
break;
}
}
}
return ans;
}
};
2.2 Method 2 ~ Using monotone stack
2.2.1 Ideas
Forward traversal of the target array , For each of these elements temperatures[i], If the stack st It's empty , Will directly i Into the stack , If the stack is not empty , Compare stack top elements st.top() Corresponding temperature temperatures[st.top()] And current temperature temperatures[i] Size , If temperatures[i] > temperatures[st.top()], Set the waiting days corresponding to the top element of the stack to i - st.top(), And push the top element out of the stack . Repeat the above operation until the stack is empty or the temperature corresponding to the top element of the stack is less than or equal to the current temperature .
In fact, at the beginning of this topic, I thought of using stack , But what I want is to keep the temperature value in the stack , Instead of saving the subscript corresponding to the temperature value , This led me to start coding Something's wrong ~
But always come back and say , This question has a somewhat eye opening effect , That's interesting ~
2.2.2 Program code
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
vector<int> ans(temperatures.size());
stack<int> st;
for(int i=0;i<temperatures.size();i++)
{
while(!st.empty() && temperatures[st.top()] < temperatures[i])
{
ans[st.top()] = i-st.top();
st.pop();
}
st.push(i);
}
return ans;
}
};
版权声明
本文为[Xiaoqiang~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210719223611.html
边栏推荐
- JVM - common parameters
- Discussion on data set division
- style/TextAppearance.Compat.Notification.Info) not found.
- Boyun beyondcmp cloud management platform version 5.6 release
- [chestnut sugar GIS] ArcMap - how to combine multiple images into one
- How does docker access redis service 6379 port in the host
- Small bug -- hidden problem of Guide Package
- Learn from me: how to release your own plugin or package in China
- Morphological operation of OpenCV image processing
- Realization of online Preview PDF file function
猜你喜欢

Les racines et le contexte de Carina

Error encountered when installing opencv

【Selenium】UnitTest测试框架的基本应用

2022.04.20华为笔试

2022.04.20 Huawei written examination

【板栗糖GIS】arcmap—如何将多张影像合并成一张

JVM——》CMS

Combined sum leetcode

Logistic regression -- case: cancer classification and prediction

Prediction of KNN Iris species after normalization and standardization
随机推荐
JVM - common parameters
TDD开发模式与DDD开发模式
Introduction to JUnit
C language simple [stack and queue] (bracket matching problem)
Realization of online Preview PDF file function
Autojs automation script how to develop on the computer, detailed reliable tutorial!!!
Cloud network integration - computing power center - roce / rmda and nvme / nvme of
How the CPU calls the process
Boyun beyondcmp cloud management platform version 5.6 release
If you can convert a two-dimensional array into a one-dimensional array gracefully and quickly, or convert a multi-dimensional array into a one-dimensional array
[Reading Notes - > statistics] 07-02 introduction to the concept of discrete probability distribution binomial distribution
POJ - 2456 aggressive cows
Speech feature extraction of emo-db dataset
Performance analysis of PostgreSQL limit
Carina 的根基與誕生背景|深入了解 Carina 系列 第一期
Chapter IV constraints and indexes
[SQL Server accelerated path] database and table (II)
6. Comparable to JMeter Net pressure measurement tool - crank practical chapter - collecting diagnosis tracking information and how to analyze bottlenecks
Chapter I overview of database
Several key points of logistic regression