<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Greynut on Medium]]></title>
        <description><![CDATA[Stories by Greynut on Medium]]></description>
        <link>https://medium.com/@ashleywang.ads?source=rss-ef89c9dbbd78------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*vFFOClCFFq0X1qXs</url>
            <title>Stories by Greynut on Medium</title>
            <link>https://medium.com/@ashleywang.ads?source=rss-ef89c9dbbd78------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 14:16:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ashleywang.ads/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[[Leetcode ] 739: Daily Temperatures]]></title>
            <link>https://medium.com/@ashleywang.ads/leetcode-739-daily-temperatures-aced04f3fdb1?source=rss-ef89c9dbbd78------2</link>
            <guid isPermaLink="false">https://medium.com/p/aced04f3fdb1</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Greynut]]></dc:creator>
            <pubDate>Wed, 12 Sep 2018 08:09:56 GMT</pubDate>
            <atom:updated>2018-09-13T03:52:55.130Z</atom:updated>
            <content:encoded><![CDATA[<h3>Problem:</h3><p>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.</p><p>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].</p><p><strong>Note:</strong> The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].</p><h3>Thoughts:</h3><p>When given this problem, it is very easy to come up with a brute force sulution: for every day, we check how many days you have to wait until a warmer temperature. Let’s say there’s in total n days. We need to do the check for each day, which is of O(n) complexity and each check operation would take O(n) in the worst case. Therefore, the brute force solution’s complexity is O(n²).</p><p>Now some would say that O(n²) solution doesn’t sound that bad. When I started doing leetcode, I’m always confused if a complexity is good enough for the problem. Usually a CPU can calculate about 1⁰⁸ times in a second. So if your O(n²) is smaller than O(1⁰⁸), then it’s fine. But in this scenario, the complexity is approximately O(1⁰⁹), which might not be acceptable.</p><p>So now we know we wanna improve the algorithm, but where to start?</p><p>One thing that is usually true is: If an algorithm is improvable, then there must be some redundancy in the way we process the data. How to find the redundancy? Let’s make same examples and find the pattern in it.</p><p>Let’s say the temperature is [68, 69, 71, 70, 69, 72, 69, 68, 70].</p><p>For the first day, we can easily find the next day to be the first warmer day. Same for the second day. We are pretty happy about it, since we only need to check once for each day.</p><p>Then some thing weird happens: From the third day to the fifth, the temperature keeps decreasing. For the third day, the first warmer day is the sixth day; For the fourth day, the first warmer day is the sixth day; For the fifth day, the first warmer day is the sixth day.</p><p>Wait, is there some information we can compress? If the weather keeps decreasing, then the first warmer day for the first day is the first warmer day for every day in the decreasing sequence! So once we find the first warmer day, we can find the first warmer day for the whole sequence.</p><p>Wait, Wait, Wait, what about this one?</p><p>[74, 72, 69, 68, 70]</p><p>70 is the next higher temperature for 68 and 69, but not for 74 and 72. We still need to keep searching for the next warmer day for 74, 72, as well as 70. Once we find a warmer day, there are some days we don’t need to keep track of anymore. If we take a look at the tracking list:</p><p>[74, 72]</p><p>[74, 72, 69]</p><p>[74, 72, 69, 68]</p><p>[74, 72, 70]</p><p>What do we find?</p><p>We are maintaining a list, more precisely a stack. And all the temperatures in the stack is in the decreasing order. Iterating through the given list, we compare the current temperature with the temperature on top of the stack (namely the last one in the list), if smaller, we append it to the stack; else, we pop out the stack until it’s empty or the top element is larger than the current value.</p><h3>Solution:</h3><p>As described above, we maintain a decreasing stack while iterating through the list. Strange as it seems, the complexity of this algorithm is O(n). Given n temperatures, each temperature can be operated at most twice: pushed back to the stack or poped out from the stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9kOhn3nFIHtah_hrLYbspA.png" /><figcaption>Solution in Python</figcaption></figure><p>There are a few corner cases we need to consider: what if the stack becomes empty? What would happen if there is no warmer day in the future? What if the input list is empty? Always think about what test cases would you use to test your code and which corner case would case failure in the code.</p><h3>Final words</h3><p>Sometimes in the interview, you might have to cater your solution to different restrictions. What if I tell you that you can not use extra space? It’s all about tradeoff.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aced04f3fdb1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>