当前位置:网站首页>Leetcode notes: biweekly contest 70
Leetcode notes: biweekly contest 70
2022-06-12 07:53:00 【Espresso Macchiato】
1. Topic 1
The link to question 1 is as follows :
1. Their thinking
This problem can be realized by using greedy algorithm directly .
First, we sort the prices , Then the two biggest prices must be at their own expense , Then we can get the third highest price candy for free , Repeat the above operation to the final answer .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost = sorted(cost, reverse=True)
cost = [c for i, c in enumerate(cost) if i % 3 != 2]
return sum(cost)
The submitted code was evaluated : Time consuming 40ms, Take up memory 14.4MB.
2. Topic two
The link to question 2 is as follows :
1. Their thinking
First of all, we assume that the first element is 0, So obviously we can recover the complete sequence .
here , We can get the difference between the largest element and the smallest element in this sequence , The difference between the range we give and the difference is our answer .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
n = len(differences)
nums = [0 for _ in range(n+1)]
for i in range(n):
nums[i+1] = nums[i] + differences[i]
_min, _max = min(nums), max(nums)
delta = _max - _min
res = upper - lower - delta + 1
return res if res >= 0 else 0
The submitted code was evaluated : Time consuming 1943ms, Take up memory 28.8MB.
3. Topic three
The link to question 3 is as follows :
1. Their thinking
This question only needs to pass one bfs You can get all the grids you can reach , Then we sort them according to the sorting rules and take the first k Just one element .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def highestRankedKItems(self, grid: List[List[int]], pricing: List[int], start: List[int], k: int) -> List[List[int]]:
n, m = len(grid), len(grid[0])
i, j = start
q = [start + [0]]
reachable = []
seen = set([(i, j)])
while q:
i, j, s = q.pop(0)
if pricing[0] <= grid[i][j] <= pricing[1]:
reachable.append([s, grid[i][j], i, j])
if i-1 >= 0 and grid[i-1][j] != 0 and (i-1, j) not in seen:
q.append((i-1, j, s+1))
seen.add((i-1, j))
if i+1 < n and grid[i+1][j] != 0 and (i+1, j) not in seen:
q.append((i+1, j, s+1))
seen.add((i+1, j))
if j-1 >= 0 and grid[i][j-1] != 0 and (i, j-1) not in seen:
q.append((i, j-1, s+1))
seen.add((i, j-1))
if j+1 < m and grid[i][j+1] != 0 and (i, j+1) not in seen:
q.append((i, j+1, s+1))
seen.add((i, j+1))
reachable = sorted(reachable)[:k]
return [x[2:] for x in reachable]
The submitted code was evaluated : Time consuming 3336ms, Take up memory 68.3MB.
4. Topic four
The link to question 4 is as follows :
1. Their thinking
This question is actually simpler than the previous one , We just need to take every two seats as the boundary , Investigate two room Between plant number ( It might as well be k), Then between the two there is k + 1 k+1 k+1 A method of setting up wall panels , And we multiply all the possibilities to get our final answer .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def numberOfWays(self, corridor: str) -> int:
MOD = 10**9 + 7
corridor = corridor.strip("P")
seat_num = len([ch for ch in corridor if ch == "S"])
if seat_num == 0 or seat_num % 2 != 0:
return 0
i, cnt, n = 0, 0, len(corridor)
res = 1
while i < n:
if corridor[i] == "P":
i += 1
continue
cnt += 1
if cnt % 2 == 0:
j = i+1
while j < n and corridor[j] == "P":
j += 1
res = res * (j-i) % MOD
i = j
else:
i += 1
return res
The submitted code was evaluated : Time consuming 1355ms, Take up memory 16MB.
边栏推荐
- Leverage contextual information
- Compiling principle on computer -- function drawing language (III): parser
- 连接数据库却无法修改数据
- R语言使用RStudio将可视化结果保存为pdf文件(export--Save as PDF)
- Seeking for a new situation and promoting development, the head goose effect of Guilin's green digital economy
- BI技巧丨当月期初
- LeetCode34. 在排序数组中查找元素的第一个和最后一个位置
- Voice assistant - Qu - single entity recall
- 初步认知Next.js中ISR/RSC/Edge Runtime/Streaming等新概念
- Mathematical knowledge - matrix - matrix / vector derivation
猜你喜欢

Voice assistant -- Qu -- semantic role annotation and its application

Bi skills - beginning of the month

Voice assistant - Multi round conversation (theory and concept)

The computer is connected to WiFi but can't connect to the Internet

Some summaries of mathematical modeling competition in 2022

Summary of semantic segmentation learning (I) -- basic concepts

经典论文回顾:Palette-based Photo Recoloring

Scoring prediction problem

20220526 yolov1-v5

Voice assistant -- Qu -- query error correction and rewriting
随机推荐
@Datetimeformat @jsonformat differences
Compiling principle on computer -- functional drawing language (IV): semantic analyzer
Improvement of hash function based on life game
Search and rescue strategy of underwater robot (FISH)
2022r2 mobile pressure vessel filling test question simulation test platform operation
Summary of machine learning + pattern recognition learning (V) -- Integrated Learning
R语言使用RStudio将可视化结果保存为pdf文件(export--Save as PDF)
Pytorch installation (GPU) in Anaconda (step on pit + fill pit)
Scoring prediction problem
qt. qpa. plugin: Could not load the Qt platform plugin “xcb“ in “***“
Process terminated
20220526 损失函数
R语言e1071包的naiveBayes函数构建朴素贝叶斯模型、predict函数使用朴素贝叶斯模型对测试数据进行预测推理、table函数构建混淆矩阵
WEB页面性能优化面试题
Summary of semantic segmentation learning (II) -- UNET network
LeetCode笔记:Weekly Contest 295
Voice assistant -- Qu -- query error correction and rewriting
Voice assistant - those classification models used in the assistant
In depth learning - overview of image classification related models
Voice assistant - Measurement Indicators