当前位置:网站首页>LeetCode笔记:Weekly Contest 296
LeetCode笔记:Weekly Contest 296
2022-06-12 07:33:00 【Espresso Macchiato】
1. 题目一
给出题目一的试题链接如下:
1. 解题思路
这一题的解题思路暂时没想到啥奇技淫巧,就先按照题意进行一下迭代求解了,毕竟按照题意的话思路还是很直接的。
2. 代码实现
给出python代码实现如下:
class Solution:
def minMaxGame(self, nums: List[int]) -> int:
n = len(nums)
if n == 1:
return nums[0]
tmp = []
for i in range(n // 2):
if i % 2 == 0:
tmp.append(min(nums[2*i: 2*i+2]))
else:
tmp.append(max(nums[2*i: 2*i+2]))
return self.minMaxGame(tmp)
提交代码评测得到:耗时74ms,占用内存14.1MB。
2. 题目二
给出题目二的试题链接如下:
1. 解题思路
这一题用贪婪算法即可求解。
我们首先对数组进行一下排序,然后在每一个集合当中依次加入元素直至最大元素与最小元素的差大于k,然后新建一个子集。
最后统计所有的子集个数。
2. 代码实现
给出python代码实现如下:
class Solution:
def partitionArray(self, nums: List[int], k: int) -> int:
nums = sorted(nums)
_min = nums[0]
n = len(nums)
res = 1
for i in range(1, n):
if nums[i] - _min > k:
res += 1
_min = nums[i]
return res
提交代码评测得到:耗时1561ms,占用内存28.8MB。
3. 题目三
给出题目三的试题链接如下:
1. 解题思路
这一题我属于审题不清,把题目做烦了,题目中限制了每个元素之可能出现一次,但是我没注意到,所以用了一个数组来记录每一个元素的位置,属于有点浪费了,但是anyway,也可以求解。
这道题感觉应该有更好的解法,但是这里我们就是借用了python当中字典的内置函数,然后就能直接求解了,所以就不多说了,算是投机取巧了吧……
2. 代码实现
给出python代码实现如下:
class Solution:
def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:
rec = {
}
for i, x in enumerate(nums):
if x in rec:
rec[x].append(i)
else:
rec[x] = [i]
for u, v in operations:
rec[v] = rec[u]
rec.pop(u)
res = [0 for _ in nums]
for k, v in rec.items():
for i in v:
res[i] = k
return res
提交代码评测得到:耗时2498ms,占用内存70.4MB。
4. 题目四
给出题目四的试题链接如下:
1. 解题思路
这一题思路上感觉没啥,按照题意去做就行了,算是一种暴力求解的思路,不过anyway能过测试样例……
2. 代码实现
给出python代码实现如下:
class TextEditor:
def __init__(self):
self.text = ""
self.cursor = 0
def addText(self, text: str) -> None:
n = len(text)
self.text = self.text[:self.cursor] + text + self.text[self.cursor:]
self.cursor += n
return
def deleteText(self, k: int) -> int:
cursor = max(0, self.cursor - k)
rm = self.cursor - cursor
self.text = self.text[:cursor] + self.text[self.cursor:]
self.cursor = cursor
return rm
def cursorLeft(self, k: int) -> str:
self.cursor = max(0, self.cursor - k)
return self.text[max(0, self.cursor-10):self.cursor]
def cursorRight(self, k: int) -> str:
self.cursor = min(len(self.text), self.cursor + k)
return self.text[max(0, self.cursor-10):self.cursor]
提交代码评测得到:耗时6010ms,占用内存29.6MB。
边栏推荐
- 2022R2移动式压力容器充装试题模拟考试平台操作
- Summary of machine learning + pattern recognition learning (IV) -- decision tree
- R语言使用caTools包的sample.split函数将机器学习数据集划分为训练集和测试集
- The first demand in my life - batch uploading of Excel data to the database
- VS2019 MFC IP Address Control 控件继承CIPAddressCtrl类重绘
- Personalized federated learning with Moreau envelopes
- Generalized semantic recognition based on semantic similarity
- BI技巧丨当月期初
- Esp8266 firmware upgrade method (esp8266-01s module)
- Golang quickly generates model and queryset of database tables
猜你喜欢

Detailed principle of 4.3-inch TFTLCD based on warship V3

In depth learning - overview of image classification related models

Unity uses shaders to highlight the edges of ugu I pictures

Detailed explanation of 14 registers in 8086CPU

ROS dynamic parameter configuration: use of dynparam command line tool (example + code)

Pyhon的第四天

LED lighting experiment with simulation software proteus

5 lines of code identify various verification codes

Demonstrate "topic communication, action communication, service communication and parameter server" with a small turtle case

Stm32cubemx learning (I) USB HID bidirectional communication
随机推荐
AcWing——4269校庆
右击文件转圈卡住、刷新、白屏、闪退、桌面崩溃的通用解决方法
RT thread studio learning summary
R语言将dataframe数据中指定数据列的数据从小数转化为百分比表示、数据转换为百分数
Complete set of typescript Basics
R语言使用epiDisplay包的summ函数计算dataframe中指定变量在不同分组变量下的描述性统计汇总信息并可视化有序点图、使用dot.col参数设置不同分组数据点的颜色
Federated reconnaissance: efficient, distributed, class incremental learning paper reading + code analysis
Continuous local training for better initialization of Federated models
Vs 2019 MFC connects and accesses access database class library encapsulation through ace engine
Xshell installation
Acwing - 4269 school anniversary
Voice assistant - Multi round conversation (theory and concept)
Machine learning from entry to re entry: re understanding of SVM
Personalized federated learning with exact stochastic gradient descent
Summary of software testing tools in 2021 - unit testing tools
The first demand in my life - batch uploading of Excel data to the database
Demonstrate "topic communication, action communication, service communication and parameter server" with a small turtle case
C language sizeof strlen
VS 2019 MFC 通过ACE引擎连接并访问Access数据库类库封装
R语言使用neuralnet包构建神经网络回归模型(前馈神经网络回归模型),计算模型在测试集上的MSE值(均方误差)