简体   繁体   English

在Java中使用O(logn)复杂度比较两个数组列表

[英]Compare two array list using O(logn) complexity in java

ArrayList<String> array1 = new ArrayList<>(Arrays.asList("netflix", "dhoni", "harini", "obama", "machintosh"));

ArrayList<String> array2 = new ArrayList<>(Arrays.asList("netflix", "dhoni", "harini", "obama", "quark", "machintosh"));

We have to array list with same string and same order but in array2 one string is different. 我们必须使用相同的字符串和相同的顺序来排列列表,但是在array2中,一个字符串是不同的。 I have to find out that string and its position using O(LogN) complexity. 我必须使用O(LogN)复杂度找出该字符串及其位置。

I have solved using O(N) complexity but I want O(LogN) complexity. 我已经解决了使用O(N)复杂度,但我想要O(LogN)复杂度。

My Solution is given below:- 我的解决方案如下:

ArrayList<String> array1 = new ArrayList<>(Arrays.asList("netflix", "dhoni", "harini", "obama", "machintosh"));
ArrayList<String> array2 = new ArrayList<>(Arrays.asList("netflix", "dhoni", "harini", "obama", "quark", "machintosh"));
for(int i = 1; i <= array2.size(); i++){
    if(array1.contains(array2.get(i-1))){

    }
    else{
        System.out.println(array2.get(i-1)+" "+i);
    }
}

But its giving O(N) complexity. 但是它给了O(N)复杂性。

Here's a solution that works with lists of any class (as long as its equals method does the right thing). 这是一种可与任何类的列表一起使用的解决方案(只要其equals方法能够正确执行操作即可)。

package so53375733;

import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> list1 = Arrays.asList("netflix", "dhoni", "harini", "obama", "machintosh");
        List<String> list2 = Arrays.asList("netflix", "dhoni", "harini", "obama", "quark", "machintosh");
        int addedElementIndex = findAddedElement(list1, list2);
        System.out.printf(
                "Found difference at index %1$d:%n" +
                        "list1[%1$d] = \"%2$s\"%n" +
                        "list2[%1$d] = \"%3$s\"%n",
                addedElementIndex,
                addedElementIndex < list1.size() ? list1.get(addedElementIndex) : "[end of list]",
                addedElementIndex < list2.size() ? list2.get(addedElementIndex) : "[end of list]");
    }

    /**
     * Performs a binary search for an added (or removed) element of list1 with respect to list2
     * (or vice versa). The lists passed as argument should differ only by the addition of one element,
     * so that their sizes differ by 1 and the lists are identical except for the extra element in one
     * of the lists. If the lists are random-access (i.e. directly indexable in O(1) time) then this
     * method's time complexity is O(log N).
     * @param list1 A random-access list
     * @param list2 A random-access list
     * @return The index of the extra element
     */
    private static <T> int findAddedElement(List<T> list1, List<T> list2) {
        int low = 0;
        int high = Math.min(list1.size(), list2.size()) - 1;

        if (list1.get(high).equals(list2.get(high)))
            return high + 1;

        // Loop invariants:
        // 1. Elements of list1 are equal to those of list2 at all indices less than 'low'.
        // 2. Elements of list1 are NOT equal to those of list2 at all indices >= 'high'.
        while (low < high) {
            int mid = (low + high) >>> 1;  // (low+high)/2 might overflow
            if (list1.get(mid).equals(list2.get(mid)))
                low = mid + 1;
            else
                high = mid;
        }

        return low;
    }
}

Output: 输出:

Found difference at index 4:
list1[4] = "machintosh"
list2[4] = "quark"

You could binary search, like this: 您可以像这样进行二进制搜索:

public static <T extends Comparable<T>> int findIndexOfNewElement(List<T> list, List<T> modelList) {
    int lower = 0;
    int upper = list.size() - 1;
    int mid = (upper + lower) / 2;
    while (lower < upper) {
        if (mid >= modelList.size()) {
            // The last element is the new one
            return modelList.size();
        }
        if (list.get(mid).compareTo(modelList.get(mid)) != 0) {
            // if they are not the same element
            // then there has been an insert before or at this index
            upper = mid;
        } else {
            lower = mid + 1;
        }
        mid = (upper + lower) / 2;
    }
    return mid;
}

public static void main(String[] args) {

    ArrayList<String> array1 = new ArrayList<>(Arrays.asList("netflix", "dhoni", "harini", "obama", "machintosh"));
    ArrayList<String> array2 = new ArrayList<>(Arrays.asList("netflix", "dhoni", "harini", "obama", "quark", "machintosh"));

    int i = findIndexOfNewElement(array2, array1);
    System.out.println(i + " = " + array2.get(i)); // 4 = quark
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM