当前位置:网站首页>LeetCode笔记:Weekly Contest 305
LeetCode笔记:Weekly Contest 305
2022-08-09 17:15:00 【Espresso Macchiato】
0. 小结
这次的比赛挺伤的,上午做昨晚的双周赛倒是挺顺利的,没想到下午做这个周赛就滑铁卢了,最后两题都没有自己搞定,可能因为中午没有午睡终究还是影响到了下午的状态吧……
1. 题目一
给出题目一的试题链接如下:
1. 解题思路
这一题其实就是我们考察每一个元素作为起始元素时能够形成的最长的序列长度。
2. 代码实现
给出python代码实现如下:
class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
n = len(nums)
res = 0
status = [0 for _ in range(n)]
for i in range(n):
if status[i] != 0:
continue
pre, cnt = nums[i], 1
status[i] = 1
for j in range(i+1, n):
if nums[j] == pre + diff:
cnt += 1
pre = nums[j]
status[j] = 1
res += max(0, cnt-2)
return res
提交代码评测得到:耗时84ms占用内存13.9MB。
2. 题目二
给出题目二的试题链接如下:
1. 解题思路
这一题的话我们首先将所有的限制点从树当中删除,然后用一个深度优先遍历即可获得最终的答案。
2. 代码实现
给出python代码实现如下:
class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
restricted = set(restricted)
if 0 in restricted:
return 0
graph = defaultdict(list)
for u, v in edges:
if u in restricted or v in restricted:
continue
graph[u].append(v)
graph[v].append(u)
res = 0
def dfs(u, pre):
nonlocal res
res += 1
for v in graph[u]:
if v == pre:
continue
dfs(v, u)
return
dfs(0, -1)
return res
提交代码评测得到:耗时2704ms,占用内存133.4MB。
3. 题目三
给出题目三的试题链接如下:
1. 解题思路
这一题一开始我想的复杂了,后来看了一下大佬们的解答之后发现就是一个简单的动态规划,就没啥好多说的了……
2. 代码实现
给出python代码实现如下:
class Solution:
def validPartition(self, nums: List[int]) -> bool:
n = len(nums)
@lru_cache(None)
def dp(idx):
if idx >= n:
return True
if idx + 1 < n and nums[idx] == nums[idx+1] and dp(idx+2):
return True
if idx + 2 < n and nums[idx] == nums[idx+1] == nums[idx+2] and dp(idx+3):
return True
if idx + 2 < n and nums[idx+1] == nums[idx]+1 and nums[idx+2] == nums[idx]+2 and dp(idx+3):
return True
return False
return dp(0)
提交代码评测得到:耗时1744ms,占用内存122.6MB。
4. 题目四
给出题目四的试题链接如下:
1. 解题思路
这一题同样是看了大佬们的解答之后做出来的,本质上都是一个简单的动态规划,不过我自己写的遇到了超时,但是大佬们的解答就能够通过,想不通区别在哪里,原则上要遍历的情况都是 O ( 26 n ) O(26n) O(26n),所以想不通差别在哪。
不过思路上倒是没啥好多说了,大佬们的解答中用cnt来记录结尾在字符i的情况下能够形成的最长子序列长度,剩下的自己看一下代码应该就行了,倒是没啥好多说的……
2. 代码实现
给出python代码实现如下:
class Solution:
def longestIdealString(self, s: str, k: int) -> int:
n = len(s)
s = [ord(ch) - ord('a') for ch in s]
cnt = [0 for _ in range(26)]
for ch in s:
cur = 0
for i in range(26):
if abs(ch - i) <= k:
cur = max(cur, cnt[i])
cur += 1
cnt[ch] = max(cnt[ch], cur)
return max(cnt)
提交代码评测得到:耗时4370ms,真用内存15.9MB。
边栏推荐
- Ark: Survival Evolved Open Server Port Mapping Tutorial
- AI基础环境搭建和设置总文
- 为什么修补应用程序漏洞并不容易
- 逻辑越权和水平垂直越权支付篡改,验证码绕过,接口
- QoS - ROS2 principle 9 】 【 deadline, activity and life
- 手写flexible.js的原理实现,我终于明白移动端多端适配
- Logic unauthorized and horizontal and vertical unauthorized payment tampering, verification code bypass, interface
- 搭建Zabbix监控系统
- FAST-LIO2代码解析(三)
- win10 uwp 简单MasterDetail
猜你喜欢

传统数据中台又贵又复杂?何不试一试永久免费的下一代数据中台

Logic unauthorized and horizontal and vertical unauthorized payment tampering, verification code bypass, interface

使用mysql:5.6和 owncloud 镜像,构建一个个人网盘

混动产品助力,自主SUV市场格局迎来新篇章

EPIC是什么平台?

艺术与科技的狂欢,云端XR支撑阿那亚2022砂之盒沉浸艺术季
![[Pycharm easy to use function]](/img/f8/4c131516033286ba8bcb511d395462.png)
[Pycharm easy to use function]

2022秋招面试宝典,啃完面试稳了

进行知识管理的好处有哪些?

【解决】虚拟机VMware通过局域网连接机器人no route to host
随机推荐
总结篇4:redis 核心数据存储结构及核心业务模型实现应用场景
【工业数字化大讲堂 第二十期】制造业数字化能力建设分享,特邀制造业高级咨询顾问 李东老师分享
自学软件测试,学到什么程度可以出去找工作啊?
动态RDLC报表(五)
win10 uwp 活动磁贴
动态RDLC报表(二)
Volatile:JVM 我警告你,我的人你别乱动
苦日子快走开
win10 uwp 改变鼠标
进程的两种创建方式,join方法,进程间的数据隔离,队列,进程间的通信IPC机制,生产者消费者模型,守护进程,僵尸进程,孤儿进程,互斥锁
Discuz!论坛程序安装+模板配置教程
安装搭建私有仓库 Harbor
手写flexible.js的原理实现,我终于明白移动端多端适配
C#介绍及基本数据类型
神秘的程序员(20-30)
InfluxDB语法
QoS - ROS2 principle 9 】 【 deadline, activity and life
LINE Verda Programming Contest (AtCoder Beginner Contest 263) A~E 题解
Self-taught software testing, how far can I go out to find a job?
loadrunner脚本--参数化