当前位置:网站首页>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];
}
}
边栏推荐
- Provincial election + noi part I dynamic planning DP
- To prevent "activation" photos from being muddled through, databao "live detection + face recognition" makes face brushing safer
- 如何招到适合自己店铺的淘宝主播
- String coordinates of number to excel
- C basic knowledge review (Part 4 of 4)
- Aardio - 阴影渐变文字
- Transaction method call @transactional
- Yolov5 advanced 7 target tracking latest environment setup
- Cmake I two ways to compile source files
- [force deduction 10 days SQL introduction] Day10 control flow
猜你喜欢
随机推荐
Programmer's regimen
Leetcode t31: prochain arrangement
如何招到适合自己店铺的淘宝主播
The Windows C disk is full
CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging
Soft keyboard height error
Set up file server Minio for quick use
Yolov5 advanced 7 target tracking latest environment setup
[untitled]
【力扣10天SQL入门】Day9 控制流
Learn the knowledge you need to know about the communication protocol I2C bus
[question brushing] character statistics [0]
2022.6.30 省赛+蓝桥国赛记录
华为机试真题专栏订阅指引
[redis] it takes you through redis installation and connection at one go
Latex table
Aardio - [problem] the problem of memory growth during the callback of bass Library
軟鍵盤高度報錯
[untitled]
5大组合拳,解决校园6大难题,护航教育信息化建设









