2017-12-26 7 views
-1

値が同じ場合は、値の差に基づいて多次元配列をソートします。最初の列をソートします。
Javaソリューションが必要です。 コードではない場合は、このタイプの問題を解決するためのアプローチやヒントを知りたいと思います。値の差に基づいて多次元配列をソートします(値が最初の列で同じソートの場合)

制約: 行の何がどんなことはないが、編集1列のないすなわち2

Example: 
int arr[][] = new int[5][2] 

    [0] [1] 
[0] 0 6 
[1] 0 7 
[2] 4 5 
[3] 2 3 
[4] 0 1 

Final Output: 

    [0] [1] 
[0] 0 1 
[1] 2 3 
[2] 4 5 
[3] 0 6 
[4] 0 7 

Explanation: 

difference between: arr[0][1] - arr[0][0] -> 6-0 -> 6 
difference between: arr[1][1] - arr[1][0] -> 7-0 -> 7 
difference between: arr[2][1] - arr[2][0] -> 5-4 -> 1 — same length ie 1 
difference between: arr[3][1] - arr[3][0] -> 3-2 -> 1 — same length ie 1 
difference between: arr[4][1] - arr[4[0] -> 1-0 -> 1 — same length ie 1 

I want to sort on the difference, in cases where difference is same I want to sort those with same difference on column [0] 

So in this case, 
The below 3 have same difference 
difference between: arr[2][1] - arr[2][0] -> 5-4 -> 1 — same difference ie 1 
difference between: arr[3][1] - arr[3][0] -> 3-2 -> 1 — same difference ie 1 
difference between: arr[4][1] - arr[4[0] -> 1-0 -> 1 — same difference ie 1 

Need to sort the above 3 based on there column[0] value 
arr[2][1] - arr[2][0] -> 5-4 -> 1 — value here is 4 ie arr[2][0] 
arr[3][1] - arr[3][0] -> 3-2 -> 1 — value here is 2 ie arr[3][0] 
arr[4][1] - arr[4[0] -> 1-0 -> 1 — value here is 1 ie arr[4][0] 

So the the one with the least value in column[0] should be first, 
In final output, 
arr[4][1] - arr[4[0] -> 1-0 -> 1 ——> 1st 
arr[3][1] - arr[3][0] -> 3-2 -> 1 ——> 2nd 
arr[4][1] - arr[4[0] -> 1-0 -> 1 ——> 3rd 
arr[0][1] - arr[0][0] -> 6-0 -> 6 ——> 4th 
arr[1][1] - arr[1][0] -> 7-0 -> 7 ——> 5th 

を固定することはできません:時間の複雑さを決定する際に必要な ヘルプ? すべての提案とコードをお寄せいただきありがとうございます。 以下は作業用コードの私のバージョンです: 私のコードの時間の複雑さを知りたいですか? 要するに、2D配列のソートの複雑さは何ですか? 1d配列 - > O(n.logn) 2d - > ??

private static int solve(int pathLength, int[][] floristIntervals) { 
     // TODO Auto-generated method stub 
     System.out.println(Arrays.deepToString(floristIntervals)); 
     Arrays.sort(floristIntervals, new Comparator<int[]>(){ 

      @Override 
      public int compare(int[] o1, int[] o2) { 
       // TODO Auto-generated method stub 

       /*System.out.println(o1[0]); 
       System.out.println(o1[1]); 
       System.out.println(o2[0]); 
       System.out.println(o2[1]);*/ 
       if(o2[1]-o2[0] == o1[1]-o1[0]){ 
        if(o2[0] > o1[0]){ 
         return -1; 
        } 
        return 1; 
       } 
       if (o2[1]-o2[0] > o1[1]-o1[0]) 
        return 1; 
       else 
        return -1; 
      } 

     }); 
     System.out.println(Arrays.deepToString(floristIntervals)); 
+0

ランダムな行数で2列のみの2D配列を入力し、「値」が同じ場合は「値」で並べ替え、最初の列の値で並べ替えます。あなたは、プライマリソートの "値"によって何を意味するのかを明確にすることはできますか?編集:私はそれを得た、私は説明と解答しています。 – prsvr

+1

http://idownvotedbecau.se/noattempt/(例えば、[Comparator '](https://docs.oracle.com/javase/9​​/docs/api/java/util/Comparator.html))を書いてくださいあなたが説明したのとまったく同じです。 – Andreas

+0

@アンドレアス私はコンパレータソリューションを知っていますが、2番目の比較はどうですか?私は違いごとにソートした後に意味します。彼らが等しい差を持っているなら、列番号/値ごとにソートしたいのですが、どうしたらいいですか? – sandy

答えて

0

OK、ので、ここで可能な解決策がある:

package com.company; 

import java.util.Arrays; 
import java.util.Comparator; 
import java.util.Scanner; 

public class Main { 

    public static void main(String[] args) { 

     // creating Scanner and declaring and initializing our 2D array 
     Scanner in = new Scanner(System.in); 
     System.out.print("Enter number of rows for your 2D array: "); 
     int rows = in.nextInt(); 
     int[][] arr = new int[rows][2]; 

     // asking user for values to assign to our array 
     for (int i = 0; i < rows; i++) { 
      for (int j = 0; j < 2; j++) { 
       System.out.print("Enter value for row " + i + " column " + j + ": "); 
       arr[i][j] = in.nextInt(); 
      } 
     } 

     // create a Comparator that compares by result of reduction of 2nd and first column in each array, then if the 
     // results are equal (0), we compare by first column 
     Comparator<int[]> cmp = Comparator.<int[]>comparingInt(x -> x[1] - x[0]).thenComparing(Comparator.comparingInt(x -> x[0])); 

     // we make a stream out of the 2D array, we sort inner elements (single dimensional arrays) by the Comparator we created and we print out results 
     Arrays.stream(arr) 
       .sorted(cmp) 
       .forEach(x -> System.out.println(Arrays.toString(x))); 

    } 

} 

入力配列:

[0] [1] 

[0] 0 6 

[1] 0 7 

[2] 4 5 

[3] 2 3 

[4] 0 1 

出力配列:

[0, 1] 
[2, 3] 
[4, 5] 
[0, 6] 
[0, 7] 

あなたがより多くの質問がある場合は確認して下さい。

関連する問題