LWC 61:739. Daily Temperatures
来源:互联网 发布:2016年11月网络 编辑:程序博客网 时间:2024/05/17 06:55
LWC 61:739. Daily Temperatures
传送门:739. Daily Temperatures
Problem:
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].
思路:
求出当前元素的第一个大于它的位置即可,所以该问题就是nextGreaterElement系列,具体参考博文【 算法细节系列(10):503. Next Greater Element II】
Java版本:
public int[] dailyTemperatures(int[] temperatures) { int n = temperatures.length; int[] ans = new int[n]; Stack<Integer> stack = new Stack<>(); Map<Integer, Integer> mem = new HashMap<>(); for (int i = 0; i < n; ++i) { while (!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]) { int nxt = stack.pop(); mem.put(nxt, i); } stack.push(i); } for (int i = 0; i < n; ++i) { if (mem.containsKey(i)) { ans[i] = mem.get(i) - i; } else ans[i] = 0; } return ans; }
Python 版本:
def dailyTemperatures(self, temperatures): """ :type temperatures: List[int] :rtype: List[int] """ n = len(temperatures) stack = [] map = {} ans = [0] * n for i in range(n): while (len(stack) > 0 and temperatures[stack[-1]] < temperatures[i]): nxt = stack.pop(-1) ans[nxt] = i - nxt stack.append(i) return ans
阅读全文
1 0
- LWC 61:739. Daily Temperatures
- leetcode weekly contest 61(739. Daily Temperatures)
- 739. Daily Temperatures
- [leetcode] 739. Daily Temperatures
- 739. Daily Temperatures
- 739. Daily Temperatures
- Leetcode 739. Daily Temperatures
- LeetCode刷题 | 739. Daily Temperatures
- leetcode:739. Daily Temperatures 单调栈
- LeetCode.739 Daily Temperatures
- Daily Temperatures问题及解法
- LWC 61:741. Cherry Pickup
- LWC 61:738. Monotone Increasing Digits
- LWC 61:740. Delete and Earn
- Daily
- CodinGame: Temperatures 反思
- English daily
- Daily Meeting
- JVM参数设置、分析
- 解决Centos7下Kubernetes(k8s)部署好之后无法访问dashboard
- Nginx多域名配置
- MySQL存储过程与定时任务-介绍与实践
- oracle 嵌套查询
- LWC 61:739. Daily Temperatures
- 欢迎使用CSDN-markdown编辑器
- OpenGL LIGHTING AND ENVIRONMENT MAPPING WITH GLSL
- Redis_集群介绍
- IntelMediaSDK SampleDecode learning-2
- Spring5学习(二)-spring projects之Spring Batch
- Redis实现消息队列
- UIImageResizingModeStretch聊天气泡的图片拉伸
- MyBatis