739. Daily Temperatures
package LeetCode_739 import java.util.* /** * 739. Daily Temperatures * https://leetcode.com/problems/daily-temperatures/description/ * * Given a list of daily temperatures T, return a list such 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 of temperatures T = [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]. * */ class Solution { /* * solution: monotonic stack to help us to find the index of first large element, * if current T large than the stack one, calculate the range; * Time complexity:O(n), because each element just access one time; * Space complexity:O(n) * */ fun dailyTemperatures(T: IntArray): IntArray { val result = IntArray(T.size) val stack = Stack<Int>() for (i in T.indices) { /* * for example: [73, 74] * found out 74 large than 73, set the result index is stack.peek(), value is subtraction of two index * */ while (stack.isNotEmpty() && T[i] > T[stack.peek()]) { //if current one large than the stack one, pop it to help keep track next pair val idx = stack.pop() result[idx] = i - idx } stack.push(i) } return result } }
标签:
monotonic stack
, leetcode
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)