当前位置:网站首页>LeetCode笔记:Weekly Contest 300
LeetCode笔记:Weekly Contest 300
2022-07-05 17:46:00 【Espresso Macchiato】
1. 题目一
给出题目一的试题链接如下:
1. 解题思路
这一题其实就是题目有点长,思路倒是很直接,按照题意构建一个字符的映射对应关系,然后进行解码就行了。
2. 代码实现
给出python代码实现如下:
class Solution:
def decodeMessage(self, key: str, message: str) -> str:
cipher = {
" ": " "}
idx = 0
for ch in key:
if ch != " " and ch not in cipher.keys():
cipher[ch] = chr(ord('a') + idx)
idx += 1
res = [cipher[ch] for ch in message]
return "".join(res)
提交代码评测得到:耗时49ms,占用内存13.9MB。
2. 题目二
给出题目二的试题链接如下:
1. 解题思路
这一题倒是和一般的旋转矩阵的解法没啥太大的区别,不断地改变方向填充数据即可。
2. 代码实现
给出python代码实现如下:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def spiralMatrix(self, m: int, n: int, head: Optional[ListNode]) -> List[List[int]]:
res = [[-1 for _ in range(n)] for _ in range(m)]
direction = [(0, 1), (1, 0), (0, -1), (-1, 0)]
x, y, idx = 0, 0, 0
while head:
res[x][y] = head.val
nx, ny = x + direction[idx][0], y + direction[idx][1]
if not(0 <= nx < m and 0 <= ny < n and res[nx][ny] == -1):
idx = (idx+1) % 4
nx, ny = x + direction[idx][0], y + direction[idx][1]
x, y = nx, ny
head = head.next
return res
提交代码评测得到:耗时2593ms,占用内存66.2MB。
3. 题目三
给出题目三的试题链接如下:
1. 解题思路
这一题思路上其实还是蛮直接的,就是一个递推算法,然后用动态规划优化计算效率即可。
所以我们只需要写出递推公式,其递推关系其实也好计算,就是在前一天知道秘密的人数减去当天会忘掉秘密的人的数目,再加上当天会新知道秘密的人数。
而当天会忘记秘密的人的数目就是forget临界的那一天新增的知道咪咪的人数,然后当天新增的人数就是从当前日期往前delay天到forget临界的那一天之间所有新增的人的数目。
由此,我们即可写出最终的递推公式。
2. 代码实现
给出python代码实现如下:
class Solution:
def peopleAwareOfSecret(self, n: int, delay: int, forget: int) -> int:
MOD = 10**9 + 7
@lru_cache(None)
def fp(idx):
return np(idx-forget)
@lru_cache(None)
def np(idx):
if idx == 1:
return 1
elif idx <= delay:
return 0
res = 0
for i in range(idx-forget+1, idx-delay+1):
res += np(i)
return res % MOD
@lru_cache(None)
def dp(idx):
if idx <= 0:
return 0
elif idx <= delay:
return 1
return (dp(idx-1) - fp(idx) + np(idx)) % MOD
return dp(n)
提交代码评测得到:耗时993ms,占用内存17.1MB。
4. 题目四
给出题目四的试题链接如下:
1. 解题思路
这一题其实较之上一题更加直接,就是一个动态规划就完事了。
每一个点的可以构成的路线数目就是1加上其邻接的所有大于这个点的路径数目。
由此,我们就可以快速地得到我们的最终答案。
2. 代码实现
给出python代码实现如下:
class Solution:
def countPaths(self, grid: List[List[int]]) -> int:
MOD = 10**9 + 7
n, m = len(grid), len(grid[0])
@lru_cache(None)
def dp(i, j):
res = 1
for x, y in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]:
if 0 <= x < n and 0 <= y < m and grid[x][y] > grid[i][j]:
res += dp(x, y)
return res % MOD
s = sum(dp(i, j) for i in range(n) for j in range(m))
return s % MOD
提交代码评测得到:耗时3547ms,占用内存107.4MB。
边栏推荐
- EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
- IDEA 项目启动报错 Shorten the command line via JAR manifest or via a classpath file and rerun.
- The five most difficult programming languages in the world
- 提高應用程序性能的7個DevOps實踐
- Tencent music launched its new product "quyimai", which provides music commercial copyright authorization
- 使用QT遍历Json文档及搜索子对象
- Why is February 28 in the Gregorian calendar
- tkinter窗口预加载
- mybash
- Please tell me why some tables can find data by writing SQL, but they can't be found in the data map, and the table structure can't be found
猜你喜欢
随机推荐
How awesome is the architecture of "12306"?
Cmake tutorial step1 (basic starting point)
提高应用程序性能的7个DevOps实践
Career advancement Guide: recommended books for people in big factories
7 pratiques devops pour améliorer la performance des applications
网络威胁分析师应该具备的十种能力
This 17-year-old hacker genius cracked the first generation iPhone!
nacos -分布式事务-Seata** linux安装jdk ,mysql5.7启动nacos配置ideal 调用接口配合 (保姆级细节教程)
Read the history of it development in one breath
Daily exercise: a series of dates
Zabbix
LeetCode 练习——206. 反转链表
統計php程序運行時間及設置PHP最長運行時間
Disabling and enabling inspections pycharm
Sentinel-流量防卫兵
Zabbix
EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
Read libco save and restore the on-site assembly code
「运维有小邓」用于云应用程序的单点登录解决方案
mybash









