LeetCode Notes: Weekly Contest 213 Contest Record

0. Summary after the game

This time the competition is still OK in terms of ranking, 324 in China, 977 in the world, and barely squeezed into the top 1000, but the process is very unhappy.

Questions 3 and 4 both thought about divergence at the beginning. As a result, both of them encountered timeout problems at the beginning. Fortunately, after the fourth question was revised, the thinking was changed back, but the third question was almost running out of time when the correct thinking was thought of. In the end, the correct code implementation was not given before the end time.

The biggest pitfall is that I checked after the final conclusion and found that the idea was completely correct. The reason for the error was that the careless code missed a line of code, a line of code, a line of code, a code, a code. . .

Alas, my heart is tired.

1. Topic One

The link to the question one test question is as follows:

1. Problem-solving ideas

This question can actually be difficult, but the question gives a lot of restrictions, abruptly limiting the difficulty to the easy level, then we only need to follow the easy level method to do it:

  • Given an index, find the arr[index]one that starts with the given pieces , compare whether the two are the same, if they are not the same, return false directly, otherwise update the index to the index after adding the piece.

2. Code implementation

The final python code is given as follows:

class Solution:
    def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool:
        cache = {
    
    }
        for x in pieces:
            if x[0] in cache:
                return False
            cache[x[0]] = x
        
        idx = 0
        while idx < len(arr):
            if arr[idx] not in cache:
                return False
            l = len(cache[arr[idx]])
            if arr[idx:idx+l] != cache[arr[idx]]:
                return False
            idx += l
        return True

After submitting the code for evaluation, it took 44ms and took up 14MB of memory.

However, the total amount of code implementation currently submitted is not enough. I don't know if there is a better code implementation for the time being. Interested readers can follow up and pay attention to it here.

2. Topic 2

The link to the second question is given as follows:

1. Problem-solving ideas

This question is a fairly satisfactory dynamic programming question:

  • Looking at the i-th character, if the last vowel character used is k, then the selectable vowel character can only be behind it (including itself), and we can use it one by one.

2. Code implementation

The final python code is given as follows:

class Solution:
    def countVowelStrings(self, n: int) -> int:
        
        @lru_cache(None)
        def dp(idx, c):
            if idx >= n:
                return 1
            elif c == 4:
                return 1
            ans = 0
            for i in range(c, 5):
                ans += dp(idx+1, i)
            return ans
        
        return dp(0, 0)

After submitting the code evaluation, it took 36ms and took up 14.4MB of memory.

The current optimal code implementation takes 28ms, but I don’t understand his ideas too much. Interested readers can study it on their own, and I won’t do more here.

3. Topic Three

The links to the three test questions are as follows:

1. Problem-solving ideas

The first reaction to this question was a simple dynamic programming question. You can write a recurrence formula without thinking about it, and then it will time out. . .

Later, after a careful analysis of the topic, it is obvious that the big trick of the ladder must be used to fight fires. Then, his method of use should be:

  • When the barrier bricks can no longer cross the current barrier, select the barrier with the largest gap from the previously filled barriers, remove the bricks and replace them with a ladder, so as to replenish the bricks for filling until the ladder is exhausted. And when the bricks cannot fill the next barrier, just return to the current position.

2. Code implementation

The python code is given as follows:

class Solution:
    def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int:
        n = len(heights)
        
        idx = 0
        gap = []
        tot_gap = 0
        while idx < n-1:
            if heights[idx+1] <= heights[idx]:
                idx += 1
            else:
                tmp = heights[idx] - heights[idx+1]
                tot_gap += tmp
                heapq.heappush(gap, tmp)
                while tot_gap + bricks < 0 and ladders > 0:
                    ladders -= 1
                    g = heapq.heappop(gap)
                    tot_gap -= g
                if tot_gap + bricks < 0:
                    return idx
                idx += 1
        return idx

After submitting the code for evaluation, it took 468ms and took up 28MB of memory.

There are not enough submission results yet. Therefore, I don't know if there is any more elegant and efficient code at present. Interested readers can follow it by themselves.

4. Topic Four

The link to the test question for question four is as follows:

1. Problem-solving ideas

This question was also a problem at the beginning. At the beginning, I wanted to use the dfs method to traverse all possible paths, and then find the kth instruction to output, the result is hehe, the test sample seems to have only passed 5, right? , Seriously timed out, just can't watch.

Later careful thought for a moment, because it is ordered by characters, so Hsome came in the Vfront, so, as long as we calculate the time when the operation is performed Hafter the operation the number of instructions back, whether we can live that contains instructions can be the first step K The instructions required for each step are derived step by step.

2. Code implementation

Therefore, we only need to follow the above ideas step by step to push to the current operations that need to be performed at each step.

The python code is given as follows:

class Solution:
    def kthSmallestPath(self, destination: List[int], k: int) -> str:
        n, m = destination
        cnm = [[1 for _ in range(m+1)] for _ in range(n+1)]
        for i in range(1, n+1):
            for j in range(1, m+1):
                cnm[i][j] = cnm[i][j-1] * (i+j) // j

        ans = ""
        while m > 0 and k > 0:
            if k > cnm[n][m-1]:
                ans += "V"
                k -= cnm[n][m-1]
                n -= 1
            else:
                ans += "H"
                m -= 1
        return ans + "H" * m + "V" * n

After submitting the code for evaluation, it took 40ms and took up 14.1MB of memory.

The current optimal code implementation takes 36ms, but after looking at his core problem-solving ideas, it is exactly the same as ours, so I won’t do more here.

Guess you like

Origin blog.csdn.net/codename_cys/article/details/109432628