简体   繁体   English

提高此算法的时间复杂度?

[英]Improve time complexity of this algorithm?

I have created this algorithm which all it does is finds the pairs of integers that have the same product and the integers in the pairs have to be different. 我创建了此算法,它的全部作用是查找具有相同乘积的整数对,并且该对中的整数必须不同。 The product must not exceed the 1024. This is the easiest way I could come up to doing it, is there a way I could improve the efficiency and time complexity of this algorithm? 乘积不能超过1024。这是我做起来的最简单的方法,有没有办法可以提高此算法的效率和时间复杂度?

Thanks 谢谢

import java.util.ArrayList;

public class Pairs {

    public static void main(String [] args){

        int nums[] = new int[1024];
        for(int i = 1;i<=1024;i++){
            nums[i-1] = i;
        }
        findPairs(nums);

    }

    static void findPairs(int [] nums){


        ArrayList<IntPair> pairs = new ArrayList<IntPair>();
        ArrayList<Products> products = new ArrayList<Products>();

        IntPair tempObject;
        Products tempProduct;
        int tempMultiplication = 0;

        for(int i =0;i<nums.length;i++){

            for(int j=0;j<nums.length;j++){

                tempObject = new IntPair(nums[i],nums[j]);
                pairs.add(tempObject);

                }


            }

        for(IntPair p:pairs){
            tempProduct = new Products(p.x,p.y);

            if(tempProduct.product <= 1024){
                products.add(tempProduct);
            }


        }


        for(int i = 0;i<products.size();i++){

            tempMultiplication = products.get(i).product;

            for(int j = 0;j<products.size();j++){

                 if(products.get(j).product == tempMultiplication)
                 {
                    if(products.get(i).x == products.get(j).x || products.get(i).y == products.get(j).y) {


                     }
                    else if (products.get(i).x == products.get(j).y || products.get(j).x == products.get(j).y || products.get(i).x == products.get(i).y){


                    }
                     else{
                         System.out.println("Matching pair found:("+ products.get(i).x + ","+products.get(i).y+")"  + "("+ products.get(j).x + ","+products.get(j).y+")" );
                     }


                 }


            }

        }


    }


}

You can't improve the time complexity of your algorithm. 您无法提高算法的时间复杂度 It is O(n^2) , which is as good as you can do if you are comparing all pairs in the input. 它是O(n^2) ,如果您要比较输入中的所有对,则效果会很好。

But that's not to say that you can't improve the running time . 但这并不是说您不能缩短运行时间 Not all O(n^2) algorithms are equally good; 并非所有O(n^2)算法都一样好; you can make it run faster by getting it to do less work; 您可以通过减少工作量来使其运行更快; in this case, that basically means skipping checking of numbers whose product can't be equal. 在这种情况下,这基本上意味着跳过对乘积不能相等的数字的检查。

Put the pairs into buckets which have equal product: 将对放入具有相同乘积的水桶中:

Map<Integer, List<int[]>> map = new HashMap<>();
for (int i = 0; i < nums.length; ++i) {
  for (int j = i+1; j < nums.length; ++j) {
    if (nums[i] == nums[j] || nums[i]*nums[j] > 1024) continue;

    map.computeIfAbsent(nums[i]*nums[j], ArrayList::new)
        .add(new int[] {nums[i], nums[j]});
  }
}

Then just iterate all of the pairs of pairs within each bucket: 然后,只需迭代每个存储桶中的所有对对即可:

for (List<int[]> list : map.values()) {
  for (int i = 0; i < list.size(); ++i) {
    for (int j = 1; j < list.size(); ++j) {
      System.out.printf(
          "Matching pair found: %s %s%n",
          Arrays.toString(list.get(i)),
          Arrays.toString(list.get(j)));
    }
  }
}

This is still O(n^2) in the worst case, which is where all pairs of numbers have an equal product, meaning that the iteration over pairs with equal product is just iterating all pairs. 在最坏的情况下,仍然是O(n^2) ,这是所有数字对具有相等乘积的意思,这意味着具有相等积的对的迭代只是对所有对进行迭代。 I doubt that's actually possible, though, without all numbers being equal, which is prohibited by the check that the numbers in the pair are distinct. 我怀疑这实际上是否可能,而不是所有数字都相等,这是通过检查该对中的数字是否不同来禁止的。

You may be able to shave off a bit of time by sorting nums first, and then breaking the top inner loop once nums[i]*nums[j] > 1024 rather than continuing; 您可以通过先对nums进行排序,然后一次使nums[i]*nums[j] > 1024而不是继续进行而中断顶部内部循环来节省时间。 it depends on how big nums is as to whether it is worth sorting it (having potentially copied it, if you don't want to change the input array). 这取决于有多大nums是它是否值得选它(已经潜在复制它,如果你不想改变输入数组)。

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

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