LeetCode Contest 308

"Contest308"

Posted by tLLWtG on September 3, 2022

第一次AK力扣周赛!!!

力扣第308场周赛

2022 年 8 月 28 日,tLLWtG 第一次 AK 力扣周赛,超激动!更刺激的是,我在最后一分钟内才提交了最后一题,而且居然一次就 Pass。
简单记录一下心路历程:

  • T1: 第一眼看到 Subsequence 时,真的吓了一跳。第一题就出这么难吗:-(然后条件反射的回想起各种数列 DP 的题。结果认真读完题后发现只是个套壳的排序+前缀和。(因为数据范围小甚至可以用暴力解法)

  • T2: 大概读完题后,觉得栈应该是最优解,但既然用的是 Python3,不妨试试它的 Slice 特性。

  • T3: 看到各种各样的选择分支然后心想,“终于,终于轮到 DP 了吗。。。不枉我刚刷完 21 天的 DP 专项。”但令人失望的是,这又是个模拟题(感觉最近周赛把把都出模拟)。写完后调了一会才提交,最后还是因为欠考虑 WA 了两次。

  • T4: 读完题目后没什么思路,又被 Special Judge 唬到了,想了一会,更加坚定了三题跑路的想法。于是就跑一边玩去了。。。剩二十分钟的时候回来,一看排名 2000+。这么多人AK的嘛?不行不行,再玩下去说不定要掉大分。又读了一遍题。诶这,这,这不就是个拓扑排序吗?!最终在巨紧张的情况下压线提交了。

总结下来,这次有惊有险的完成了个人的第一次周赛 AK,稍微上了点分。只是在完成时间上有点遗憾。

xunzhang

Contest308

周赛题

T1

2389. Longest Subsequence With Limited Sum

  • Difficulty: Easy

You are given an integer array nums of length n, and an integer array queries of length m.
Return an array answer of length m where answer[i] is the maximum size of a subsequence that you can take from nums such that the sum of its elements is less than or equal to queries[i].
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.

  • Example 1:

Input: nums = [4,5,2,1], queries = [3,10,21]
Output: [2,3,4]
Explanation:We answer the queries as follows:
The subsequence [2,1] has a sum less than or equal to 3. It can be proven that 2 is the maximum size of such a subsequence, so answer[0] = 2.
The subsequence [4,5,1] has a sum less than or equal to 10. It can be proven that 3 is the maximum size of such a subsequence, so answer[1] = 3.
The subsequence [4,5,2,1] has a sum less than or equal to 21. It can be proven that 4 is the maximum size of such a subsequence, so answer[2] = 4.

  • Example 2:

Input: nums = [2,3,4,5], queries = [1]
Output: [0]
Explanation: The empty subsequence is the only subsequence that has a sum less than or equal to 1, so answer[0] = 0.

  • Constraints:

n == nums.length
m == queries.length
1 <= n, m <= 1000
1 <= nums[i], queries[i] <= 106

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
    def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        n,m=len(nums),len(queries)
        ans=[0]*m
        nums.sort()
        he=[nums[0]]+[0]*(n-1)
        for i in range(1,n):
            he[i]=he[i-1]+nums[i]
        for i in range(m):
            x=0
            while x<n and he[x]<=queries[i]:
                x+=1
            ans[i]=x
        return ans

T2

2390.Removing Stars From a String

  • Difficulty: Medium

You are given a string s, which contains stars *.
In one operation, you can:
Choose a star in s. Remove the closest non-star character to its left, as well as remove the star itself. Return the string after all stars have been removed.

Note:
The input will be generated such that the operation is always possible.
It can be shown that the resulting string will always be unique.

  • Example 1:

Input: s = “leet*code”
Output: “lecoe”
Explanation: Performing the removals from left to right:
The closest character to the 1st star is ‘t’ in “leet*code”. s becomes “leecode”.
The closest character to the 2nd star is ‘e’ in “leecode”. s becomes “lecode”.
The closest character to the 3rd star is ‘d’ in “lecod
e”. s becomes “lecoe”.
There are no more stars, so we return “lecoe”.

  • Example 2:

Input: s = “erase*****”
Output: “”
Explanation: The entire string is removed, so we return an empty string.

  • Constraints:

1 <= s.length <= 105
s consists of lowercase English letters and stars *.
The operation above can be performed on s.

1
2
3
4
5
6
7
8
9
10
class Solution:
    def removeStars(self, s: str) -> str:
        n=len(s)
        x=0
        while x<len(s):
            if s[x]=="*":
                s=s[:x-1]+s[x+1:]
                x-=2
            x+=1
        return s

T3

2391. Minimum Amount of Time to Collect Garbage

  • Difficulty: Medium

You are given a 0-indexed array of strings garbage where garbage[i] represents the assortment of garbage at the ith house. garbage[i] consists only of the characters ‘M’, ‘P’ and ‘G’ representing one unit of metal, paper and glass garbage respectively. Picking up one unit of any type of garbage takes 1 minute.
You are also given a 0-indexed integer array travel where travel[i] is the number of minutes needed to go from house i to house i + 1.
There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at house 0 and must visit each house in order; however, they do not need to visit every house.
Only one garbage truck may be used at any given moment. While one truck is driving or picking up garbage, the other two trucks cannot do anything.
Return the minimum number of minutes needed to pick up all the garbage.

  • Example 1:

Input: garbage = [“G”,”P”,”GP”,”GG”], travel = [2,4,3]
Output: 21
Explanation:
The paper garbage truck:
1.Travels from house 0 to house 1
2.Collects the paper garbage at house 1
3.Travels from house 1 to house 2
4.Collects the paper garbage at house 2
Altogether, it takes 8 minutes to pick up all the paper garbage. The glass garbage truck:
1.Collects the glass garbage at house 0
2.Travels from house 0 to house 1
3.Travels from house 1 to house 2
4.Collects the glass garbage at house 2
5.Travels from house 2 to house 3
6.Collects the glass garbage at house 3
Altogether, it takes 13 minutes to pick up all the glass garbage. Since there is no metal garbage, we do not need to consider the metal garbage truck.
Therefore, it takes a total of 8 + 13 = 21 minutes to collect all the garbage.

  • Example 2:

Input: garbage = [“MMM”,”PGM”,”GP”], travel = [3,10]
Output: 37
Explanation:
The metal garbage truck takes 7 minutes to pick up all the metal garbage.
The paper garbage truck takes 15 minutes to pick up all the paper garbage.
The glass garbage truck takes 15 minutes to pick up all the glass garbage.
It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage.

  • Constraints:

2 <= garbage.length <= 105
garbage[i] consists of only the letters ‘M’, ‘P’, and ‘G’.
1 <= garbage[i].length <= 10
travel.length == garbage.length - 1
1 <= travel[i] <= 100

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution:
    def garbageCollection(self, garbage: List[str], travel: List[int]) -> int:
        ment,pla,gla=0,0,0
        mentzhan,plazhan,glazhan=0,0,0
        n=len(garbage)
        for i in range(n):
            temp=[0,0,0]
            for x in garbage[i]:
                if x=="M":
                    temp[0]+=1
                elif x=="P":
                    temp[1]+=1
                elif x=="G":
                    temp[2]+=1
            if temp[0]:
                mentzhan=i
                ment+=temp[0]
            if temp[1]:
                plazhan=i
                pla+=temp[1]
            if temp[2]:
                glazhan=i
                gla+=temp[2]
        for i in range(1,n-1):
            travel[i]=travel[i-1]+travel[i]
        time=ment+pla+gla
        if mentzhan:
            time+=travel[mentzhan-1]
        if plazhan:
            time+=travel[plazhan-1]
        if glazhan:
            time+=travel[glazhan-1]
        return time

T4

2392. Build a Matrix With Conditions

  • Difficulty:Hard

You are given a positive integer k. You are also given:
a 2D integer array rowConditions of size n where rowConditions[i] = [abovei, belowi], and a 2D integer array colConditions of size m where colConditions[i] = [lefti, righti].
The two arrays contain integers from 1 to k.
You have to build a k x k matrix that contains each of the numbers from 1 to k exactly once. The remaining cells should have the value 0.
The matrix should also satisfy the following conditions:
The number abovei should appear in a row that is strictly above the row at which the number belowi appears for all i from 0 to n - 1.
The number lefti should appear in a column that is strictly left of the column at which the number righti appears for all i from 0 to m - 1.
Return any matrix that satisfies the conditions. If no answer exists, return an empty matrix.

  • Example 1:

Input: k = 3, rowConditions = [[1,2],[3,2]], colConditions = [[2,1],[3,2]]
Output: [[3,0,0],[0,0,1],[0,2,0]]
Explanation: The diagram above shows a valid example of a matrix that satisfies all the conditions.
The row conditions are the following:
Number 1 is in row 1, and number 2 is in row 2, so 1 is above 2 in the matrix.
Number 3 is in row 0, and number 2 is in row 2, so 3 is above 2 in the matrix.
The column conditions are the following:
Number 2 is in column 1, and number 1 is in column 2, so 2 is left of 1 in the matrix.
Number 3 is in column 0, and number 2 is in column 1, so 3 is left of 2 in the matrix.
Note that there may be multiple correct answers.

  • Example 2:

Input: k = 3, rowConditions = [[1,2],[2,3],[3,1],[2,3]], colConditions = [[2,1]]
Output: []
Explanation: From the first two conditions, 3 has to be below 1 but the third conditions needs 3 to be above 1 to be satisfied. No matrix can satisfy all the conditions, so we return the empty matrix.

  • Constraints:

2 <= k <= 400
1 <= rowConditions.length, colConditions.length <= 104
rowConditions[i].length == colConditions[i].length == 2
1 <= abovei, belowi, lefti, righti <= k
abovei != belowi
lefti != righti

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution:
    def buildMatrix(self, k: int, rowConditions: List[List[int]], colConditions: List[List[int]]) -> List[List[int]]:
        def fun(condi):
            id=defaultdict(int)
            od=defaultdict(list)
            for i,j in condi:
                id[j] +=1
                od[i].append(j)
            que=deque()
            for i in range(1,k+1):
                if id[i]==0:
                    que.append(i)
            ans=list()
            while que:
                n= que.pop()
                ans.append(n)
                for x in od[n]:
                    id[x]-=1
                    if id[x]==0:
                        que.appendleft(x)
            return ans if len(ans) ==k else None
        row= fun(rowConditions)
        col= fun(colConditions)
        if row is None or col is None:
            return list()
        rowid={val:i for i,val in enumerate(row)}
        colid={val:i for i,val in enumerate(col)}
        ans=[[0 for c in range(k)] for r in range(k)]
        for i in range(1,1+k):
            r=rowid[i]
            c=colid[i]
            ans[r][c]=i
        return ans

新勋章

xunzhang

后记

(保留)



ღゝ◡╹)ノ♡ PV: NaN | UV: NaN