当前位置:网站首页>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. 题目四
给出题目四的试题链接如下:
这一题我放弃了,实在是没有啥很好的思路,唉……
边栏推荐
- What is the relationship between synchronized and multithreading
- Netease strict selection offline data warehouse quality construction practice
- China soft ice cream market forecast and investment prospect research report (2022 Edition)
- 二级指针
- Chapter IX app project test (the end of this chapter)
- Solution to the problem that the assembly drawing cannot be recognized after the storage position of SolidWorks part drawing is changed
- Material dynamic self illumination
- 信息学奥赛一本通1194:移动路线
- Lumiprobe deoxyribonucleic acid phosphate CpG 1000 solid carrier
- 【牛客网刷题系列 之 Verilog快速入门】~ 移位运算与乘法
猜你喜欢

Chinese garbled code output from idea output station

Lumiprobe 活性染料丨环炔染料:AF488 DBCO,5 异构体

文本预处理库spaCy的基本使用(快速入门)

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

Lumiprobe click chemistry - non fluorescent azide: azide-peg3-oh

const用法精讲

render后续来了,封装一个表单往表格中添加数据

Analysis of constant current source circuit composed of two NPN tubes

Ink drop typesetting

Create an API rapid development platform, awesome!
随机推荐
Informatics Olympiad all in one 1194: mobile route
How bad can a programmer be?
MySQL的json 数组操作 json_array_append、json_array_insert
Lumiprobe reactive dye miscellaneous dye: BDP FL ceramide
Whitelabel error page access
信息学奥赛一本通1194:移动路线
China arsenic trioxide industry research and future forecast report (2022 Edition)
Lumiprobe 脱氧核糖核酸丨炔烃dT亚磷酰胺
微信公众号—菜单
Lumiprobe click chemistry - non fluorescent azide: azide-peg3-oh
Indice d'évaluation du logiciel hautement simultané (site Web, interface côté serveur)
两个字的名字如何变成有空格的3个字符的名字
Lumiprobe deoxyribonucleic acid alkyne DT phosphimide
Pytorch two-dimensional multi-channel convolution operation method
信息学奥赛一本通1003:对齐输出
华为软件测试笔试真题之变态逻辑推理题【二】华为爆火面试题
BioVendor遊離輕鏈(κ和λ)Elisa 試劑盒的化學性質
Lumiprobe reactive dye cycloalkyne dye: af488 dbco, 5 isomer
关于项目采购管理,这些你需要知道
Yi Ming ang Ke rushed to Hong Kong shares: loss of 730million in the year Lilly and sunshine life insurance were shareholders