zoukankan      html  css  js  c++  java
  • 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
        }
    }
  • 相关阅读:
    C# 利用 Geckofx60 实现下载
    C# 线程 线程池
    C# DateTime 与 String 格式转换
    C# WPF 获取程序路径
    C# 计时器 Timer 介绍
    获取远程图片并把它保存到本地
    php sql 过滤
    PHP如何生成伪静态
    用php获取客户端IP地址的方法
    php过滤危险html代码
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13727304.html
Copyright © 2011-2022 走看看