当前位置:网站首页>Leetcode: Game 302 (summary)
Leetcode: Game 302 (summary)
2022-07-20 10:04:00 【Starry sky and bright moon】
This game uses C++ I wrote two questions , Used after the game python I have gained a lot from the supplementary questions , I learned a little about python Built in functions for . Method learning comes from other excellent bloggers .
6120. How many pairs can an array form (A topic )
Ideas
Method 1 : I wrote my own algorithm , First pair nums Sort , Then traverse the array to find whether the adjacent positions are the same .
Method 2 : Count the number of times each number appears nums, And then nums Divide 2 Add up and get , Is the first answer , Unmatched use nums Subtract twice the matching logarithm from the length of .
Code
class Solution:
def numberOfPairs(self, nums: List[int]) -> List[int]:
cnt = Counter(nums)
pairs = sum(num // 2 for num in cnt.values())
return [pairs, len(nums) - pairs * 2]
6164. The maximum sum of digits and equal pairs (B topic )
Ideas
1. Group by digits , Key memory digits and , Value store itself
2. Maintain the first two numbers with a large root heap
Code
class Solution:
def maximumSum(self, nums: List[int]) -> int:
groups = defaultdict(list)
for num in nums:
s = 0
n = num
while n:
s += n % 10
n //= 10
if len(groups[s]) < 2 :
heappush(groups[s], num)
else:
heappushpop(groups[s], num) # First pop up and press in
ans = -1
for g in groups.values():
if len(g) > 1:
ans = max(ans, g[-1] + g[-2])
return ans
6121. Query the number K Small numbers (C topic )
Ideas
Use the principle of Radix sorting to sort .
First we need to use zip() Function to take iteratable objects as parameters , Package the corresponding elements in the object into tuples , And then return the objects made up of these tuples .
And then use zip Function will nums and queries Save the subscript of the array , Sort , You have to use it here sorted Stable sequencing .
Then use the idea of cardinality to sort queries Of trim Sort ,nums Sort from the last column , Then find the corresponding trim, Take second place k The following table of small numbers .
Finally, save these subscripts to ans Array returns .
Code
class Solution:
def smallestTrimmedNumbers(self, nums: List[str], queries: List[List[int]]) -> List[int]:
#
qs = sorted(zip(queries, range(len(queries))), key=lambda q: q[0][1]) # Sort according to the columns of the two-dimensional array
a = sorted(zip(nums, range(len(nums))), key=lambda t: t[0][-1]) # Sort by the last character
j = 2 # j Record the current row of string
ans = [0] * len(queries) # Define an answer array
for (k, trim),i in qs:
while j <= trim: # Rank at trim individual
a.sort(key=lambda t:t[0][-j]) # Countdown j Sort columns
j += 1 # Next time
ans[i] = a[k - 1][1] # Record the... After sorting k Small subscript
return ans
6122. The minimum number of deletions that make the array divisible (D topic )
Ideas
First seek numsDivide Maximum common divisor of g, And then in nums Find the smallest one that can be g Divisible number , recorded , The number of deletions is nums Middle ratio g Small number .
We need to use reduce function ,reduce function , Put the first two operations in the first set , And then calculate the result with the third one .
Code
class Solution:
def minOperations(self, nums: List[int], numsDivide: List[int]) -> int:
g = reduce(gcd, numsDivide)
mn = min((num for num in nums if g % num == 0), default = 0)
if mn == 0:
return -1
return sum(num < mn for num in nums)
边栏推荐
- 代码审计之若依系统
- 乐山师范程序设计大赛2020-G: 最大公约数【思维】
- What about the software problem of Quanzhi a40i network card
- 我的2020年线上的夏令营总结
- How to test the availability and stability of EIM bus
- 统计实验数据命名,数据命名规则like:d8i8.txt
- Qt解决大量重复数据的操作
- 乐山师范程序设计大赛2020- H: 最小公倍数【求因子个数】
- Leetcode:905. 按奇偶排序数组【双指针+三种语言实现】
- Hard core strength! Feiling Ti Sitara am62x series-335x classic continuation
猜你喜欢
FPGA majority voter (including code)
离散数据(数组)的过零位置搜索
vulnhub 靶機 Jangow: 1.0.1
Secure Code Warrlor学习记录(四)
关于浮点数的剪不断理还乱
Master Karnaugh map simplification in one minute
How to test the availability and stability of EIM bus
Solution of STM32 cubeide breakpoint failure
详细讲解JS中的加法(+)运算,基本数据类型相加,引用数据类型相加底层的运算规则,[]+{},{}+[]
内网渗透隧道技术的相关知识
随机推荐
Many new features of ktor2.0 were thought of a year ago and have been implemented in the project
Leshan normal programming competition 2020-e: divide stones [01 backpacks]
乐山师范程序设计大赛2020- H: 最小公倍数【求因子个数】
使用google cloud部署基于flask的网站
二分查找 mid值溢出问题
Allegro cannot display drill legend information when adding drill legend
Openstack uses dashboard to perform image operations, manage instances, and other operations
2022-7-8 第八小组 顾宇佳 拓展训练
Leshan normal programming competition 2020-a: good pairs
Vulnhub target jangow: 1.0.1
乐山师范程序设计大赛2020-D: 后缀语言【STL】
Leetcode:14. 最长公共前缀【思维+排序】
Detailed explanation of the principle of triode series linear voltage stabilizing circuit and Multisim Simulation
2022-7-12 第八小组 顾宇佳 (Js)
my homework 模板 latex版本
i. Mx8mp development board porting USBWiFi rtl8192eu driver
After ise14.7 was installed in win10 system, the software flashed back and the driver could not be installed normally
RISC_V交叉编译环境(国内镜像 避坑)
The third "intelligence Cup" National College Students' IT skills competition (solution to group B of the preliminary competition)
FPGA network port implementation and detailed explanation (3)