Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Daily Temperatures

Posted on February 17, 2018July 26, 2020 by braindenny

Daily Temperatures



Similar Problems:

  • LeetCode: Leetcode: Next Greater Element I
  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #monotone

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

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


## https://code.dennyzhang.com/daily-temperatures
## Basic Ideas: Monotonous stack can help us find first largest element in O(n) time complexity.
##
##              Descending stack: find the next bigger nubmer for each element
##
##              For any given number, if we haven't met the bigger number. We push it to the stack
##              If we pop out one element, we do find a bigger number than this element.
##
## Complexity: Time O(n), Space O(n)
class Solution(object):
    def dailyTemperatures(self, temperatures):
        """
        :type temperatures: List[int]
        :rtype: List[int]
        """
        length = len(temperatures)
        res = [0]*length
        stack = []
        for i in xrange(length):
            # If current number is bigger, we solved the previous puzzles
            while len(stack) != 0 and temperatures[i] > temperatures[stack[-1]]:
                k = stack.pop()
                # t[i] is the next bigger number than t[k]
                res[k] = i-k
            stack.append(i)
        return res
linkedin
github
slack

Post Views: 6
Posted in AmusingTagged #classic, #codetemplate, #inspiring, monotone

Post navigation

LeetCode: Second Degree Follower
LeetCode: Palindrome Number

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.