简体   繁体   English

证明:使用线性时间和常量空间检查两个整数数组是否相互排列

[英]Proof: Check if two integer arrays are permutations of each other using linear time and constant space

I was interested in creating a simple array problem with running time and space constraints. 我有兴趣创建一个运行时间和空间限制的简单数组问题。 It seems that I have found a solution to my problem. 似乎我找到了解决问题的方法。 Please read the initial description comment of the problem in the following java code: 请在以下java代码中阅读问题的初始描述注释:

 /*
 * Problem: Given two integer arrays, a and b, return whether array a is a permutation of array b.
 * Running time complexity: O(n)
 * Space complexity: O(1)
 * Example 1:
 * a = [1, 2, -3]
 * b = [2, 3, 1]
 * false
 * Example 2:
 * a = [1, 2, 3]
 * b = [0, 1]
 * false
 * Example 3:
 * a = [2, 7, 3, 5]
 * b = [5, 7, 2, 3]
 * true
 * Example 4:
 * a = [1, -2, 10000]
 * b = [10000, 1, -2]
 * true
 * Example 5:
 * a = [1, 2, 2, 3]
 * b = [3, 2, 1, 3]
 * false
 * Example 6:
 * a = [2, 2, 4, 4]
 * b = [3, 3, 3, 3]
 * false
 * Example 7:
 * a = [4, 4, 2, 2, 4, 4]
 * b = [4, 3, 3, 3, 3, 4]
 * false
 * ----------------------
 * Input is two space separated lines of integers
 * Output is true or false
 * Terminal Example:
 * 1 4 9 25
 * 4 25 9 2
 * false
 * ----------------------
 * Solution:
 * 1. Average displacement (delta) between the elements of array a and array b equals 0
 * AND
 * 2. xor-ing all of the values between the elements of array a and array b equals 0
 * AND
 * 3. mins are the same and maxs are the same
 * @author (David Brewster)
 * @version (27.01.2016) (requires java 1.8)
 */

import java.util.Scanner;
import java.util.Arrays;
public class ArrayProb
{
    public static int xorComparison(int[] a, int[] b, int i, int xortotal)
    {
        return i == a.length ?
                xortotal : xorComparison(a, b, i + 1, xortotal ^ a[i] ^ b[i]);
    }
    public static int deltaComparison(int[] a, int[] b, int i, int deltatotal)
    {
        return i == a.length ?
                deltatotal : deltaComparison(a, b, i + 1, deltatotal + a[i] - b[i]);
    }
    public static int minComparison(int[] a, int[] b, int i, int amin, int bmin)
    {
        return i == a.length ?
                amin-bmin : minComparison(a, b, i + 1,
                a[i] < amin ? a[i] : amin,
                b[i] < bmin ? b[i] : bmin);
    }
    public static int maxComparison(int[] a, int[] b, int i, int amax, int bmax)
    {
        return i == a.length ?
                amax-bmax : maxComparison(a, b, i+1,
                a[i] > amax ? a[i] : amax,
                b[i] > bmax ? b[i] : bmax);
    }
    public static boolean arePermutations(int[] a, int[] b)
    {
        if (a.length == b.length)
        {
            boolean d = xorComparison(a, b, 0, 0) == 0;
            boolean e = deltaComparison(a, b, 0, 0) == 0;
            boolean f = maxComparison(a, b, 0, Integer.MIN_VALUE, Integer.MIN_VALUE) == 0;
            boolean g = minComparison(a, b, 0, Integer.MAX_VALUE, Integer.MAX_VALUE) == 0;
            return d && e && f && g;
        }
        else
        {
            return false;
        }
    }
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int[] a = Arrays.stream(input.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        int[] b = Arrays.stream(input.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        System.out.println(arePermutations(a, b));
    }
}

Even thought the algorithm seems to work for most cases (at least all that I have tested so far), how would I go about proving that the solution is correct 100% of the time? 即使认为该算法似乎适用于大多数情况(至少到目前为止我测试的所有情况),我将如何在100%的时间内证明解决方案是正确的?

What you are basically trying to do is computing a fixed size fingerprint of each array (representing a multiset) and then determining equality of the multisets by comparing the fingerprints. 你基本上要做的是计算每个数组的固定大小指纹(代表一个多重集),然后通过比较指纹确定多重集的相等性。

This is clearly not possible because there is an infinite number of multisets but if space is constant, there is only a limited number of fingerprints. 这显然是不可能的,因为存在无限多个多集,但如果空间不变,则指纹数量有限。 So you will inevitably find examples where two different multisets map to the same fingerprint. 因此,您将不可避免地找到两个不同的多重集合映射到同一指纹的示例。

我不是Java文学,但你可以只对每个数组进行排序,然后迭代地检查排序的数组,以确定每个元素的相等性吗?

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

相关问题 如何检查两个字符串在O(n)时间中是否彼此置换? (java) - How can I check if two strings are permutations of each other in O(n) time? (java) 在恒定时间和线性空间中找到两个字符串的 LCP - finding LCP of two strings in constant time and linear space 检查两个 arrays 是否是循环排列 - Check if two arrays are cyclic permutations 程序的时间复杂度,确定两个字符串是否彼此置换 - Time complexity of program determining if two strings are permutations of each other 给定一个整数数组,在线性时间和常量空间中找到第一个缺失的正数 integer - Given an array of integers, find the first missing positive integer in linear time and constant space 如何判断两个 arrays 是否是彼此的排列(无法对它们进行排序) - How to tell if two arrays are permutations of each other (without the ability to sort them) 如何证明两个字符串是彼此的排列? - How to prove that two strings are permutations of each other? 在 Python 中检查两个字符串是否是彼此的排列 - Checking if two strings are permutations of each other in Python 在线性时间和恒定空间中以交替位置对两个排序序列进行排序 - Sorting an array with two sorted sequences in alternate position in linear time and constant space 在线性时间和恒定空间中排序前n个整数 - Sort first n integers in linear time and constant space
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM