forked from Wang-Jun-Chao/coding-interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test36.java
83 lines (72 loc) · 2.42 KB
/
Test36.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Author: 王俊超
* Date: 2015-06-11
* Time: 17:20
* Declaration: All Rights Reserved !!!
*/
public class Test36 {
public static int inversePairs(int[] data) {
if (data == null || data.length < 1) {
throw new IllegalArgumentException("Array arg should contain at least a value");
}
int[] copy = new int[data.length];
System.arraycopy(data, 0, copy, 0, data.length);
return inversePairsCore(data, copy, 0, data.length - 1);
}
private static int inversePairsCore(int[] data, int[] copy, int start, int end) {
if (start == end) {
copy[start] = data[start];
return 0;
}
int length = (end - start) / 2;
int left = inversePairsCore(copy, data, start, start + length);
int right = inversePairsCore(copy, data, start + length + 1, end);
// 前半段的最后一个数字的下标
int i = start + length;
// 后半段最后一个数字的下标
int j = end;
// 开始拷贝的位置
int indexCopy = end;
// 逆序数
int count = 0;
while (i >= start && j >= start + length + 1) {
if (data[i] > data[j]) {
copy[indexCopy] = data[i];
indexCopy--;
i--;
count += j - (start + length); // 对应的逆序数
} else {
copy[indexCopy] = data[j];
indexCopy--;
j--;
}
}
for (; i >= start; i--) {
copy[indexCopy] = data[i];
indexCopy--;
i--;
}
for (; j >= start + length + 1; j--) {
copy[indexCopy] = data[j];
indexCopy--;
j--;
}
return count + left + right;
}
public static void main(String[] args) {
int[] data = {1, 2, 3, 4, 7, 6, 5};
System.out.println(inversePairs(data)); // 3
int[] data2 = {6, 5, 4, 3, 2, 1};
System.out.println(inversePairs(data2)); // 15
int[] data3 = {1, 2, 3, 4, 5, 6};
System.out.println(inversePairs(data3)); // 0
int[] data4 = {1};
System.out.println(inversePairs(data4)); // 0
int[] data5 = {1, 2};
System.out.println(inversePairs(data5)); // 0
int[] data6 = {2, 1};
System.out.println(inversePairs(data6)); // 1
int[] data7 = {1, 2, 1, 2, 1};
System.out.println(inversePairs(data7)); // 3
}
}