当前位置:网站首页>[leetcode weekly replay] 302 weekly 20220717

[leetcode weekly replay] 302 weekly 20220717

2022-07-19 10:51:00 Seven water Shuliang

One 、 Summary of this week's race

  • It's all simulation questions ,wa A crazy ranking o(╥﹏╥)o.
     Insert picture description here

Two 、 [Easy] 6120. How many pairs can an array form

link : 6120. How many pairs can an array form

1. Title Description

 Insert picture description here

2. Thought analysis

grading Easy.
Count the number of occurrences of each number , The same number can be removed once every two ; How many odd numbers remain is the remaining number .

3. Code implementation

class Solution:
    def numberOfPairs(self, nums: List[int]) -> List[int]:
        cnt = Counter(nums)
        ans1 = sum(v&1 for k,v in cnt.items())
        ans0 = sum(v//2 for k,v in cnt.items())
        
        return [ans0,ans1]

3、 ... and 、[Medium] 6164. The maximum sum of digits and equal pairs

link : 6164. The maximum sum of digits and equal pairs

1. Title Description

 Insert picture description here

2. Thought analysis

grading Medium.

  • Use a hash table to record each digit and the associated digit .
  • Traverse this hash table , Update a group of numbers by adding the two largest numbers ans that will do .

3. Code implementation

class Solution:
    def maximumSum(self, nums: List[int]) -> int:
        g=defaultdict(list)
        
        def sum_digit(a):
            return sum(int(i) for i in str(a))
        
        for a in nums:
            g[sum_digit(a)].append(a)
        ans = -1
        for k,v in g.items():
            if len(v)<2:
                continue
            v.sort()
            ans = max(ans,v[-1]+v[-2])
        return ans

Four 、[Medium] 6121. Query the number K Small numbers

link : 6121. Query the number K Small numbers

1. Title Description

 Insert picture description here

2. Thought analysis

grading Medium.
It's a long question , But violent simulation is enough .

3. Code implementation

class Solution:
    def smallestTrimmedNumbers(self, nums: List[str], queries: List[List[int]]) -> List[int]:
        m,n = len(nums),len(nums[0])
        
        ans = []
        for k,t in queries:
            arr = [(num[-t:],i) for i,num in enumerate(nums)]
            arr.sort()
            x = arr[k-1]
            ans.append(x[1])
        return ans

5、 ... and 、[Hard] 6122. The minimum number of deletions that make the array divisible

link : 6122. The minimum number of deletions that make the array divisible

1. Title Description

 Insert picture description here

2. Thought analysis

  • aliquot b All elements in are equal to divisible b The greatest common divisor in gcd.
  • Preprocessing b Of gcd, Yes a After sorting, find the first one that can divide gcd The element position of .

3. Code implementation

class Solution:
    def minOperations(self, nums: List[int], numsDivide: List[int]) -> int:
        n = len(nums)
        d = numsDivide[0]
    
        for i in range(1,len(numsDivide)):
            d = gcd(d,numsDivide[i])
            
        nums.sort()
        ans = 0
        for i,v in enumerate(nums):
            if d%v==0:
                break
            ans += 1
        if ans == n:
            return -1
        else:
            return ans        
原网站

版权声明
本文为[Seven water Shuliang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207171306358262.html