当前位置:网站首页>Leetcode notes: Weekly contest 277
Leetcode notes: Weekly contest 277
2022-06-12 07:53:00 【Espresso Macchiato】
1. Topic 1
The link to question 1 is as follows :
1. Their thinking
This question is actually simple , Let's first merge the same elements , Then on unique The elements are sorted , Then take the middle elements and calculate the sum of their occurrence times .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def countElements(self, nums: List[int]) -> int:
cnt = Counter(nums)
vals = sorted(cnt.items())
return 0 if len(vals) <= 2 else sum(x[1] for x in vals[1:-1])
The submitted code was evaluated : Time consuming 94ms, Take up memory 14.3MB.
2. Topic two
The link to question 2 is as follows :
1. Their thinking
Our thinking on this question is very violent , Take out the positive and negative elements respectively and then merge them again .
However, a more elegant way can follow the example of fast sorting, and continuously sort elements with two pointers , But not here .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
pos = [x for x in nums if x > 0]
neg = [x for x in nums if x < 0]
n = len(pos)
res = [0 for _ in range(2*n)]
for i in range(n):
res[2*i] = pos[i]
res[2*i+1] = neg[i]
return res
The submitted code was evaluated : Time consuming 1576ms, Take up memory 45.8MB.
3. Topic three
The link to question 3 is as follows :
1. Their thinking
Similarly, we only need to sort the original array to get our answer quickly .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def findLonely(self, nums: List[int]) -> List[int]:
n = len(nums)
nums = [-2] + sorted(nums) + [10**7]
res = []
for i in range(1, n+1):
if nums[i] - nums[i-1] <= 1 or nums[i+1] - nums[i] <= 1:
continue
res.append(nums[i])
return res
The submitted code was evaluated : Time consuming 1424ms, Take up memory 30.3MB.
4. Topic four
The link to question 4 is as follows :
1. Their thinking
Our idea of this problem is a violent depth first traversal , Examine each person's telling the truth and falsehood , Then investigate separately .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def maximumGood(self, statements: List[List[int]]) -> int:
n = len(statements)
def dfs(idx, status):
if idx >= n:
return len([x for x in status if x >= 1])
if status[idx] != 1:
s = deepcopy(status)
s[idx] = 0
s1 = dfs(idx+1, s)
else:
s1 = 0
def have_conflict(idx):
return any(statements[idx][i] != 2 and status[i] != 2 and statements[idx][i] != status[i] for i in range(n))
if status[idx] != 0:
if have_conflict(idx):
return s1
s = deepcopy(status)
s[idx] = 1
for i in range(n):
if statements[idx][i] != 2:
s[i] = statements[idx][i]
s2 = dfs(idx+1, s)
else:
s2 = 0
return max(s1, s2)
res = dfs(0, [2 for _ in range(n)])
return res
The submitted code was evaluated : Time consuming 2252ms, Take up memory 14.6MB.
边栏推荐
- Interview questions on mobile terminal, Android and IOS compatibility
- Chapter 8 - firewall, Chapter 9 - Intrusion Detection
- vscode 1.68变化与关注点(整理导入语句/实验性新命令中心等)
- 2D visualization of oil storage, transportation and production, configuration application enables intelligent development of industry
- Seeking for a new situation and promoting development, the head goose effect of Guilin's green digital economy
- 20220607. face recognition
- Utilize user tag data
- Search and rescue strategy of underwater robot (FISH)
- 连接数据库却无法修改数据
- Getting started with Jetson nano Series IV: common skills of NVIDIA Jetson nano
猜你喜欢

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

Seeking for a new situation and promoting development, the head goose effect of Guilin's green digital economy

Getting started with Jetson nano Series IV: common skills of NVIDIA Jetson nano

vscode 1.68变化与关注点(整理导入语句/实验性新命令中心等)

Primal problem and dual problem

2022 simulated test platform operation of hoisting machinery command test questions

Logistic regression

Some summaries of mathematical modeling competition in 2022

Support vector machine (SVM)

In depth learning - overview of image classification related models
随机推荐
移动端、安卓、IOS兼容性面试题
Solve mapper duplication problem in reverse engineering
Scoring prediction problem
WEB页面性能优化面试题
Meter Reading Instrument(MRI) Remote Terminal Unit electric gas water
Connect to the database but cannot modify the data
Compiling principle on computer -- functional drawing language (I)
Voice assistant -- Architecture and design of Instruction Assistant
The latest hbuilderx editing uni app project runs in the night God simulator
Topic 1 Single_ Cell_ analysis(4)
"Three.js" auxiliary coordinate axis
Voice assistant - overall architecture and design
LeetCode笔记:Biweekly Contest 79
Topic 1 Single_ Cell_ analysis(3)
Improvement of hash function based on life game
R语言dplyr包mutate_at函数和one_of函数将dataframe数据中指定数据列(通过向量指定)的数据类型转化为因子类型
MSE (mean square error) calculation package function
20220524 deep learning technology points
Ecmascript6 interview questions
Topic 1 Single_Cell_analysis(1)