当前位置:网站首页>Review of week 280 of leetcode
Review of week 280 of leetcode
2022-07-01 08:26:00 【What a sacrifice】
6004. obtain 0 The number of operations
Here are two for you non-negative Integers num1 and num2 .
Each step operation in , If num1 >= num2 , You have to use num1 reduce num2 ; otherwise , You have to use num2 reduce num1 .
for example ,num1 = 5 And num2 = 4 , Should use the num1 reduce num2 , therefore , obtain num1 = 1 and num2 = 4 . However , If num1 = 4 And num2 = 5 , After one step operation , obtain num1 = 4 and num2 = 1 .
Return to make num1 = 0 or num2 = 0 Of Operands .
Simulation is enough
class Solution {
public int countOperations(int num1, int num2) {
if(num1==0||num2==0){
return 0;
}
int ans=1;
if(num1>=num2){
num1-=num2;
}else{
num2-=num1;
}
ans+=countOperations(num1,num2);
return ans;
}
}6005. The minimum number of operands to make an array an alternating array
I'll give you a subscript from 0 Starting array nums , The array consists of n It's made up of four positive integers .
If the following conditions are met , The array nums It's a Alternating array :
nums[i - 2] == nums[i] , among 2 <= i <= n - 1 .
nums[i - 1] != nums[i] , among 1 <= i <= n - 1 .
In one step operation in , You can choose the subscript i And will nums[i] change by any Positive integer .
Returns the that makes the array an alternating array The minimum number of operands .
Count the maximum number of occurrences corresponding to odd and even positions 、 Next largest number , If the maximum number is different , Use these numbers , The answer is len-max1-max2, If the same , Use the next largest occurrence number of another
class Solution {
public int minimumOperations(int[] nums) {
int n=nums.length,ans=0;
if(n==1)return 0;
int even1=0,even2=0,odd1=0,odd2=0;
int max1=0,max2=0;
Map<Integer,Integer>map1=new HashMap<>();
Map<Integer,Integer>map2=new HashMap<>();
for(int i=0;i<n;i+=2){
map1.put(nums[i],map1.getOrDefault(nums[i],0)+1);
}
for(int num:map1.keySet()){
int cnt=map1.get(num);
if(cnt>even1){
max1=num;
even2=even1;
even1=cnt;
}else if(cnt>even2){
even2=cnt;
}
}
for(int i=1;i<n;i+=2){
map2.put(nums[i],map2.getOrDefault(nums[i],0)+1);
}
for(int num:map2.keySet()){
int cnt=map2.get(num);
if(cnt>odd1){
max2=num;
odd2=odd1;
odd1=cnt;
}else if(cnt>odd2){
odd2=cnt;
}
}
if(max1!=max2){
ans=n-even1-odd1;
}else{
if(odd1+even2>even1+odd2){
System.out.println(" Use the maximum odd number "+odd1+" And sub large even numbers "+even2);
ans=n-odd1-even2;
}else{
System.out.println(" Use the maximum even number "+even1+" And sub large odd numbers "+odd2);
ans=n-even1-odd2;
}
}
System.out.println(n);
return ans;
}
}6006. Take out the smallest number of magic beans
To give you one just An array of integers beans , Each integer represents the number of magic beans in a bag .
Please... From each bag take out Some beans ( It's fine too Don't take out ), Make the rest Non empty In the bag ( namely At least also One Magic bean bag ) Number of magic beans equal . Once the magic beans are removed from the bag , You can't put it in any other bag .
Please return to where you need to take out the magic beans Minimum number .
Prioritize , Then traverse from front to back , The calculation is based on the current bag ( Clear all the front ) The number of beans you need to take out ( Note that it is not possible to empty the last bag , The last bag as a benchmark must consume less beans than emptying ), Returns the minimum number of magic beans
class Solution {
public long minimumRemoval(int[] beans) {
Arrays.sort(beans);
int n=beans.length;
if(n==1)return 0;
long sum=beans[0];
long[]pre=new long[n];
pre[0]=(long)beans[0];
for(int i=1;i<n;i++){
sum+=(long)beans[i];
}
for(int i=0;i<n;i++){
pre[i]=sum-(long)(n-i)*beans[i];
}
Arrays.sort(pre);
return pre[0];
}
}
边栏推荐
- Connect timed out of database connection
- 【刷题】字符统计【0】
- AArdio - 【问题】bass库回调时内存增长的问题
- Transaction method call @transactional
- Comprehensive experiment Li
- getInputStream() has already been called for this request
- web254
- Aardio - [problem] the problem of memory growth during the callback of bass Library
- Soft keyboard height error
- Leetcode t34: find the first and last positions of elements in a sorted array
猜你喜欢

Gateway-88
![[staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)](/img/ff/ebd936eaa6e57b1eabb691b0642957.jpg)
[staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)

使用 setoolkit 伪造站点窃取用户信息

Comprehensive experiment Li

Latex table
![[untitled]](/img/be/3523d0c14d555b293673af2b6fbcff.jpg)
[untitled]

軟鍵盤高度報錯

0 basic introduction to single chip microcomputer: how to use digital multimeter and precautions

01 NumPy介绍
![Thread safety analysis of [concurrent programming JUC] variables](/img/f9/a3604bec6f7e5317dd2c578da73018.jpg)
Thread safety analysis of [concurrent programming JUC] variables
随机推荐
When using charts to display data, the time field in the database is repeated. How to display the value at this time?
Yolov5进阶之七目标追踪最新环境搭建
Instead of houses, another kind of capital in China is rising
Adding color blocks to Seaborn clustermap matrix
[untitled]
Luogu p1088 [noip2004 popularization group] Martians
事务方法调用@Transactional
2022 Chinese cook (technician) simulation test and Chinese cook (technician) practice test video
Find the nearest n-th power of 2
Airsim雷达相机融合生成彩色点云
如何招到适合自己店铺的淘宝主播
Manually dig XSS vulnerabilities
To prevent "activation" photos from being muddled through, databao "live detection + face recognition" makes face brushing safer
Anddroid 文本合成语音TTS实现
Precautions and skills in using regular expressions in golang
Cmake I two ways to compile source files
Count number of rows per group and add result to original data frame
使用threejs简单Web3D效果
Uni hot update
Provincial election + noi part I dynamic planning DP