[PYTHON] Let Code Day 50 "739. Daily Temperatures" Starting from Zero

Overview

It seems that coding tests are conducted overseas in interviews with engineers, and in many cases, the main thing is to implement specific functions and classes according to the theme.

As a countermeasure, it seems that a site called Let Code will take measures.

A site that trains algorithmic power that can withstand coding tests that are often done in the home.

I think it's better to have the algorithm power of a human being, so I'll solve the problem irregularly and write down the method I thought at that time as a memo.

Leetcode

Leet Code Table of Contents Starting from Zero

Last time Leet Code Day49 starting from zero "1323. Maximum 69 Number"

Right now, I'm prioritizing the Medium of the Top 100 Liked Questions. I solved all Easy, so if you are interested, please go to the table of contents.

Twitter I'm doing it.

50 times! 50 times!

problem

739. Daily Temperatures The difficulty level is Medium. Excerpt from Top 100 Liked Questions.

The problem is given a list of Ts that stores the temperatures for each day. Design an algorithm that returns a list showing how many days it will take for those elements to reach warmer temperatures than each day of input.

For example, if T = [73, 74, 75, 71, 69, 72, 76, 73], the returned list will be [1, 1, 4, 2, 1, 1, 0, 0]. I will.

It is assumed that the length of temperature is given between [1,30000] and each temperature is given between [30,100].

When you see the problem, what's the first? ?? Why is the output like this? I thought, but when I looked closely, I found that my understanding was low.

It's not about the difference from the next day, but simply how many days later the temperature will exceed that day.

solution

First, I prepared a list with 0s for the length of the element, and I thought it would be easy to understand how to lick the list obediently. If you use stack, it's LIFO, so it will be easier to implement.

In the case of the example, prepare stack and [0] first for the length of T. Let's lick the list from there.

What is important here is what kind of conditioning should be used for assignment, and what kind of processing should be performed to shift the elements.

In this case, only if stack exists and T [stack [-1]] is larger than the element when licking from the front, the element of ʻansis popped and the element It means that you should put the one obtained by subtracting the last element ofstack` from the order of.

Then, if you add an index to stack and repeat until there are no more elements, you can get the required number of days for each schedule in ʻans`.

I wrote the flow so far.

class Solution(object):
    def dailyTemperatures(self, T):
        ans,stack = [0] * len(T),[]
        for i, j in enumerate(T):
            while stack and T[stack[-1]] < j:
                ans[stack.pop()] = i - stack[-1]
            stack.append(i)
        return ans
# Runtime: 548 ms, faster than 32.01% of Python3 online submissions for Daily Temperatures.
# Memory Usage: 17.1 MB, less than 95.07% of Python3 online submissions for Daily Temperatures.

It is the familiar ʻenumerate`. In the for loop, you can get the index number (count, order) at the same time as the element of the iterable object such as a list (array), and it is an excellent thing that you can get both such count and order and assign it to a variable. is.

I had an Easy problem several times, so I used my head like this for the first time in a while. If you think about it carefully, you can think of a distance between two points.

Up to here for this time. Thank you for your hard work.

Recommended Posts

Let Code Day 50 "739. Daily Temperatures" Starting from Zero
Let Code Day58 Starting from Zero "20. Valid Parentheses"
Let Code Day16 Starting from Zero "344. Reverse String"
Let Code Day49 starting from zero "1323. Maximum 69 Number"
Let Code Day89 "62. Unique Paths" Starting from Zero
Let Code Day 55 "22. Generate Parentheses" Starting from Zero
Let Code Day18 starting from zero "53. Maximum Subarray"
Let Code Day 13 "338. Counting Bits" Starting from Zero
Let Code Day 82 "392. Is Subsequence" Starting from Zero
Let Code Day51 Starting from Zero "647. Palindromic Substrings"
Let Code Day 15 "283. Move Zeroes" starting from zero
Let Code Day14 starting from zero "136. Single Number"
Let Code Day 43 "5. Longest Palindromic Substring" Starting from Zero
Let Code Day74 starting from zero "12. Integer to Roman"
Let Code Day 42 "2. Add Two Numbers" Starting from Zero
Let Code Day57 Starting from Zero "35. Search Insert Position"
Let Code Day47 Starting from Zero "14. Longest Common Prefix"
Let Code Day78 Starting from Zero "206. Reverse Linked List"
Let Code Day 44 "543. Diameter of Binary Tree" starting from zero
Let Code Day 64 starting from zero "287. Find the Duplicate Number"
Let Code Day 84 starting from zero "142. Linked List Cycle II"
Let Code Day24 Starting from Zero "21. Merge Two Sorted Lists"
Let Code Day12 Starting from Zero "617. Merge Two Binary Trees"
Let Code Day2 Starting from Zero "1108. Defanging an IP Address"
Let Code Day70 Starting from Zero "295. Find Median from Data Stream"
Let Code Day81 "347. Top K Frequent Elements" Starting from Zero
Let Code Day48 Starting from Zero "26. Remove Duplicates from Sorted Array"
Let Code Day87 Starting from Zero "1512. Number of Good Pairs"
Let Code Day67 Starting from Zero "1486. XOR Operation in an Array"
Let Code Day56 Starting from Zero "5453. Running Sum of 1d Array"
Let Code Day92 Starting from Zero "4. Median of Two Sorted Arrays"
Let Code Day5 starting from zero "1266. Minimum Time Visiting All Points"
Let Code Day 35 "160. Intersection of Two Linked Lists" Starting from Zero
Let Code Day83 Starting from Zero "102. Binary Tree Level Order Traversal"
Let Code Day 27 "101. Symmetric Tree" starting from scratch
Let Code Day 41 "394. Decode String" starting from scratch
Let Code Day 25 "70. Climbing Stairs" starting from scratch
Let Code Day 34 starting from scratch "118. Pascal's Triangle"
Let Code Day20 starting from scratch "134. Gas Station"
Let Code Day 88 "139. Word Break" starting from scratch
Let Code Day 28 "198. House Robber" starting from scratch
Let Code Day 39 "494. Target Sum" starting from scratch
Let Code Day 36 "155. Min Stack" starting from scratch
Let Code Day 17 "169. Majority Element" starting from scratch
Let Code Day 33 "1. Two Sum" starting from scratch
Let Code Day 40 Starting from Zero "114. Flatten Binary Tree to Linked List"
Let Code Day 91 "153. Find Minimum in Rotated Sorted Array" starting from zero
Let Code Day59 starting from zero "1221. Split a String in Balanced Strings"
Let Code Day 11 Starting from Zero "1315. Sum of Nodes with Even-Valued Grandparent"
Let Code Day6 Starting from Zero "1342. Number of Steps to Reduce a Number to Zero"
Let Code Day 23 "226. Invert Binary Tree" starting from scratch
Let Code Day8 starting from scratch "1302. Deepest Leaves Sum"
Let Code Day 22 starting from scratch "141. Linked List Cycle"
Let Code Day 30 starting from scratch "234. Palindrome Linked List"
Let Code Day68 starting from scratch "709. To Lower Case"
Let Code Day73 Starting from Zero "1491. Average Salary Excluding the Minimum and Maximum Salary"
Let Code Day1 Starting from Zero "1389. Create Target Array in the Given Order"
Let Code Day10 Starting from Zero "1431. Kids With the Greatest Number of Candies"
Code wars kata starting from zero
Let Code Day21 Starting from Zero "448. Find All Numbers Disappeared in an Array"
Let Code Day 19 Starting from Zero "121. Best Time to Buy and Sell Stock"