当前位置:网站首页>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];
}
}
边栏推荐
- 【力扣10天SQL入门】Day9 控制流
- php laravel微信支付
- 【刷题】字符统计【0】
- C basic knowledge review (Part 4 of 4)
- window c盘满了
- Find the nearest n-th power of 2
- 图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
- Leetcode t39: combined sum
- OJ input and output exercise
- [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)
猜你喜欢

Hijacking a user's browser with beef

Learn reptiles for a month and earn 6000 a month? Tell you the truth about the reptile, netizen: I wish I had known it earlier

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

【入门】提取不重复的整数

Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure

Aardio - Shadow Gradient Text

CPU設計實戰-第四章實踐任務一簡單CPU參考設計調試

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

軟鍵盤高度報錯

华为机试真题专栏订阅指引
随机推荐
XX attack - reflective XSS attack hijacking user browser
PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux
CPU设计实战-第四章实践任务一简单CPU参考设计调试
Php laraver Wechat payment
shardingSphere
Lm08 mesh series mesh inversion (fine)
php laravel微信支付
When using charts to display data, the time field in the database is repeated. How to display the value at this time?
一套十万级TPS的IM综合消息系统的架构实践与思考
EDA open source simulation tool verilator beginner 6: debugging examples
On several key issues of digital transformation
Codeworks round 803 (Div. 2) VP supplement
【入门】提取不重复的整数
Airsim雷达相机融合生成彩色点云
2022 ordinary scaffolder (special type of construction work) examination question bank and the latest analysis of ordinary scaffolder (special type of construction work)
Comprehensive experiment Li
Cmake I two ways to compile source files
Leetcode t39: combined sum
华为机试真题专栏订阅指引
empirical study and case study