[Meituan Autumn Recruitment] 20230922 Xiaomei’s Skittles

Xiaomei’s rainbow candy

Xiao Mei has a lot of rainbow candies, each of which has a color. She can eat two rainbow candies every day. If the combination of rainbow candies she eats today is a combination she has never eaten before, Xiao Mei will be very happy today.
For example, Xiaomei has 6 rainbow candies, the colors are [1,1,4,5,1,4].

If Xiaohong eats a set of rainbow candies with colors 1 and 4 on the first day, Xiaomei will be very happy; if Xiaohong
eats a set of rainbow candies with colors 4 and 1 on the second day, Xiaomei will not be very happy;
on the third day Xiaomei will not be very happy. If you eat a set of rainbow candies with colors 1 and 5, Xiaomei will be very happy. At this time, Xiaomei will be happy for a total of 2 days.

Xiaomei wants to know how many days at most she will be happy.

Input description:
Enter an integer n in the first line (1<=n<= 1 0 5 10^5105 ) Indicates the number of rainbow candies.
In the second line, n integers are entered to represent the color of rainbow candy a (1<=ai a_iai<= 1 0 9 10^9 109)。

Output Description
Output an integer representing the answer.

Example input

6
1 1 4 5 1 4

output

3

Instructions:
On the first day, eat a set of rainbow candies with colors 1 and 4.
On the second day, eat a set of rainbow candies with colors 4 and 5.
On the third day, eat a set of rainbow candies with colors 1,1.
Xiaomei will be very happy for 3 days.

answer

(Wrong answer)
The hash table counts the number of occurrences of each occurrence. If the number is greater than 2, first create the sub-ring (x, x), then traverse the keys of the map, and try to match. If the number is still there and the last one has not been matched, do ++

import java.util.*;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < n; i++) {
    
    
            int temp = in.nextInt();
            map.put(temp, map.getOrDefault(temp, 0) + 1);
        }
        //System.out.println(map);

        Object[] array = map.keySet().toArray();
        int count = 0;

        for (int i = 0; i < array.length; i++) {
    
    
            if (map.get(array[i]) >= 2) {
    
    
                map.put((Integer) array[i], map.get(array[i]) - 2);
                count++;
            }
        }
        //System.out.println(map);
        //System.out.println(count);

        for (int i = 0; i < array.length; i++) {
    
    
            for (int j = i + 1; j < array.length; j++) {
    
    
                if (map.get(array[i]) > 0 && map.get(array[j]) > 0) {
    
    
                    map.put((Integer) array[i], map.get(array[i]) - 1);
                    map.put((Integer) array[j], map.get(array[j]) - 1);
                    count++;
                    //System.out.println(map);
                }
            }
        }
        System.out.println(count);
    }
}
//22
//十个1,十个2,俩个3不对

It feels like the right answer

First, sort them according to the number of occurrences from large to small, pick the one with the largest number, first judge whether it can self-loop, then match others in turn, and then take out the next one.

import java.util.*;
import java.util.stream.Collectors;

public class test {
    
    
    public static void main(String[] args) {
    
    
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Map<Integer, Integer> map = new HashMap<>();
        int count = 0;

        //得到 map
        for (int i = 0; i < n; i++) {
    
    
            int temp = in.nextInt();
            map.put(temp, map.getOrDefault(temp, 0) + 1);
        }
        //System.out.println(map);

        // 使用Stream API根据 Map的值进行排序
        List<Map.Entry<Integer, Integer>> sortedMapList = map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue())
                .collect(Collectors.toList());

        int[] tempArray = new int[sortedMapList.size()];
        int a = 0;
        // 输出排序后的结果
        for (Map.Entry<Integer, Integer> entry : sortedMapList) {
    
    
            tempArray[a] = entry.getKey();
            a++;
        }
        /*for (int j:tempArray) {
            System.out.print(j+" ");
        }
        System.out.println();*/

        for (int i = tempArray.length-1; i >=0 ; i--) {
    
    
            //先匹配自己
            if(map.get(tempArray[i]) >= 2){
    
    
                map.put(tempArray[i], map.get(tempArray[i]) - 2);
                count++;
            }
            //再匹别人
            for (int j = i-1; j >=0 ; j--) {
    
    
                //自己大于0,且想匹配的数也大于0
                if (map.get(tempArray[i]) > 0 && map.get(tempArray[j]) > 0) {
    
    
                    map.put(tempArray[i], map.get(tempArray[i]) - 1);
                    map.put(tempArray[j], map.get(tempArray[j]) - 1);
                    count++;
                }
            }
        }
        System.out.println(count);
    }
}

Guess you like

Origin blog.csdn.net/qq_44033208/article/details/132645506