zoukankan      html  css  js  c++  java
  • [LeetCode]739. Daily Temperatures

    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].

    Solution

    这个题的题意很容易理解,就是按索引顺序找出每个元素比它自身大的元素离当前元素的距离。

    解法一

    老规矩,先给出一个最简单的解法。这个解法就是遍历数组,针对每个元素从该元素的索引位置开始遍历到数组末尾,直到遇到比自身大的元素的时候返回遍历的次数temp。可以说是很符合直觉的解法,性能当然也是意料之中的糟糕。

    class Solution {
        public int[] dailyTemperatures(int[] T) {
            int[] result=new int[T.length];
            for(int i=0;i<T.length;i++){
                int temp=0;
                for(int j=i+1;j<T.length;j++){
                      temp++;
                    if(T[j]>T[i]){
                        break;
                    }
                    if(j==T.length-1){
                        temp=0;
                    }
                }
                result[i]=temp;
            }
            return result;
        }
    }
    

    该解法性能如下:

    执行用时 : 653 ms, 在所有 Java 提交中击败了5.00%的用户
    内存消耗 :42.8 MB, 在所有 Java 提交中击败了93.00%的用户

  • 相关阅读:
    AC日记——[ZJOI2012]网络 bzoj 2816
    [USACO08FEB]酒店Hotel 线段树
    divisors 数学
    Count on a tree 树上主席树
    STL备忘
    [TJOI2013]松鼠聚会 曼哈顿距离
    斐波那契数列 矩阵乘法优化DP
    [TJOI2013]奖学金 乱搞
    铁轨 清北学堂 线段树
    P3939 数颜色 线段树动态开点
  • 原文地址:https://www.cnblogs.com/rever/p/11614504.html
Copyright © 2011-2022 走看看