当前位置:网站首页>Leetcode notes: Weekly contest 295
Leetcode notes: Weekly contest 295
2022-06-12 07:38:00 【Espresso Macchiato】
- Match Links :https://leetcode.com/contest/weekly-contest-295/
1. Topic 1
The link to question 1 is as follows :
1. Their thinking
This question only needs to be correct source String and target String for character statistics , And let's see target The characters are in source A few groups at most .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def rearrangeCharacters(self, s: str, target: str) -> int:
cnt = Counter(s)
res = math.inf
for k,v in Counter(target).items():
res = min(cnt[k] // v, res)
return res
The submitted code was evaluated : Time consuming 31ms, Take up memory 13.8MB.
2. Topic two
The link to question 2 is as follows :
1. Their thinking
This question only needs to identify the price part of the word , Then the price can be converted .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def discountPrices(self, sentence: str, discount: int) -> str:
def is_price(w):
return re.match(r"^\$\d+\.?\d*$", w)
def fn(w):
if is_price(w):
return "$" + "{:.2f}".format(float(w[1:]) * (100 - discount) / 100)
else:
return w
s = [fn(w) for w in sentence.split()]
return " ".join(s)
The submitted code was evaluated : Time consuming 385ms, Take up memory 16.7MB.
3. Topic three
The link to question 3 is as follows :
1. Their thinking
Frankly speaking, I haven't solved this problem , In terms of ideas, there is no clear idea , As a whole, I know whether to use an ordered array , However, how to adaptively adjust to this problem has never been understood , So I won't explain more here , Just translated the words of the big men on the ranking list code.
2. Code implementation
Give the translated python The code is as follows :
class Solution:
def totalSteps(self, nums: List[int]) -> int:
res = 0
s = []
n = len(nums)
l = 0
for i in nums[::-1]:
depth = 0
while s != [] and i > s[-1][0]:
prev, prev_depth = s.pop()
depth += 1 + max(prev_depth - depth - 1, 0)
res = max(res, depth)
s.append((i, depth))
return res
The submitted code was evaluated : Time consuming 1193ms, Take up memory 29.1MB.
4. Topic four
The link to question 4 is as follows :
1. Their thinking
The same question just translated the words of the top leaders on the ranking list code, So I won't teach others here , Interested readers can take a look at it for themselves code, This question code In fact, it is not difficult to understand ……
2. Code implementation
give python The code implementation is as follows :
class Solution:
def minimumObstacles(self, grid: List[List[int]]) -> int:
n, m = len(grid), len(grid[0])
distance = [[math.inf for _ in range(m)] for _ in range(n)]
distance[0][0] = 0
q = [(0, 0)]
def inside(x, y):
return 0 <= x < n and 0 <= y < m
while q:
x0, y0 = q.pop(0)
for x, y in [(x0-1, y0), (x0+1, y0), (x0, y0-1), (x0, y0+1)]:
if not inside(x, y):
continue
if distance[x][y] > distance[x0][y0] + grid[x][y]:
distance[x][y] = distance[x0][y0] + grid[x][y]
if grid[x][y]:
q.append((x, y))
else:
q.insert(0, (x, y))
return distance[-1][-1]
The submitted code was evaluated : Time consuming 8747ms, Take up memory 40MB.
边栏推荐
- Embedded gd32 code read protection
- AcWing——4269校庆
- WEB页面性能优化面试题
- Primal problem and dual problem
- Exposure compensation, white increase and black decrease theory
- AcWing——4268. Sexy element
- Arrangement of statistical learning knowledge points gradient descent, least square method, Newton method
- The function of C language string Terminator
- There is no solid line connection between many devices in Proteus circuit simulation design diagram. How are they realized?
- Utilize user behavior data
猜你喜欢

谋新局、促发展,桂林绿色数字经济的头雁效应

Exploring shared representations for personalized federated learning paper notes + code interpretation

Voice assistant -- vertical class perpetual motion machine -- automated iteration framework

Personalized federated learning using hypernetworks paper reading notes + code interpretation

Hongmeng OS first training

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

Non IID data and continuous learning processes in federated learning: a long road ahead

Chapter 4 - key management and distribution

Voice assistant - potential skills and uncalled call technique mining

謀新局、促發展,桂林綠色數字經濟的頭雁效應
随机推荐
The first demand in my life - batch uploading of Excel data to the database
我人生中的第一个需求——Excel数据批量上传到数据库
‘CMRESHandler‘ object has no attribute ‘_ timer‘,socket. gaierror: [Errno 8] nodename nor servname pro
vscode 1.68变化与关注点(整理导入语句/实验性新命令中心等)
Voice assistant - Qu - ner and intention slot model
Chapter 2 - cyber threats and attacks
2022R2移动式压力容器充装试题模拟考试平台操作
WEB页面性能优化面试题
2022年危险化学品经营单位安全管理人员特种作业证考试题库及答案
VS 2019 MFC 通过ACE引擎连接并访问Access数据库类库封装
Arrangement of statistical learning knowledge points -- maximum likelihood estimation (MLE) and maximum a posteriori probability (map)
20220525 RCNN--->Faster RCNN
R语言dplyr包mutate_at函数和one_of函数将dataframe数据中指定数据列(通过向量指定)的数据类型转化为因子类型
Golang 快速生成数据库表的 model 和 queryset
SQL -- course experiment examination
R语言caTools包进行数据划分、scale函数进行数据缩放、class包的knn函数构建K近邻分类器、比较不同K值超参数下模型准确率(accuracy)
Thyristor, it is a very important AC control device
Machine learning from entry to re entry: re understanding of SVM
Decryption game of private protocol: from secret text to plaintext
Vs 2019 MFC connects and accesses access database class library encapsulation through ace engine