【LeetCode】739. Daily Temperatures 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/daily-temperatures/description/
题目描述
Given a list of daily temperatures, produce a list that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.
For example, given the list temperatures = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].
Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].
解题方法
倒序遍历
这个题难在找到下一个比当前气温高的位置和当前位置的差。注意到题目中温度变化范围只有60,而天数的变化范围有30000,所以对温度遍历是可以接受的,对天数遍历不可接受。
所以我们倒序遍历温度,保留每个温度的最新的天数位置,保存在字典中。对当前的温度,我们从字典中找所有比他大的温度的位置,保留最小值。如果没有比他大的,就写入0.
class Solution(object):
def dailyTemperatures(self, temperatures):
"""
:type temperatures: List[int]
:rtype: List[int]
"""
save = {}
answer = []
for day in range(len(temperatures) - 1, -1, -1):
temp = temperatures[day]
save[temp] = day
larger = []
for i in range(temp + 1, 102):
if i in save:
larger.append(save[i] - day)
if larger:
answer.append(min(larger))
else:
answer.append(0)
return answer[::-1]
栈
如果正序遍历的话需要一个栈,栈的操作是这样的:
如果栈是空或者栈顶的元素小于当前元素,那么说明前面的这天的温度小于今天的,所以直接弹出前面这天,并且把他这天的结果设置为和今天的位置差。
需要注意的是,无论当天的温度是高是低,它的结果的确定需要根据后面确定,所以要入栈。
class Solution(object):
def dailyTemperatures(self, T):
"""
:type T: List[int]
:rtype: List[int]
"""
N = len(T)
stack = []
res = [0] * N
for i, t in enumerate(T):
while stack and stack[-1][0] < t:
oi = stack.pop()[1]
res[oi] = i - oi
stack.append((t, i))
return res
C++版本的代码如下:
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& T) {
const int N = T.size();
stack<pair<int, int>> s;
vector<int> res(N);
for (int i = 0; i < N; i++) {
while (!s.empty() && s.top().first < T[i]) {
int io = s.top().second; s.pop();
res[io] = i - io;
}
s.push({T[i], i});
}
return res;
}
};
日期
2018 年 2 月 7 日
2018 年 12 月 7 日 —— 恩,12月又过去一周了
【LeetCode】739. Daily Temperatures 解题报告(Python & C++)的更多相关文章
- LeetCode 739. Daily Temperatures
原题链接在这里:https://leetcode.com/problems/daily-temperatures/description/ 题目: Given a list of daily temp ...
- 739. Daily Temperatures && 单调栈 && Python collections deque
题目大意 给你接下来每一天的气温,求出对于每一天的气温,下一次出现比它高气温的日期距现在要等多少天 解题思路 利用单调栈,维护一个单调递减的栈 将每一天的下标i入栈,维护一个温度递减的下标 若下一个温 ...
- LeetCode 739. Daily Temperatures (每日温度)
题目标签:HashMap 题目给了我们一组温度,让我们找出 对于每一天,要等多少天,气温会变暖.返回一组等待的天数. 可以从最后一天的温度遍历起,从末端遍历到开头,对于每一天的温度,把它在T里面的in ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 739. Daily Temperatures - LeetCode
Question 739. Daily Temperatures Solution 题目大意:比今天温度还要高还需要几天 思路:笨方法实现,每次遍历未来几天,比今天温度高,就坐标减 Java实现: p ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
随机推荐
- kubernetes部署Docker私有仓库Registry
在后面的部署过程中,有很多的docker镜像文件,由于kubernetes是使用国外的镜像,可能会出现下载很慢或者下载不下来的情况,我们先搭建一个简单的镜像服务器,我们将需要的镜像下载回来,放到我们自 ...
- .Net调用Java的实现方法
一. IKVM 1.1下载配置IKVM 1.1.1. 下载路径 http://www.ikvm.net/index.html 1.1.2. 设置路径 解压ikvm-0.42.0.3.zip,并将%IK ...
- day27 网络编程
1.OSI七层协议 1.七层划分为:应用层,表示层.会话层.传输层.网络层.数据链路层.物理层 2.五层划分:应用层.传输层.网络层.数据链路层.物理层 应用层: 表示层: 会话层: 传输层:四层交换 ...
- tomcat在eclipse上发布,Perference下的server找不到解决办法
help--->Install New software得到如下所示 下面work with选项的内容与你的eclipse版本有关 我的eclipse版本为eclipse-java-2019-0 ...
- 【leetcode】208. Implement Trie (Prefix Tree 字典树)
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...
- E面波导和H面波导的问题
我感觉与窄壁平行是E面,反之为H面.通常E面(窄面)是指与电场方向平行的方向图切面(窄面):H面(宽面)是指与磁场方向平行的方向图切面(宽面).E面的意思是... ElevationH面的意思是... ...
- Windows zip版本安装MySQL
Windows --MySQL zip版本安装记录: step1. 官网download zip包:http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5. ...
- 【编程思想】【设计模式】【其他模式】hsm
Python版 https://github.com/faif/python-patterns/blob/master/other/hsm/hsm.py """ Impl ...
- Docker 安装 Oracle12c
为选定需要pull到系统中的数据库镜像 # docker pull sath89/oracle-12c --------sath89/oracle-12c为选定需要pull到系统中的数据库镜像 doc ...
- Spring Cloud中使用Eureka
一.创建00-eurekaserver-8000 (1)创建工程 创建一个Spring Initializr工程,命名为00-eurekaserver-8000,仅导入Eureka Server依赖即 ...