LeetCode notes: Biweekly Contest 67

Posted by dcro2 on Fri, 31 Dec 2021 01:50:56 +0100

1. Topic 1

The test question link given in question 1 is as follows:

1. Problem solving ideas

The idea of this problem is actually good. In fact, it is to find the maximum number of k, and then form a subset for output. However, it should be noted that the returned sequence needs to maintain its original order. Therefore, when sorting, we need to first retain its original location information, and then restore its original sorting according to the location information.

2. Code implementation

The python code is implemented as follows:

class Solution:
    def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
        nums = [(i, k) for i, k in enumerate(nums)]
        nums = sorted(nums, key=lambda x: x[1])[-k:]
        return [x[1] for x in sorted(nums, key=lambda x: x[0])]

The submitted code evaluation shows that it takes 48ms and occupies 14.4MB of memory.

2. Topic 2

The link of the test questions given in question 2 is as follows:

1. Problem solving ideas

Our idea of this problem is also relatively direct, that is, we directly investigate the number of days when the guards on the left and right of each position are no more than the current day, and then only need to return on the days when both the left and right are more than the target time.

2. Code implementation

The python code is implemented as follows:

class Solution:
    def goodDaysToRobBank(self, security: List[int], time: int) -> List[int]:
        n = len(security)
        down = [0 for _ in range(n)]
        up = [0 for _ in range(n)]
        for i in range(n-1):
            if security[i] >= security[i+1]:
                down[i+1] = down[i] + 1
            if security[n-1-i] >= security[n-2-i]:
                up[n-2-i] = up[n-1-i] + 1
        
        is_good = [i for i in range(n) if up[i] >= time and down[i] >= time]
        return is_good

The submitted code evaluation shows that it takes 1120ms and occupies 33.9MB of memory.

3. Topic 3

The test question link for question 3 is as follows:

1. Problem solving ideas

My idea of this problem is also violent, that is, first find the bomb that each bomb can spread through a double cycle, and then find the total number of bombs that can be detonated when each bomb is detonated through a depth first traversal.

2. Code implementation

The python code is implemented as follows:

class Solution:
    def maximumDetonation(self, bombs: List[List[int]]) -> int:
        n = len(bombs)
        cover = defaultdict(list)
        for i, (x, y, r) in enumerate(bombs):
            for j in range(n):
                if j == i:
                    continue
                x1, y1, _ = bombs[j]
                if (x-x1)**2 + (y-y1)**2 <= r*r:
                    cover[i].append(j)
        
        def dfs(u, seen):
            for v in cover[u]:
                if v not in seen:
                    seen.add(v)
                    dfs(v, seen)
            return len(seen)
        
        return max(dfs(i, {i}) for i in range(n))

The submitted code evaluation shows that it takes 784ms and occupies 14.8MB of memory.

4. Topic 4

The test question link given in question 4 is as follows:

1. Problem solving ideas

Frankly speaking, I don't see why it is a hard problem. It can be completed by maintaining an ordered array.

2. Code implementation

The python code is implemented as follows:

class SORTracker:

    def __init__(self):
        self.idx = 0
        self.q = []

    def add(self, name: str, score: int) -> None:
        bisect.insort(self.q, (-score, name))

    def get(self) -> str:
        res = self.q[self.idx][1]
        self.idx += 1
        return res

The submitted code evaluation shows that it takes 1360ms and occupies 37.4MB of memory.