当前位置:网站首页>Leetcode:20220213 week race (less bugs, top 10% 555)
Leetcode:20220213 week race (less bugs, top 10% 555)
2022-07-06 00:19:00 【White speed Dragon King's review】
Ideas :
1. Divide the odd and even statistical data frequency
2. Then see if they are equal
3. Take down one as appropriate
src:
lass Solution:
def minimumOperations(self, nums: List[int]) -> int:
num1 = nums[::2]
num2 = nums[1::2]
n1 = len(num1)
n2 = len(num2)
if n1 * n2 == 0:
return 0
cnt1 = defaultdict(int)
for num in num1:
cnt1[num] += 1
cnt2 = defaultdict(int)
for num in num2:
cnt2[num] += 1
sort1 = sorted(cnt1.items(),key=lambda x:x[1],reverse=True) # Sort in descending order according to word frequency
sort2 = sorted(cnt2.items(),key=lambda x:x[1],reverse=True) # Sort in descending order according to word frequency
has = 0
if sort1[0][0] != sort2[0][0]:
has = sort1[0][1] + sort2[0][1]
else:
if len(sort1) == 1 and len(sort2) == 1:
has = max(sort1[0][1], sort2[0][1])
elif len(sort1) == 1:
has = sort1[0][1] + sort2[1][1]
elif len(sort2) == 1:
has = sort2[0][1] + sort1[1][1]
else:
if sort1[1][1] + sort2[0][1] > sort1[0][1] + sort2[1][1]:
has = sort1[1][1] + sort2[0][1]
else:
has = sort1[0][1] + sort2[1][1]
return n1 + n2 - has
Ideas :
Solve the absolute value equation , Given x
If greater than or equal to x, Make a difference ; If less than x, Then take the sum of this number 0 Distance of
By traversing x, You can get the smallest ans
Be careful , You don't need to calculate every traversal , It only needs - some + Just some , This is the charm of the mixture of absolute value function and traversal
src:
class Solution:
def minimumRemoval(self, beans: List[int]) -> int:
# All become the smallest or Minimal change 0
beans.sort()
n = len(beans)
#print(beans)
# Determine a value x, Less than it, add it all , If it is greater than it, take the difference
temp = 0
x = beans[0]
index = 0
ans = 0xffffffff
for num in beans:
temp += num - x
ans = min(ans, temp)
while index < n:
nextIndex = index
while nextIndex < n and beans[nextIndex] == x:
nextIndex += 1
if nextIndex >= n:
break
temp += x * (nextIndex - index)
y = beans[nextIndex]
temp -= (y - x) * (n - nextIndex)
ans = min(ans, temp)
x = y
index = nextIndex
return ans
summary :
without bug, Just before 300 了 , It's all because the second question is wrong index
边栏推荐
- [designmode] composite mode
- Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
- 单商户V4.4,初心未变,实力依旧!
- Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
- AtCoder Beginner Contest 258【比赛记录】
- QT--线程
- Hardware and interface learning summary
- MDK debug时设置数据实时更新
- LeetCode 6005. The minimum operand to make an array an alternating array
- 从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
猜你喜欢
Determinant learning notes (I)
FFT 学习笔记(自认为详细)
FFMPEG关键结构体——AVFormatContext
MySql——CRUD
Mysql - CRUD
云呐|固定资产管理系统功能包括哪些?
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
How to solve the problems caused by the import process of ecology9.0
Atcoder beginer contest 258 [competition record]
Effet Doppler (déplacement de fréquence Doppler)
随机推荐
JS 这次真的可以禁止常量修改了!
Shardingsphere source code analysis
【GYM 102832H】【模板】Combination Lock(二分图博弈)
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot system behavior analysis and project summary (4
MySQL之函数
Yunna | what are the main operating processes of the fixed assets management system
[Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
[designmode] adapter pattern
shardingsphere源码解析
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
LeetCode 6004. Get operands of 0
Configuring OSPF load sharing for Huawei devices
XML configuration file (DTD detailed explanation)
GD32F4xx uIP协议栈移植记录
What are the functions of Yunna fixed assets management system?
LeetCode 6005. The minimum operand to make an array an alternating array
时区的区别及go语言的time库
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
notepad++正則錶達式替換字符串