前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >739. Daily Temperatures

739. Daily Temperatures

作者头像
Dylan Liu
发布2019-07-01 14:06:26
3390
发布2019-07-01 14:06:26
举报
文章被收录于专栏:dylanliudylanliu

Description

Tag:Stack, Hash Table Difficulty: Medium

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

从后向前,将大值压入栈中,在压栈过程中,将小值pop出去,然后依次查找即可。

代码语言:javascript
复制
type ele struct {
     index int
     temperature int
} 
var stack []ele
var top = -1
func dailyTemperatures(T []int) []int {
    size := len(T)
    stack = make([]ele, size)
    result := make([]int, size)
    for i:= size -1; i>=0;i-- {
        result[i] = find(T[i], i)
        push(T[i], i)
    }
    
    return result
}

func find(t int, index int) int {
    for i:=top; i>=0; i-- {
        if stack[i].temperature > t {
            return stack[i].index - index
        }
    }
    return 0
}

func push(t int, i int) {
    for top >=0 && stack[top].temperature <= t {
        top--
    }
    top++
    element := ele{i, t}
    stack[top] = element
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Solution
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档