当前位置:网站首页>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.
边栏推荐
- Getting started with Jetson nano Series IV: common skills of NVIDIA Jetson nano
- Topic 1 Single_Cell_analysis(3)
- Arrangement of statistical learning knowledge points gradient descent, least square method, Newton method
- Topic 1 Single_ Cell_ analysis(3)
- Utilize user behavior data
- Exposure compensation, white increase and black decrease theory
- CONDA reports an error when creating a virtual environment, and the problem is solved
- NaiveBayes function of R language e1071 package constructs naive Bayes model, predict function uses naive Bayes model to predict and reason test data, and table function constructs confusion matrix
- The latest hbuilderx editing uni app project runs in the night God simulator
- Windows10 configuration database
猜你喜欢

Latex usage problems and skills summary (under update)

Topic 1 Single_Cell_analysis(4)

Architecture and performance analysis of convolutional neural network

Scoring prediction problem

Voice assistant - Qu - single entity recall

2022 electrician (elementary) examination question bank and simulation examination

Summary of machine learning + pattern recognition learning (V) -- Integrated Learning

Arrangement of statistical learning knowledge points -- maximum likelihood estimation (MLE) and maximum a posteriori probability (map)

Voice assistant -- Architecture and design of Instruction Assistant

Voice assistant -- Qu -- semantic role annotation and its application
随机推荐
20220607. 人脸识别
二、八、十、十六进制相互转换
2021.10.29-30 scientific research log
Latex usage problems and skills summary (under update)
Dynamic simulation method of security class using Matlab based Matpower toolbox
最新hbuilderX编辑uni-app项目运行于夜神模拟器
Meter Reading Instrument(MRI) Remote Terminal Unit electric gas water
Interview questions on mobile terminal, Android and IOS compatibility
Windows10 configuration database
R语言dplyr包mutate_at函数和one_of函数将dataframe数据中指定数据列(通过向量指定)的数据类型转化为因子类型
Some summaries of mathematical modeling competition in 2022
Multithread decompression of tar
谋新局、促发展,桂林绿色数字经济的头雁效应
石油储运生产 2D 可视化,组态应用赋能工业智慧发展
CONDA reports an error when creating a virtual environment, and the problem is solved
Improvement of hash function based on life game (continued 2)
[redistemplate method details]
R语言glm函数构建泊松回归模型(possion)、epiDisplay包的poisgof函数对拟合的泊松回归模型进行拟合优度检验、即模型拟合的效果、验证模型是否有过度离散overdispersion
Exposure compensation, white increase and black decrease theory
Voice assistant - Multi round conversation (process implementation)