当前位置:网站首页>Leetcode notes: biweekly contest 71
Leetcode notes: biweekly contest 71
2022-06-12 07:54:00 【Espresso Macchiato】
1. Topic 1
The link to question 1 is as follows :
1. Their thinking
This question allows 0 At the beginning , Therefore, the overall idea is relatively simple , Only need to 4 Take out a number , Then put the two smaller numbers in the tens , The remaining two large numbers can be placed in single digits .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def minimumSum(self, num: int) -> int:
digits = []
while num != 0:
digits.append(num % 10)
num = num // 10
digits = sorted(digits)
return 10*sum(digits[:2]) + sum(digits[2:])
The submitted code was evaluated : Time consuming 43ms, Take up memory 13.8MB.
2. Topic two
The link to question 2 is as follows :
1. Their thinking
My thinking on this question is more violent , Direct the data according to pivot Divided into three piles , Then reassemble .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
less = [x for x in nums if x < pivot]
equal = [x for x in nums if x == pivot]
large = [x for x in nums if x > pivot]
return less + equal + large
The submitted code was evaluated : Time consuming 2373ms, Take up memory 31.6MB.
3. Topic three
The link to question 3 is as follows :
1. Their thinking
This question is actually quite direct , Calculate according to the meaning of the question , The only thing to notice is that , Because seconds can reach 99, Therefore, there may be multiple settings at some times , You need to compare the two sizes .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:
minute, second = targetSeconds // 60, targetSeconds % 60
def get_cost(minute, second):
digits = (f"{
minute:02d}" + f"{
second:02d}").lstrip("0")
res, pre = 0, str(startAt)
for ch in digits:
res += pushCost
if ch != pre:
res += moveCost
pre = ch
return res
res = get_cost(minute, second) if minute < 100 else math.inf
if minute > 0 and second + 60 < 100:
res = min(res, get_cost(minute-1, second+60))
return res
The submitted code was evaluated : Time consuming 59ms, Take up memory 13.9MB.
4. Topic four
The link to question 4 is as follows :
1. Their thinking
The idea of this question is to choose a separation point in the middle , Left selection n The smallest element , Select... On the right n The biggest element , Then calculate the difference .
therefore , We are just traversing the separation point in the middle , Then count the left and right front n The sum of the largest or smallest elements , Then calculate the difference . The maintenance of the former array can be efficiently implemented through heap arrangement .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def minimumDifference(self, nums: List[int]) -> int:
n = len(nums) // 3
left = [-x for x in nums[:n]]
right = nums[-n:]
ls, rs = sum(left), sum(right)
heapq.heapify(left)
heapq.heapify(right)
cnt = [[ls, rs] for _ in range(n+1)]
for i in range(n):
heapq.heappush(left, -nums[n+i])
lpop = heapq.heappop(left)
cnt[i+1][0] = cnt[i][0] - nums[n+i] - lpop
heapq.heappush(right, nums[2*n-1-i])
rpop = heapq.heappop(right)
cnt[n-1-i][1] = cnt[n-i][1] + nums[2*n-1-i] - rpop
return -max(sum(x) for x in cnt)
The submitted code was evaluated : Time consuming 3545ms, Take up memory 46.3MB.
边栏推荐
- Voice assistant - those classification models used in the assistant
- Topic 1 Single_ Cell_ analysis(3)
- Connect to the database but cannot modify the data
- The computer is connected to WiFi but can't connect to the Internet
- Leetcode notes: Weekly contest 295
- Vs 2019 MFC connects and accesses access database class library encapsulation through ace engine
- Pytorch installation (GPU) in Anaconda (step on pit + fill pit)
- qt. qpa. plugin: Could not load the Qt platform plugin “xcb“ in “***“
- Improvement of hash function based on life game
- Voice assistant - Multi round conversation (process implementation)
猜你喜欢

Topic 1 Single_Cell_analysis(1)

Windows10 configuration database

Exposure compensation, white increase and black decrease theory

二、八、十、十六进制相互转换

电脑连接上WiFi但是上不了网

Chapter 4 - key management and distribution

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

2022 G3 boiler water treatment recurrent training question bank and answers

2022 electrician (elementary) examination question bank and simulation examination

Summary of machine learning + pattern recognition learning (V) -- Integrated Learning
随机推荐
qt. qpa. plugin: Could not load the Qt platform plugin “xcb“ in “***“
Web page performance optimization interview questions
Vscode 1.68 changes and concerns (sorting and importing statements / experimental new command center, etc.)
Leetcode notes: Weekly contest 295
Topic 1 Single_Cell_analysis(2)
Chapter V - message authentication and digital signature
Generalized semantic recognition based on semantic similarity
连接数据库却无法修改数据
Voice assistant -- Qu -- query error correction and rewriting
Ecmascript6 interview questions
Compiling principle on computer -- functional drawing language (I)
R language dplyr package mutate_ At function and one_ The of function converts the data type of a specified data column (specified by a vector) in dataframe data to a factor type
数值计算方法 Chapter5. 解线性方程组的直接法
The R language uses the sample The split function divides the machine learning data set into training set and test set
Rich dad, poor dad Abstract
Voice assistant - future trends
10 lessons from the recommended system
Topic 1 Single_ Cell_ analysis(1)
Unity uses shaders to highlight the edges of ugu I pictures
Summary of semantic segmentation learning (II) -- UNET network