LeetCode Notes: Weekly Contest 216 Contest Record

0. Summary after the game

This time the game really overturned the big car, it was almost impossible, and the reason for the overturned car is very strange: because the topic is too simple. . .

Alas, I took a look. It took more than 8 points to get the first big guy, and then I didn’t think it was particularly clear for the sake of speeding up the results, and then I was wrong 3 times, and the strange thing is that 3 times are very cheating. It took 50 minutes, but it was barely acceptable. The result was 1 hour and 5 minutes. The domestic ranking dropped by nearly 150, and the world ranking dropped by 400+. , Oh, when will my carelessness be completely corrected. . .

Btw, the result of this competition is 434 in China and 1089 in the world. It is very tragic to fail to enter the top 10%. I am really unwilling! ! !

I still can't be anxious in the future, I can't eat hot tofu if I'm anxious, alas. . .


Well, I later found out that I was lucky. My solution to the fourth question was actually flawed. When I was blogging, I sorted out my thoughts and found out. Fortunately, the test samples were not comprehensive enough. I managed to escape. I was lucky. lucky. . .


It’s a good luck. It’s almost impossible. Suddenly I found that Leetcode seemed to have found that the test samples were incomplete, and then I cancelled my fourth question. It was judged that my fourth question was not done, and my ranking dropped to 1000 domestically. The name is near!

It’s pretty straightforward. Let’s not talk about how they discovered the problem of incomplete test samples. If you show me a wrong sample, I can also directly correct it. It’s not a problem, and it’s a mistake in their own work. It is really simple and rude to deal with it directly, and the good mood of the whole day is suddenly ruined, F**K!

1. Topic One

The link to the question given for question one is as follows:

1. Problem-solving ideas

The idea of ​​solving this problem is extremely straightforward, just concatenate the strings in the list and compare the final concatenation results.

2. Code implementation

The final python code is given as follows:

class Solution:
    def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
        return "".join(word1) == "".join(word2)

For a line of code, submit the code for evaluation and get: it takes 44ms and takes up 14.2MB of memory.

2. Topic 2

The link to the test questions for topic two is as follows:

1. Problem-solving ideas

This question can of course be implemented in a recursive way, but it is more straightforward that we directly construct the answer, because obviously the way the final answer is constructed is to fill in as much as possible in the front a.

Suppose athere are xx in the final resultx months,ztherezzz , the other filling characters (exactly one) arey, then there are:

{ x ⋅ 1 + 1 ⋅ y + z ⋅ 26 = k x + 1 + z = n \left\{ \begin{aligned} x \cdot 1 + 1 \cdot y + z \cdot 26 = k \\ x + 1 + z = n \end{aligned} \right. { x1+1Y+with26=kx+1+with=n

The transformation is:
y + 25 ⋅ z = k − n + 1 y + 25 \cdot z = k-n+1Y+25with=kn+1

Among them, the value range of y is 1~26.

Therefore, we can solve for:
{z = ⌊ k − n + 1 25 ⌋ if y> 1 z = ⌊ k − n + 1 25 ⌋ − 1 if y ≤ 1 \begin{cases} z = \lfloor{\frac {k-n+1}{25}}\rfloor & if \ y> 1 \\ z = \lfloor{\frac{k-n+1}{25}}\rfloor -1 & if \ y \leq 1 \end{cases}{ with=25kn+1with=25kn+11if y>1ify 1

2. Code implementation

The python code is given as follows:

class Solution:
    def getSmallestString(self, n: int, k: int) -> str:
        z = (k-n+1) // 25
        y = (k-n+1) % 25 
        if y <= 1 and z > 0:
            y += 25
            z -= 1
        x = n-z-1
        return x*'a' + chr(ord('a')+y-1) + z*'z'

Submit code evaluation and get: It takes 40ms and takes up 15.2MB of memory.

The current optimal code implementation takes 24ms, but looking at it is essentially a direct search, but he seems to have further optimized the way to determine y and z. Interested readers can take a look at it, here It's not unfolding too much.

3. Topic Three

The link to the test questions for topic three is as follows:

1. Problem-solving ideas

The third question is the biggest question I have encountered this time. A huge amount of time was wasted, just to be lazy. As a result, stealing chickens won't make you lose money, alas. . .

The idea of ​​this question is actually very straightforward, which is to traverse each value and consider the changes after each value is deleted.

When each value is deleted, the next value will be added, so the parity of the id of the rear number will be reversed. Therefore, we only need to record the sum of all the same parity numbers in the front and the rear odd parity. Is the sum of numbers equal to the sum of all numbers with different parity in front and the sum of all numbers with same parity in the back.

The only thing you need to pay attention to is the marginal issue. Just be careful about this.

2. Code implementation

The python code is given as follows:

class Solution:
    def waysToMakeFair(self, nums: List[int]) -> int:
        n = len(nums)
        l = [0 for i in range(n+2)]
        r = [0 for i in range(n+2)]
        for i in range(n):
            l[i+2] = l[i] + nums[i]
            r[n-i-1] = r[n-i+1] + nums[n-i-1]
        ans = 0
        for i in range(n):
            if l[i+1]+r[i+2] == l[i] + r[i+1]:
                ans += 1
        return ans

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

The current optimal code implementation takes 1356ms. Looking at his implementation ideas, they should be the same, anyway, they are all O (N) O(N)O ( N ) algorithm complexity, not much to expand.

4. Topic Four

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

1. Problem-solving ideas

Uh, I suddenly found out that luck was better this time. Although the solution given during the game passed the test, it was actually a bit problematic. . .

Alas, luck is better, luck is better. . .

The real solution is not much different from our original idea. First of all, if you want to complete all tasks, then obviously the energy required is at least the energy actually required for all tasks, and then the key is to consider the difference that needs to be made up.

Regarding the selection of the difference, we need to correlate the order of task selection. Obviously, the larger the difference, we need to complete it as soon as possible when the energy is still sufficient, and then under the same difference, the greater the energy is needed, we hope to take advantage As long as the energy is still enough, finish it as soon as possible.

Thus, we sort the tasks, and then calculate the required energy difference.

2. Code implementation

The final code implementation is given as follows:

class Solution:
    def minimumEffort(self, tasks: List[List[int]]) -> int:
        tasks = sorted(tasks, key=lambda x: (x[1] - x[0], x[1]), reverse=True)
        cost = sum(x[0] for x in tasks)
        remain = cost
        for x, y in tasks:
            if remain < y:
                cost += y - remain
                remain = y
            remain -= x
        return cost

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

The current optimal code implementation takes 1248ms. After looking at it, it is almost the same overall, so I won't expand too much.

Guess you like

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