-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));
ランダムな行数で2列のみの2D配列を入力し、「値」が同じ場合は「値」で並べ替え、最初の列の値で並べ替えます。あなたは、プライマリソートの "値"によって何を意味するのかを明確にすることはできますか?編集:私はそれを得た、私は説明と解答しています。 – prsvr
http://idownvotedbecau.se/noattempt/(例えば、[Comparator '](https://docs.oracle.com/javase/9/docs/api/java/util/Comparator.html))を書いてくださいあなたが説明したのとまったく同じです。 –
Andreas
@アンドレアス私はコンパレータソリューションを知っていますが、2番目の比較はどうですか?私は違いごとにソートした後に意味します。彼らが等しい差を持っているなら、列番号/値ごとにソートしたいのですが、どうしたらいいですか? – sandy