当前位置:网站首页>[Li Kou] the second set of the 280 Li Kou weekly match
[Li Kou] the second set of the 280 Li Kou weekly match
2022-07-06 03:11:00 【Xiao Huang, who likes to knock code】
- Author's brief introduction : Hello everyone , I'm Xiao Huang who likes to knock code , Unicorn enterprise Java Development Engineer ,Java New star creators in the field
- Official account number : Xiao Huang who likes to knock code
- Series column :Java Design patterns 、 Data structures and algorithms
- If there is something wrong with the knowledge of the article , Please correct me. ! Learn with you , Progress together
- If you feel the blogger's article is good , Please support the blogger for the third company
- Bloggers are trying to complete 2022 Planned : Take dreams as horses , Set sail ,2022 Dream catcher
List of articles
One 、 introduction
Today's weekly match is hard to say , Because of a simple long Conversion negligence leads to oneself T3 no A
Two 、 6004. obtain 0 The number of operations
1、 Introduction to the topic

2、 title
- Simulation is enough
- Pay attention to the return condition :
while(num1 != 0 && num2 != 0)
3、 Title code
class Solution {
public int countOperations(int num1, int num2) {
int count = 0;
while(num1 != 0 && num2 != 0){
if(num1 >= num2){
num1 = num1 - num2;
}else{
num2 = num2 - num1;
}
count++;
}
return count;
}
}
3、 ... and 、6005. The minimum number of operands to make an array an alternating array
1、 Introduction to the topic

2、 title
- greedy
- We analyze the topic , In a nutshell , The final result :
Our odd digits are the same number , Even digits are the same number , And the numbers cannot be equal - We use two priority queues to store its number , according to
The most frequent occurrenceJudge - Let's consider the following situations :
- [0]:
Queue 2 is empty - [1,2]:
The size of queue one and queue two is 1 - [1,2,3]:
The size of queue one is 2, The size of queue 2 is 1 - [1,2,3,4]:
The size of queue one is 2, The size of queue 2 is 2
- [0]:
- We compare the priority queue
int value = peek(), The number of times the number appears- If not equal , It means that you need to become
value, Calculate the number of times - If it's equal , We need to compare
valueSize , Big ones stay the same , Small needs becomeThe second largest
- If not equal , It means that you need to become
3、 Title code
class Solution {
public static int minimumOperations(int[] nums) {
if (nums.length == 1) {
return 0;
}
HashMap<Integer, Integer> map1 = new HashMap<>();
HashMap<Integer, Integer> map2 = new HashMap<>();
int count1 = 0;
int count2 = 0;
for (int i = 0; i < nums.length; i++) {
if (i % 2 != 0) {
count1++;
map1.put(nums[i], map1.getOrDefault(nums[i], 0) + 1);
} else {
count2++;
map2.put(nums[i], map2.getOrDefault(nums[i], 0) + 1);
}
}
// Odd number
PriorityQueue<int[]> priorityQueue1 = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o2[1] - o1[1];
}
});
for (Integer key : map1.keySet()) {
priorityQueue1.add(new int[]{
key, map1.get(key)});
}
// even numbers
PriorityQueue<int[]> priorityQueue2 = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o2[1] - o1[1];
}
});
for (Integer key : map2.keySet()) {
priorityQueue2.add(new int[]{
(int) key, (int) map2.get(key)});
}
if (priorityQueue1.size() == 1 && priorityQueue2.size() == 1) {
if (priorityQueue1.peek()[0] != priorityQueue2.peek()[0]) {
return 0;
} else {
return priorityQueue1.peek()[1] >= priorityQueue2.peek()[1] ? priorityQueue2.peek()[1] : priorityQueue1.peek()[1];
}
}else if (priorityQueue1.size() > 1 && priorityQueue2.size() == 1) {
if (priorityQueue1.peek()[0] != priorityQueue2.peek()[0]) {
return count1 - priorityQueue1.peek()[1];
} else {
if (priorityQueue1.peek()[1] >= priorityQueue2.peek()[1]) {
return count2 + count1 - priorityQueue1.peek()[1];
} else {
priorityQueue1.poll();
return count1 - priorityQueue1.peek()[1];
}
}
} else {
if (priorityQueue1.peek()[0] != priorityQueue2.peek()[0]) {
return count1 - priorityQueue1.peek()[1] + count2 - priorityQueue2.peek()[1];
} else {
if (priorityQueue1.peek()[1] >= priorityQueue2.peek()[1]) {
priorityQueue2.poll();
return count1 - priorityQueue1.peek()[1] + count2 - priorityQueue2.peek()[1];
} else {
priorityQueue1.poll();
return count1 - priorityQueue1.peek()[1] + count2 - priorityQueue2.peek()[1];
}
}
}
}
}
Four 、6006. Take out the smallest number of magic beans
1、 Introduction to the topic

2、 title
- We found that , We want to make magic beans equal , Need to find one
critical point, According to this critical point :Less than his all become 0, More than his whole becomes it - Sort first
- We use
[1,4,5,6]As an example- Suppose the current critical point is
5, So our1、4Need to become0,6become5, The final result is :7 - Because our magic beans can't be increased , Can only reduce
- When we traverse its value , We will
Overall sum - The number of subsequent data * The current value
- Be careful , When we are evaluating , Don't forget to change the type to
long - This topic can also be used
The prefix and
- Suppose the current critical point is
3、 Title code
class Solution {
public long minimumRemoval(int[] beans) {
int len = beans.length;
Arrays.sort(beans);
long sum = 0;
for(int i = 0; i < len; i++){
sum = sum + beans[i];
}
long minSum = Long.MAX_VALUE;
for(int i = 0; i < len; i++){
// Attention turns to long
minSum = Math.min(minSum, sum - (len - i) * (long)beans[i]);
}
return minSum;
}
}
5、 ... and 、 summary
The result of this week's race is still not ideal , I hope the next week's race will be better ~
Next time, be sure to pay attention to the conversion of types
Come on ~~~
边栏推荐
- . Net 6 and Net core learning notes: Important issues of net core
- Pat 1084 broken keyboard (20 points) string find
- SD card reports an error "error -110 whilst initializing SD card
- 故障分析 | MySQL 耗尽主机内存一例分析
- 多态day02
- Some problem records of AGP gradle
- 淘宝焦点图布局实战
- tcpdump: no suitable device found
- Briefly describe the implementation principle of redis cluster
- 真机无法访问虚拟机的靶场,真机无法ping通虚拟机
猜你喜欢

Communication between microservices
![[pointer training - eight questions]](/img/fd/1aa3937548a04078c4d7e08198c3a8.png)
[pointer training - eight questions]
![[concept] Web basic concept cognition](/img/27/14bcd73ca70d136436a4382a1b4bd1.jpg)
[concept] Web basic concept cognition

OCR文字识别方法综述
![[ruoyi] enable Mini navigation bar](/img/28/a8b38aecd90c8ddc98333f0e2d3eab.png)
[ruoyi] enable Mini navigation bar

解决:AttributeError: ‘str‘ object has no attribute ‘decode‘

I sorted out a classic interview question for my job hopping friends

【Kubernetes 系列】一文学会Kubernetes Service安全的暴露应用

全国大学生信息安全赛创新实践赛初赛---misc(永恒的夜)

MySQL advanced notes
随机推荐
【 kubernets series】 a Literature Study on the Safe exposure Applications of kubernets Service
CobaltStrike-4.4-K8修改版安装使用教程
Performance analysis of user login TPS low and CPU full
Codeworks 5 questions per day (1700 average) - day 6
07 singleton mode
Taobao focus map layout practice
[kubernetes series] learn the exposed application of kubernetes service security
Solution: attributeerror: 'STR' object has no attribute 'decode‘
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1
mysqldump数据备份
【概念】Web 基础概念认知
[concept] Web basic concept cognition
故障分析 | MySQL 耗尽主机内存一例分析
js 正则过滤和增加富文本中图片前缀
Apt installation ZABBIX
微服务间通信
jsscript
Introduction to robotframework (III) Baidu search of webui automation
手写数据库客户端
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]