当前位置:网站首页>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. 题目四
给出题目四的试题链接如下:
这一题我放弃了,实在是没有啥很好的思路,唉……
边栏推荐
- Hi,你有一份Code Review攻略待查收
- BioVendor遊離輕鏈(κ和λ)Elisa 試劑盒的化學性質
- Informatics Olympiad all in one 2062: movie tickets
- Lumiprobe click chemistry - non fluorescent alkyne: hexanoic acid STP ester
- 重磅!2022最新SCI影响因子发布,三大名刊NCS及国内期刊TOP10排名有变化 (内附2022年最新影响因子)
- Abnormal logic reasoning problem of Huawei software test written test [2] Huawei hot interview problem
- Evaluation index of high concurrency software (website, server interface)
- [Verilog quick start of Niuke online question series] ~ shift operation and multiplication
- Chapter IX app project test (the end of this chapter)
- Indice d'évaluation du logiciel hautement simultané (site Web, interface côté serveur)
猜你喜欢

Chinese garbled code output from idea output station

What should phpcms do when it sends an upgrade request to the official website when it opens the background home page?

Draw a slash on a plane coordinate

Unity C# 基础复习26——初识委托(P447)

Configuration tutorial for swagger2

【牛客网刷题系列 之 Verilog快速入门】~ 移位运算与乘法

Heavyweight! The latest SCI impact factors were released in 2022, and the ranking of the three famous journals NCS and the top10 of domestic journals has changed (the latest impact factors in 2022 are

微信公众号—菜单

Lumiprobe deoxyribonucleic acid phosphate CpG 1000 solid carrier

EMC surge protection and decoupling design
随机推荐
Lumiprobe reactive dye cycloalkyne dye: af488 dbco, 5 isomer
信息学奥赛一本通1003:对齐输出
材质 动态自发光
信息学奥赛一本通2061:梯形面积
Query function of Excel vlookup
const用法精讲
Ink drop typesetting
wieshark抓包mysql协议简单分析
Lumiprobe click chemistry - non fluorescent azide: azide-peg3-oh
PostgreSQL learning (based on rookie course)
Chinese garbled code output from idea output station
捷氢科技冲刺科创板:拟募资10.6亿 上汽集团是大股东
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
信息学奥赛一本通1001:Hello,World!
Sofaregistry source code | data synchronization module analysis
华理生物冲刺科创板:年营收2.26亿 拟募资8亿
Imitation headline news information DZ template discuz news information business version GBK template source code
卫龙更新招股书:年营收48亿 创始人刘卫平家族色彩浓厚
I want to search the hundreds of nodes in the data warehouse. Can I check a table used in the SQL
Lumiprobe 活性染料丨羧酸:Sulfo-Cyanine7.5羧酸