当前位置:网站首页>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
边栏推荐
- Atcoder beginer contest 254 [VP record]
- USB Interface USB protocol
- N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
- Configuring OSPF load sharing for Huawei devices
- Yunna | what are the main operating processes of the fixed assets management system
- Gd32f4xx UIP protocol stack migration record
- MySQL之函数
- Key structure of ffmpeg - avframe
- Notepad + + regular expression replace String
- Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
猜你喜欢

云呐|公司固定资产管理系统有哪些?
![[binary search tree] add, delete, modify and query function code implementation](/img/38/810a83575c56f17a7a0ed428a2e02e.png)
[binary search tree] add, delete, modify and query function code implementation

Priority queue (heap)
![[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)](/img/d6/c3128e26d7e629b7f128c551cd03a7.png)
[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)

选择致敬持续奋斗背后的精神——对话威尔价值观【第四期】

Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
![Atcoder beginer contest 258 [competition record]](/img/e4/1d34410f79851a7a81dd8f4a0b54bf.gif)
Atcoder beginer contest 258 [competition record]

认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)

Determinant learning notes (I)

Mathematical model Lotka Volterra
随机推荐
Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
MySQL之函数
Upgrade openssl-1.1.1p for openssl-1.0.2k
FFmpeg学习——核心模块
Hudi of data Lake (2): Hudi compilation
Problems encountered in the database
Key structure of ffmpeg -- AVCodecContext
硬件及接口学习总结
FFMPEG关键结构体——AVCodecContext
MySql——CRUD
[gym 102832h] [template] combination lock (bipartite game)
Priority queue (heap)
Browser local storage
Yolov5、Pycharm、Anaconda环境安装
DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
Permission problem: source bash_ profile permission denied
SQLServer连接数据库读取中文乱码问题解决
MySQL global lock and table lock
时区的区别及go语言的time库
Chapter 16 oauth2authorizationrequestredirectwebfilter source code analysis