[daily algorithm] force deduction 350 Intersection of two arrays II

Posted by phpete2 on Sun, 27 Feb 2022 02:04:52 +0100

describe

Given two arrays, write a function to calculate their intersection.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output:[2,2]
Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output:[4,9]

explain:

  • The number of occurrences of each element in the output result shall be consistent with the minimum number of occurrences of elements in the two arrays.

  • We can ignore the order of output results.

Make a question

I didn't quite understand the first item of this instruction.

From the topic "intersection of two arrays", it should be that the order of two arrays does not need to be considered.

At the beginning, I thought that we should consider the order of two arrays. For example, there is order in [4,9] of test example 2, which is more difficult to do. The only way may be a two-layer traversal loop.

Because we don't need to consider the order of results, we can consider using map to store the number of occurrences of each number, and then take the number with the least number of times as the result.

public int[] intersect(int[] nums1, int[] nums2) {
    Map<Integer,Integer> map1=new HashMap<>();
    Map<Integer,Integer> map2=new HashMap<>();

    for (int i = 0; i < nums1.length; i++) {
        int num = nums1[i];
        Integer integer = map1.get(num);
        if (integer == null){
            integer = 1;
            map1.put(num,integer);
            continue;
        }
        integer +=1;
        map1.put(num,integer);
    }

    for (int i = 0; i < nums2.length; i++) {
        int num = nums2[i];
        Integer integer = map2.get(num);
        if (integer == null){
            integer = 1;
            map2.put(num,integer);
            continue;
        }
        integer +=1;
        map2.put(num,integer);
    }

    List<Integer> list = new ArrayList<>();
    Set<Integer> integers = map1.keySet();
    Iterator<Integer> iterator = integers.iterator();
    while (iterator.hasNext()){
        Integer next = iterator.next();
        Integer integer2 = map2.get(next);
        if (integer2==null){
            continue;
        }
        Integer integer1 = map1.get(next);
        integer1=integer2>integer1?integer1:integer2;
        while (integer1-->0){
            list.add(next);
        }
    }
    int[] result = new int[list.size()];
    for (int i = 0; i < list.size(); i++) {
        result[i]=list.get(i);
    }

    return result;
}

Run is run through.

But the efficiency is good and low, and the code is also very long.

Optimize this part of the code and combine it with the second loop to reduce the time complexity.

public int[] intersect1(int[] nums1, int[] nums2) {
    Map<Integer,Integer> map1=new HashMap<>();
    List<Integer> list = new ArrayList<>();
    for (int i = 0; i < nums1.length; i++) {
        int num = nums1[i];
        Integer integer = map1.get(num);
        if (integer == null){
            integer = 1;
            map1.put(num,integer);
            continue;
        }
        map1.put(num,++integer);
    }

    for (int i = 0; i < nums2.length; i++) {
        int num = nums2[i];
        Integer integer1 = map1.get(num);
        if (integer1 == null){
            //When integer1 equals null, there is no need to do the following operations
            continue;
        }
        if ( integer1!=0){
            list.add(num);
            map1.put(num,--integer1);
        }

    }
    int[] result = new int[list.size()];
    for (int i = 0; i < list.size(); i++) {
        result[i]=list.get(i);
    }

    return result;
}

I don't know about optimization. As soon as I optimize, I reduce more than 20 lines of code and optimize map2.

But as a result of this operation, how does the memory usage become higher?

That's all for today.

This is programmer Xu Xiaobai, and [daily algorithm] is my new column. It mainly records my daily learning algorithm, and I hope I can adhere to the daily learning algorithm. I don't know whether you like this style of article. Don't be stingy with your free praise. Your praise, collection and comments are the driving force for me to stick to more writing after work.

Topics: Algorithm data structure leetcode