leetcode739 每日温度

简介: leetcode739 每日温度

不是题解

2de1e93a82084b0e80952e18d0ecd4bc.png

使用单调栈

c++代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int maxn = 100005;
int b[maxn];
int n;
pair<int, int> a[maxn]; // id value
stack<pair<int, int>> p;
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        a[i].first = i;
        cin >> a[i].second;
    }
    for (int i = 1; i <= n; i++)
    {
        if (p.empty())
        {
            p.push(a[i]);
        }
        else
        {
            while (!p.empty())
            {
                if (a[i].second > p.top().second)
                {
                    b[p.top().first] = i - p.top().first;
                    p.pop();
                }
                else
                    break;
            }
            p.push(a[i]);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        cout << b[i] << endl;
    }
}


相关文章
|
1月前
|
存储 Python
【Leetcode刷题Python】739. 每日温度
LeetCode题目“739. 每日温度”的Python解决方案,使用单调栈来高效地计算出每天需要等待多少天才能遇到更暖天气的答案。
23 4
|
4月前
|
算法
代码随想录算法训练营第五十七天 | LeetCode 739. 每日温度、496. 下一个更大元素 I
代码随想录算法训练营第五十七天 | LeetCode 739. 每日温度、496. 下一个更大元素 I
40 3
|
4月前
|
存储 算法
代码随想录算法训练营第五十九天 | LeetCode 739. 每日温度、496. 下一个更大元素 I
代码随想录算法训练营第五十九天 | LeetCode 739. 每日温度、496. 下一个更大元素 I
35 1
|
4月前
|
索引
leetcode代码记录(每日温度
leetcode代码记录(每日温度
27 0
|
4月前
|
容器
代码随想录 Day49 单调栈01 LeetCode LeetCodeT739每日温度 T496 下一个最大元素I
代码随想录 Day49 单调栈01 LeetCode LeetCodeT739每日温度 T496 下一个最大元素I
54 0
|
4月前
|
SQL
leetcode-SQL-197. 上升的温度
leetcode-SQL-197. 上升的温度
33 0
|
4月前
|
索引
leetcode-739:每日温度
leetcode-739:每日温度
38 0
【LeetCode-每日一题】-739-每日温度
【LeetCode-每日一题】-739-每日温度
力扣739 每日温度
力扣739 每日温度
61 0
|
测试技术 数据库
LeetCode(数据库)- 上升的温度
LeetCode(数据库)- 上升的温度
134 0