当前位置:网站首页>LeetCode笔记:Weekly Contest 299
LeetCode笔记:Weekly Contest 299
2022-06-29 14:49:00 【Espresso Macchiato】
1. 题目一
给出题目一的试题链接如下:
1. 解题思路
这一题思路上还是比较直接的,直接按照题意检查一下对角元素和非对角元素即可。
2. 代码实现
给出python代码实现如下:
class Solution:
def checkXMatrix(self, grid: List[List[int]]) -> bool:
n = len(grid)
for i in range(n):
for j in range(n):
if i == j:
if grid[i][j] == 0:
return False
elif i + j == n-1:
if grid[i][j] == 0:
return False
else:
if grid[i][j] != 0:
return False
return True
提交代码评测得到:耗时644ms,占用内存15.1MB。
2. 题目二
给出题目二的试题链接如下:
1. 解题思路
这一题我的思路就是一个简单的动态规划。
2. 代码实现
给出python代码实现如下:
class Solution:
def countHousePlacements(self, n: int) -> int:
MOD = 10**9 + 7
@lru_cache(None)
def dp(idx, pre):
if idx >= n:
return 1
res = 0
for i in range(4):
if i & pre == 0:
res += dp(idx+1, i)
return res % MOD
return dp(0, 0)
提交代码评测得到:耗时2850ms,占用内存473.8MB。
3. 算法优化
看了一下其他人提交的结果,发现有一个更加优雅的解法。
显然两岸的排布方式是独立的,因此,我们只需要考察一侧的可能排布方式,然后平方一下就是我们最终需要的答案。
而关于如何计算一侧的可能排布方式,其事实上恰好就是一个斐波那契数列,因此,其答案也是简单的。
摘录对应的python代码实现如下:
mod = 10**9 + 7
@lru_cache(10**5 + 1)
def fib(n):
if n <= 0:
return 1
return (fib(n - 1) + fib(n - 2)) % mod
class Solution:
def countHousePlacements(self, n: int) -> int:
return fib(n)**2 % mod
3. 题目三
给出题目三的试题链接如下:
1. 解题思路
这一题的思路来说分两个阶段,第一阶段就是计算一个累积数组,分别即为 s 1 s_1 s1和 s 2 s_2 s2。
由此一来,我们要计算的最终答案可以表示为如下两个结果之一:
{ m a x i , j ( s 1 ( n ) + ( s 2 ( j ) − s 2 ( i ) ) − ( s 1 ( j ) − s 1 ( i ) ) ) m a x i , j ( s 2 ( n ) + ( s 1 ( j ) − s 1 ( i ) ) − ( s 2 ( j ) − s 2 ( i ) ) ) \left\{ \begin{aligned} max_{i, j}(s_1(n) + (s_2(j) - s_2(i)) -(s_1(j) - s_1(i))) \\ max_{i, j}(s_2(n) + (s_1(j) - s_1(i)) -(s_2(j) - s_2(i))) \end{aligned} \right. { maxi,j(s1(n)+(s2(j)−s2(i))−(s1(j)−s1(i)))maxi,j(s2(n)+(s1(j)−s1(i))−(s2(j)−s2(i)))
改写上两式即有:
{ m a x i , j ( s 1 ( n ) + ( s 2 ( j ) − s 1 ( j ) ) − ( s 2 ( i ) − s 1 ( i ) ) ) m a x i , j ( s 2 ( n ) + ( s 1 ( j ) − s 1 ( j ) ) − ( s 1 ( i ) − s 2 ( i ) ) ) \left\{ \begin{aligned} max_{i, j}(s_1(n) + (s_2(j) - s_1(j)) -(s_2(i) - s_1(i))) \\ max_{i, j}(s_2(n) + (s_1(j) - s_1(j)) -(s_1(i) - s_2(i))) \end{aligned} \right. { maxi,j(s1(n)+(s2(j)−s1(j))−(s2(i)−s1(i)))maxi,j(s2(n)+(s1(j)−s1(j))−(s1(i)−s2(i)))
因此,我们只需要维护 s 1 − s 2 s_1 - s_2 s1−s2以及 s 2 − s 1 s_2 - s_1 s2−s1的两个单调数组即可完成答案的求解。
2. 代码实现
给出python代码实现如下:
class Solution:
def maximumsSplicedArray(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
s1, s2 = [0] + list(accumulate(nums1)), [0] + list(accumulate(nums2))
res = max(s1[-1], s2[-1])
delta = [x-y for x, y in zip(s1, s2)]
s = []
for x in delta:
while s != [] and x <= s[-1]:
s.pop()
s.append(x)
res = max(res, s2[-1] + s[-1] - s[0])
delta = [x-y for x, y in zip(s2, s1)]
s = []
for x in delta:
while s != [] and x <= s[-1]:
s.pop()
s.append(x)
res = max(res, s1[-1] + s[-1] - s[0])
return res
提交代码评测得到:耗时1914ms,占用内存40.2MB。
4. 题目四
给出题目四的试题链接如下:
这一题我放弃了,实在是没有啥很好的思路,唉……
边栏推荐
- Create an API rapid development platform, awesome!
- Uncover the practice of Baidu intelligent test in the field of automatic test execution
- Differential equations of satellite motion
- Jet hydrogen technology rushes to the scientific innovation board: SAIC Group is the major shareholder to raise 1.06 billion yuan
- synchronized 与多线程的哪些关系
- ROS 笔记(10)— launch 文件启动
- 信息学奥赛一本通1000:入门测试题目
- For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?
- Informatics Olympiad all in one 1003: aligned output
- ROS notes (10) - Launch file startup
猜你喜欢

Netease strict selection offline data warehouse quality construction practice

技术沟通遇到3个为什么背后的逻辑

他山之石 | 丁香园 医疗领域图谱的构建与应用

又拍云 Redis 的改进之路

Implementing redis distributed locks using custom annotations

What is the relationship between synchronized and multithreading

MCS:多元随机变量——多项式分布

Lumiprobe 点击化学丨非荧光叠氮化物:叠氮化物-PEG3-OH

携程季报图解:净营收41亿 与疫情前相比已被“腰斩”

Lumiprobe click chemistry - non fluorescent azide: azide-peg3-oh
随机推荐
Chapter IX app project test (the end of this chapter)
Huashu high tech rushes to the scientific innovation board: the actual controller xuxiaoshu and his son, who plan to raise 660million yuan, are both American nationals
word如何自动生成目录
西北工业大学遭境外电邮攻击
【牛客网刷题系列 之 Verilog快速入门】~ 移位运算与乘法
Biovendor free light chain( κ and λ) Chemical properties of ELISA Kit
真正的软件测试人员 =“半个产品+半个开发”?
Indice d'évaluation du logiciel hautement simultané (site Web, interface côté serveur)
Whitelabel error page access
使用自定义注解实现Redis分布式锁
信息学奥赛一本通2062:电影票
Lumiprobe click chemistry - non fluorescent azide: azide-peg3-oh
China soft ice cream market forecast and investment prospect research report (2022 Edition)
揭秘百度智能测试在测试自动执行领域实践
konva系列教程4:图形属性
Asynchronous artifact completable future
高並發軟件(網站,服務器端接口)的評價指標
Informatics Olympiad all in one 1194: mobile route
Lumiprobe deoxyribonucleic acid alkyne DT phosphimide
go学习(四、面向接口)