当前位置:网站首页>Leetcode notes: Weekly contest 300
Leetcode notes: Weekly contest 300
2022-07-05 17:59:00 【Espresso Macchiato】
- Match Links :https://leetcode.com/contest/weekly-contest-300/
1. Topic 1
The link to question 1 is as follows :
1. Their thinking
This question is actually a little long , The idea is very direct , Build a character mapping correspondence according to the meaning of the topic , Then decode it .
2. Code implementation
give python The code implementation is as follows :
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)
The submitted code was evaluated : Time consuming 49ms, Take up memory 13.9MB.
2. Topic two
The link to question 2 is as follows :
1. Their thinking
This problem is not much different from the general solution of rotation matrix , Just fill in the data by constantly changing the direction .
2. Code implementation
give python The code implementation is as follows :
# 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
The submitted code was evaluated : Time consuming 2593ms, Take up memory 66.2MB.
3. Topic three
The link to question 3 is as follows :
1. Their thinking
In fact, the idea of this question is quite direct , It's a recursive algorithm , Then use dynamic programming to optimize the calculation efficiency .
So we just need to write a recursive formula , Its recurrence relation is actually easy to calculate , That is, the number of people who knew the secret the day before minus the number of people who would forget the secret that day , Plus the number of new people who will know the secret that day .
And the number of people who will forget the secret that day is forget On the critical day, the new number of people who know Mimi , Then the number of people added on that day is from the current date delay Day to forget The number of all new people between the critical day .
thus , We can write the final recursive formula .
2. Code implementation
give python The code implementation is as follows :
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)
The submitted code was evaluated : Time consuming 993ms, Take up memory 17.1MB.
4. Topic four
The link to question 4 is as follows :
1. Their thinking
This question is actually more direct than the previous one , It's just a dynamic planning .
The number of routes that can be formed by each point is 1 Add the number of adjacent paths greater than this point .
thus , We can get our final answer quickly .
2. Code implementation
give python The code implementation is as follows :
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
The submitted code was evaluated : Time consuming 3547ms, Take up memory 107.4MB.
边栏推荐
- 图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
- ITK Example
- Leetcode daily question: the first unique character in the string
- 十个顶级自动化和编排工具
- Accuracy of BigDecimal Division
- Clickhouse (03) how to install and deploy Clickhouse
- Use QT designer interface class to create two interfaces, and switch from interface 1 to interface 2 by pressing the key
- 企业数字化发展中的六个安全陋习,每一个都很危险!
- Easynmon Usage Summary
- 神经网络自我认知模型
猜你喜欢

破解湖+仓混合架构顽疾,星环科技推出自主可控云原生湖仓一体平台

Matlab reference

Short the command line via jar manifest or via a classpath file and rerun

To solve the problem of "double click PDF file, pop up", please install Evernote program

leetcode每日一题:字符串中的第一个唯一字符

Leetcode daily question: the first unique character in the string

Thesis reading_ Medical NLP model_ EMBERT

Cmake tutorial step1 (basic starting point)

Daily exercise: a series of dates

GFS distributed file system
随机推荐
7 pratiques devops pour améliorer la performance des applications
较文心损失一点点性能提升很多
钉钉开放平台小程序API的缓存接口都有哪些内容?
登录连接 CDB 和 PDB
Sophon AutoCV:助力AI工业化生产,实现视觉智能感知
Configure pytorch environment in Anaconda - win10 system (small white packet meeting)
Use QT designer interface class to create two interfaces, and switch from interface 1 to interface 2 by pressing the key
How awesome is the architecture of "12306"?
Ten capabilities that cyber threat analysts should have
记一次使用Windbg分析内存“泄漏”的案例
Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀
删除数组中的某几个元素
rsync
Cmake tutorial Step4 (installation and testing)
Which platform of outer disk gold is regular and safe, and how to distinguish it?
Sentinel-流量防卫兵
神经网络自我认知模型
基于YOLOv3的口罩佩戴检测
nacos -分布式事务-Seata** linux安装jdk ,mysql5.7启动nacos配置ideal 调用接口配合 (保姆级细节教程)
「运维有小邓」用于云应用程序的单点登录解决方案