Algorithms for solving several criteria

find in rectangle A [n] 2 rectangles A [i] and A [j] for which A [i] .width> A [j] .width and A [i] .length - A [j] .length is the most long.

Is there a way to reduce complexity to O (nlogn)? I cannot find a way to search for O (logn) for the second rectangle. Sorting doesn't seem to help here because of the possibility that the 2 criteria are completely opposite to each other. Maybe I'm just wrong? direct me on the right path, please. Thanks.

Note. Assigning homework using another object and using 2 criteria instead of 3, but the context is the same.

+4
source share
2 answers

, , OP:

. , , . , ( j). , A [i].length-A [j].length, A [i].width > A [j].width

: O(n*Log(n)), O(n).

+1

java ::

  import java.util.Arrays;
  import java.util.Comparator;
  import java.util.Random;

  public class RequiredRectangle {

    public static void main(String[] args) {
        // test the method
        int n=10;
        Rectangle[] input = new Rectangle[n];
        Random r = new Random();
        for(int i=0;i<n;i++){
            input[i] = new Rectangle(r.nextInt(100)+1,r.nextInt(100)+1);
        }

        System.out.println("INPUT:: "+Arrays.deepToString(input));

        Rectangle[] output = getMaxLengthDiffAndGreaterBreadthPair(input);
        System.out.println("OUTPUT:: "+Arrays.deepToString(output));

    }

    public static Rectangle[] getMaxLengthDiffAndGreaterBreadthPair(Rectangle[] input){
        Rectangle[] output = new Rectangle[2];

        Arrays.sort(input, new Comparator<Rectangle>() {
            public int compare(Rectangle rc1,Rectangle rc2){
                return rc1.getBreadth()-rc2.getBreadth();
            }
        });

        int len=0;
        Rectangle obj1,obj2;
        for(int i=0;i<input.length;i++){
            obj2=input[i];
            for(int j=i+1;j<input.length;j++){
                obj1=input[j];
                int temp=obj1.getLength() - obj2.getLength();

                if(temp>len && obj1.getBreadth() > obj2.getBreadth()){
                    len=temp;
                    output[0]=obj1;
                    output[1]=obj2;
                }
            }
        }
        return output;
    }
  }

  class Rectangle{
    private int length;
    private int breadth;

    public int getLength(){
        return length;
    }

    public int getBreadth(){
        return breadth;
    }

    public Rectangle(int length,int breadth){
        this.length=length;
        this.breadth=breadth;
    }

    @Override
    public boolean equals(Object obj){
        Rectangle rect = (Rectangle)obj;

        if(this.length==rect.length && this.breadth==rect.breadth)
            return true;

        return false;
    }

    @Override
    public String toString(){
        return "["+length+","+breadth+"]";
    }
  }

`

:

INPUT:: [[8,19], [68,29], [92,14], [1,27], [87,24], [57,42], [45,5], [66,27], [45,28], [29,11]]
OUTPUT:: [[87,24], [8,19]]
0

Source: https://habr.com/ru/post/1607382/


All Articles