当前位置:网站首页>LeetCode笔记:Biweekly Contest 83
LeetCode笔记:Biweekly Contest 83
2022-07-26 18:03:00 【Espresso Macchiato】
0. 小结
这周的两个比赛也是有点伤,都是大佬们只需要7、8分钟就能搞定的题目,我硬是两个第四题都没有想到好的思路,最后都是看了其他大佬们的解答才搞定的,然后发现这两题都只需要几行,就很伤……
感觉最近状态真心不太好,唉……
1. 题目一
给出题目一的试题链接如下:
1. 解题思路
这一题就是rule based翻译一下题中给出的4个情况即可。
2. 代码实现
给出python代码实现如下:
class Solution:
def bestHand(self, ranks: List[int], suits: List[str]) -> str:
if len(set(suits)) == 1:
return "Flush"
cnt = Counter(ranks).values()
if max(cnt) >= 3:
return "Three of a Kind"
elif max(cnt) == 2:
return "Pair"
else:
return "High Card"
提交代码评测得到:耗时34ms,占用内存13.9MB。
2. 题目二
给出题目二的试题链接如下:
1. 解题思路
这一题其实就是找到原序列当中所有的连续的0的子序列,然后计算一下他们能够构成的子序列的数目,最后把这些数全部相加即可。
2. 代码实现
给出python代码实现如下:
class Solution:
def zeroFilledSubarray(self, nums: List[int]) -> int:
res, cnt = 0, 0
for x in nums:
if x == 0:
cnt += 1
else:
res += cnt * (cnt+1) // 2
cnt = 0
res += cnt * (cnt+1) // 2
return res
提交代码评测得到:耗时1895ms,占用内存24.7MB。
3. 题目三
给出题目三的试题链接如下:
1. 解题思路
这道题的主要难点就在于说需要对每一个数快速地找到其最小的index,因此我们需要额外地对每一个数建立一个有序的index的数组,然后一直维护这个数组即可。
2. 代码实现
给出python代码实现如下:
class NumberContainers:
def __init__(self):
self.nums = []
self.index = defaultdict(list)
def change(self, index: int, number: int) -> None:
idx = bisect.bisect_left(self.nums, (index, -1))
if idx >= len(self.nums) or self.nums[idx][0] != index:
bisect.insort(self.nums, (index, number))
bisect.insort(self.index[number], index)
else:
x = self.nums[idx][1]
self.nums[idx] = (index, number)
bisect.insort(self.index[number], index)
self.index[x].pop(bisect.bisect_left(self.index[x], index))
return
def find(self, number: int) -> int:
return -1 if self.index[number] == [] else self.index[number][0]
提交代码评测得到:耗时2350ms,占用内存58.5MB。
4. 题目四
给出题目四的试题链接如下:
1. 解题思路
这一题其实我一开始没想到,但是看了答案之后发现其实还是挺简单的,其实就是一个简单的构造问题。
我们给出一种构造方式,当所有的数字第一次出现一轮之后,那么对于长度为1的子序列,我们总能够对这个序列实现构造。
假设我们现在已经可以完成长度为 n n n的序列,如果我们在其之后又能够出现一轮所有 1 → k 1 \to k 1→k的数字,那么我们就能够构造出任意一个长度为 n + 1 n+1 n+1的子序列。
反之,如果有一个数没有存在,那么我们总能够找到一个子序列,使得这个序列无法被成功构造。
2. 代码实现
给出python代码实现如下:
class Solution:
def shortestSequence(self, rolls: List[int], k: int) -> int:
seen = set()
res = 1
for x in rolls:
seen.add(x)
if len(seen) == k:
res += 1
seen = set()
return res
提交代码评测得到:耗时1503ms,占用内存28.3MB。
边栏推荐
- Safer, healthier and without endurance anxiety, Wei brand latte dht-phev is here
- TypeScript阶段学习
- 2022年焊工(初级)操作证考试题库及模拟考试
- MySQL学习笔记-2.如何提高sql语句的查询性能
- Unity 农场 2 —— 种植系统
- Advanced template (runner's notes)
- SSM integration configuration
- [soft exam] soft exam tutorial + real questions over the years
- SSM integration - exception handler and project exception handling scheme
- 2022T电梯修理考试题及在线模拟考试
猜你喜欢

MySQL exercises elementary 45 questions (Unified table)

Learn UML system modeling from me

likeshop外卖点餐系统开源啦100%开源无加密

FTP协议

Redis学习笔记-2.客户端的使用

Huawei cloud · cloud sharing experts~

Likeshop takeout order system is open source, 100% open source, no encryption

CTO will teach you: how to take over his project when a technician suddenly leaves

图解用户登录验证流程,写得太好了!

(ICLR-2022)TADA!用于视频理解的时间自适应卷积
随机推荐
The diagram of user login verification process is well written!
5 best overseas substitutes for WPS Office
Multi thread learning notes -1.cas
Racher deploys kubernetes cluster
立创EDA使用笔记
FTP协议
JS使用readline来实现终端输入数据
How many pairs can an array of leetcode simple questions form
2022年化工自动化控制仪表考题模拟考试平台操作
SD NAND与eMMC优劣势对比
【考研词汇训练营】Day 13 —— reliance,expert,subject,unconscious,photograph,exaggeration,counteract
Is it safe to apply for public REITs account by mobile phone?
Kubectl common commands and simple explanations
How to become an excellent test / development programmer? Focus on planning and then move
Write a starter
Advanced template (runner's notes)
likeshop外卖点餐系统开源啦100%开源无加密
JS刷题计划——链表
MySQL学习笔记-2.如何提高sql语句的查询性能
Have you ever encountered a deadlock problem in MySQL? How did you solve it?