当前位置:网站首页>LeetCode:第302场周赛(总结)
LeetCode:第302场周赛(总结)
2022-07-19 05:14:00 【星空皓月】
这场比赛用C++写了两个题,在赛后用python补题收获很多,学习了一些python的内置函数。方法学习源于其他优秀的博主。
6120. 数组能形成多少数对(A题)
思路
方法一:我自己写的算法,先对nums排序,之后遍历数组找相邻的位置是否相同。
方法二:统计每个数出现的次数nums,然后将nums除以2再累加得到,就是求的第一个答案,未匹配的用nums的长度减去匹配对数的两倍即可。
代码
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. 数位和相等数对的最大和(B题)
思路
1.按数位分组,键存数位和,值存本身
2.用大根堆来维护前两大的数
代码
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) # 先弹出压入
ans = -1
for g in groups.values():
if len(g) > 1:
ans = max(ans, g[-1] + g[-2])
return ans
6121. 裁剪数字后查询第 K 小的数字(C题)
思路
利用基数排序的原理进行排序。
首先我们要用到zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象。
然后用zip函数将nums和queries数组的下标存起来,进行排序,这里一定要用sorted稳定的排序。
然后用基数排序的思想先对queries的trim进行排序,nums从最后一列开始进行排序,然后找到对应的trim,再取第k小的数的下表。
最后将这些下标存到ans数组中返回。
代码
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]) # 按照二维数组的列进行排序
a = sorted(zip(nums, range(len(nums))), key=lambda t: t[0][-1]) # 按照最后一个字符排序
j = 2 # j记录当前排到字符串第几列
ans = [0] * len(queries) # 定义一个答案数组
for (k, trim),i in qs:
while j <= trim: # 排到第trim个
a.sort(key=lambda t:t[0][-j]) # 对倒数第j列进行排序
j += 1 # 再下一趟
ans[i] = a[k - 1][1] # 记录排序后的第k小的下标
return ans
6122. 使数组可以被整除的最少删除次数(D题)
思路
先求numsDivide的最大公约数g,然后在nums中找到最小的能被g整除的数,记录下来,删除的个数就是nums中比g小的数。
这里要用到reduce函数,reduce函数,将第一个集合中的前两个运算,然后再将运算结果与第三个进行运算。
代码
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)
边栏推荐
- Default password of common devices
- 提升开发效率的 Chrome 开发者工具快捷键参考
- Stm32f4 uses advanced timer to output PWM wave (including code)
- i.MX8MP开发板移植USBWIFI RTL8192EU驱动
- C语言数据类型及typedef下的uint8_t / uint32_t
- Vivado2018.2 error reporting and solution records
- Experience of installing ROS in Jetson nano (failed)
- Allegro (cadence)导出gerber文件步骤
- 雷达基础系列文章之五:雷达调制样式的功能
- Solution of STM32 cubeide breakpoint failure
猜你喜欢
代码审计之oasys系统
vulnhub 靶機 Jangow: 1.0.1
[vscade configuration markdown environment] user friendly~
Detailed explanation of the principle of triode series linear voltage stabilizing circuit and Multisim Simulation
How to put a "platform" into a small box? (I) scheme comparison
1人天搞定9人天的日志接入开发——基于指令集物联网操作系统的项目开发实践
小波分析之多分辨率分解和重构过程详解
i. Mx8mp development board porting USBWiFi rtl8192eu driver
Precautions for the selection and conversion of single power supply operational amplifier and dual power supply operational amplifier and their power supply mode
Vivado2018.2 version with PS side configuration (BD) when calling Modelsim simulation: (vlog-13006) could not find the package (sc_util_v1_0_3_pkg)
随机推荐
单电源运放和双电源运放及其供电方式选择与转换的注意事项
PCL基本操作大全
TypeScript 之泛型
FreeRTOS thread safe and interrupt safe printf implementation
Chrome developer tool shortcut key reference to improve development efficiency
Master Karnaugh map simplification in one minute
Xilinx FPGA key resource evaluation
cadence 17.2 版本的 OrCAD Capture CIS打开时不停地弹出空白网页解决办法
The difference between bitwise negation and logical negation in Verilog language
FPGA decoder + decoder (including code)
Vivado2018.2 error reporting and solution records
FPGA network port implementation and detailed explanation (2)
Stm32f4 uses advanced timer to output PWM wave (including code)
Default password of common devices
ARM中断优先级之理解
First hand evaluation of Reza g2l core board and development board
Allegro添加Drill Legend时不能显示Drill Legend信息之问题
【BOM】初识BOM~
Detailed explanation of multiresolution decomposition and reconstruction of wavelet analysis
Generics of typescript