簡體   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"));

我們必須使用相同的字符串和相同的順序來排列列表,但是在array2中,一個字符串是不同的。 我必須使用O(LogN)復雜度找出該字符串及其位置。

我已經解決了使用O(N)復雜度,但我想要O(LogN)復雜度。

我的解決方案如下:

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);
    }
}

但是它給了O(N)復雜性。

這是一種可與任何類的列表一起使用的解決方案(只要其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;
    }
}

輸出:

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

您可以像這樣進行二進制搜索:

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