当前位置:网站首页>LeetCode笔记:Biweekly Contest 84
LeetCode笔记:Biweekly Contest 84
2022-08-09 17:15:00 【Espresso Macchiato】
1. 题目一
给出题目一的试题链接如下:
1. 解题思路
这一题思路还是很简单的,用一个字典把两个item列表当中相同val的权重相加即可。
2. 代码实现
给出python代码实现如下:
class Solution:
def mergeSimilarItems(self, items1: List[List[int]], items2: List[List[int]]) -> List[List[int]]:
res = defaultdict(int)
for v, w in items1:
res[v] += w
for v, w in items2:
res[v] += w
res = [[k,v] for k, v in res.items()]
return sorted(res)
提交代码评测得到:耗时137ms,占用内存14.8MB。
2. 题目二
给出题目二的试题链接如下:
1. 解题思路
这一题咋看之下需要一个二重循环,但是事实上我们只需要将数组进行一下变换,将每一个元素减去其位置,那么我们就只要考察相同元素即可了。
所有的好的pair一定都会拥有相同的值,因此我们在所有的pair数当中减去好的那些就能够得到我们的答案了。
2. 代码实现
给出python代码实现如下:
class Solution:
def countBadPairs(self, nums: List[int]) -> int:
n = len(nums)
for i in range(n):
nums[i] -= i
cnt = Counter(nums)
res = n * (n-1) // 2
for v in cnt.values():
res -= v * (v-1) // 2
return res
提交代码评测得到:耗时898ms,占用内存32.9MB。
3. 题目三
给出题目三的试题链接如下:
1. 解题思路
这一题其实非常简单,用一个额外的字典来记录一下每一个任务下次开启的最早时间即可。
2. 代码实现
给出python代码实现如下:
class Solution:
def taskSchedulerII(self, tasks: List[int], space: int) -> int:
nxt = defaultdict(int)
day = 0
for t in tasks:
day += 1 + max(0, nxt[t] - day)
nxt[t] = day + space
return day
提交代码评测得到:耗时1765ms,占用内存31.2MB。
4. 题目四
给出题目四的试题链接如下:
1. 解题思路
这一题其实就是两个关键点:
- 题目给出的操作只能将数字减小,不可能增大,因此我们只需要倒过来看就可以一个循环搞定了;
- 对于任何一个数 y y y,假设其后一个数字是 x x x,有 y = n x + r y = nx + r y=nx+r,那么有两种情况:
- 如果 r = 0 r = 0 r=0,那么将会至少经过 n − 1 n-1 n−1次操作将这个数变成n个x;
- 如果 r ≠ 0 r \neq 0 r=0,那么需要经过 n n n次操作,使得最大的数不大于 x x x,此时能够得到的最小数的最大值为 ⌊ y / ( n + 1 ) ⌋ \lfloor y/(n+1)\rfloor ⌊y/(n+1)⌋
由此,我们就能够给我们最终的解了。
2. 代码实现
给出python代码实现如下:
class Solution:
def minimumReplacement(self, nums: List[int]) -> int:
n = len(nums)
_max = nums[-1]
res = 0
for i in range(n-1, -1, -1):
if nums[i] <= _max:
_max = nums[i]
continue
m = nums[i] // _max
if nums[i] % _max == 0:
res += m-1
else:
res += m
_max = nums[i] // (m+1)
return res
提交代码评测得到:耗时1056ms,占用内存27.8MB。
边栏推荐
- Substrate 源码更新导读八月第1周: 新版事务化存储层启用默认模式, Polkadot v0.9.27发布
- JVM:(八)运行时数据区之方法区
- Prometheus full installation
- URLError: <urlopen error [Errno 11004] getaddrinfo failed>调用seaborn-data无法使用
- ARM 汇编基础
- win10 uwp 获取指定的文件 AQS
- 【ROS2原理9】 QoS - 截止日期、活跃度和寿命
- AI基础环境搭建和设置总文
- A carnival of art and technology, cloud XR supports Anaya 2022 Sandbox Immersive Art Season
- MySQL数据指令
猜你喜欢
安装搭建私有仓库 Harbor
mysql如何查看所有复合主键的表名?
The strongest distributed lock tool: Redisson
leetcode300.最长递增子序列(动态规划)
What are some good open source automation testing frameworks to recommend?
What platform is EPIC?
JSDN博客系统
The senior told me that the MySQL of the big factory is connected through SSH
2022秋招面试宝典,啃完面试稳了
我不写单元测试,被批了
随机推荐
【Pycharm好用功能】
动手学深度学习_全卷积网络 FCN
偷偷盘点一下各大互联网公司的实习薪资
Detailed explanation of JVM memory model and structure (five model diagrams)
50道Redis面试题,来看看你会多少?
Ark: Survival Evolved Open Server Port Mapping Tutorial
AI基础环境搭建和设置总文
mysql生成随机姓名、手机号、日期
approach和method的区别
The senior told me that the MySQL of the big factory is connected through SSH
One-key login principle of local number
Apache Doris 社区 PMC 杨政国:开源项目如何在自身和社区的需求中取得平衡?
The strongest distributed lock tool: Redisson
那些关于DOM的常见Hook封装(二)
JMeter notes 6 | JMeter recording agent (configuration)
国能准能集团研发矿山数字孪生系统 填补国内采矿行业空白
JSDN blog system
史上最全架构师知识图谱
我不写单元测试,被批了
win10 uwp 设置启动窗口大小 获取窗口大小