小S的倒排索引

79 阅读2分钟

The problem at hand involves finding the intersection of two lists of integers, identifying elements that are present in both lists, and returning these elements in descending order. The solution requires counting occurrences of each integer from both lists and then filtering based on whether an integer appears at least once in both lists.

Let's break down the provided solution and explain how it works:

Key Components of the Solution:

  1. Initialization:

    • An integer array arr is created to count occurrences of numbers up to a maximum possible value (assumed to be 50005).
    • max is initialized to track the highest number encountered in both lists.
  2. Counting Elements:

    • Two loops iterate over each list (a and b). For each number, it increments the count in the arr array and updates max if the current number is greater than the current max.
  3. Finding Common Elements:

    • A third loop iterates from max down to 0. For each index in arr, it checks if the count is 2 (indicating the number appears in both lists).
    • If it does, the number is added to the result list arr1.
  4. Returning the Results:

    • Finally, the method returns arr1, which holds the common elements in descending order.

Time Complexity:

  • The overall time complexity of this solution is O(n + m), where n is the size of list a and m is the size of list b. This is because we traverse through both lists once and then process the count array, which has a fixed maximum size.

Space Complexity:

  • The space complexity is O(k), where k is the range of integers handled (in this case, fixed at 50005). This is primarily used for the count array.

Code Explanation:

Here's the code with added comments for clarity:

import java.util.*;

public class Main {
    public static List<Integer> solution(List<Integer> a, List<Integer> b) {
        // Array to count occurrences of numbers; assuming a maximum value of 50004
        int[] arr = new int[50005];
        int max = 0;

        // Count occurrences in list a
        for (int i = 0; i < a.size(); i++) {
            arr[a.get(i)]++;
            max = Math.max(max, a.get(i)); // Update max
        }

        // Count occurrences in list b
        for (int i = 0; i < b.size(); i++) {
            arr[b.get(i)]++;
            max = Math.max(max, b.get(i)); // Update max
        }

        List<Integer> arr1 = new ArrayList<>(); // Result list
        // Find common elements in descending order
        for (int i = max; i >= 0; i--) {
            if (arr[i] == 2) { // Appears in both lists
                arr1.add(i);
            }
        }
        
        return arr1; // Return the resulting list
    }

    public static void main(String[] args) {
        // Test cases to verify the solution
        System.out.println(solution(Arrays.asList(1, 2, 3, 7), Arrays.asList(2, 5, 7)).equals(Arrays.asList(7, 2)));
        System.out.println(solution(Arrays.asList(1, 4, 8, 10), Arrays.asList(2, 4, 8, 10)).equals(Arrays.asList(10, 8, 4)));
        System.out.println(solution(Arrays.asList(3, 5, 9), Arrays.asList(1, 4, 6)).equals(Collections.emptyList()));
        System.out.println(solution(Arrays.asList(1, 2, 3), Arrays.asList(1, 2, 3)).equals(Arrays.asList(3, 2, 1)));
    }
}

Summary:

This solution effectively combines elements from both lists, keeping track of their counts and yielding the intersection in a sorted manner (descending), using a straightforward counting approach. The algorithm is efficient and handles the task with minimal complexity.