{"id":3188,"date":"2018-07-16T07:38:53","date_gmt":"2018-07-16T14:38:53","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3188"},"modified":"2018-07-16T07:39:05","modified_gmt":"2018-07-16T14:39:05","slug":"leetcode-739-daily-temperatures","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-739-daily-temperatures\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 739. Daily Temperatures"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a list of daily\u00a0<code>temperatures<\/code>, 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\u00a0<code>0<\/code>\u00a0instead.<\/p>\n<p>For example, given the list\u00a0<code>temperatures = [73, 74, 75, 71, 69, 72, 76, 73]<\/code>, your output should be\u00a0<code>[1, 1, 4, 2, 1, 1, 0, 0]<\/code>.<\/p>\n<p><b>Note:<\/b>\u00a0The length of\u00a0<code>temperatures<\/code>\u00a0will be in the range\u00a0<code>[1, 30000]<\/code>. Each temperature will be an integer in the range\u00a0<code>[30, 100]<\/code>.<\/p>\n<h1><strong>Solution: Stack<\/strong><\/h1>\n<p>Use a stack to track indices of future warmer days. From top to bottom: recent to far away.<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 148 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; dailyTemperatures(vector&lt;int&gt;&amp; temperatures) {\r\n    const int n = temperatures.size();\r\n    stack&lt;int&gt; s;\r\n    vector&lt;int&gt; ans(n);\r\n    for (int i = n - 1; i &gt;= 0; --i) {\r\n      while (!s.empty() &amp;&amp; temperatures[s.top()] &lt;= temperatures[i]) s.pop();\r\n      ans[i] = s.empty() ? 0 : s.top() - i;\r\n      s.push(i);\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a list of daily\u00a0temperatures, produce a list that, for each day in the input, tells you how many days you would have to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,177,180],"class_list":["post-3188","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-medium","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3188"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3188\/revisions"}],"predecessor-version":[{"id":3190,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3188\/revisions\/3190"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}