-
Notifications
You must be signed in to change notification settings - Fork 17
/
inverse_pairs.php
59 lines (50 loc) · 1.47 KB
/
inverse_pairs.php
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
<?php
/**
* 剑指 Offer 系列:数组中的逆序对
* Author:学院君
*/
/**
* 套用归并排序的流程来统计数组中的逆序对
* @param $nums
* @return mixed
*/
function inversePairs($nums)
{
if (count($nums) <= 1) {
return 0;
}
$copy = $nums;
return inversePairsCore($nums, $copy, 0, count($nums) - 1);
}
function inversePairsCore(&$nums, &$copy, $start, $end)
{
if ($start >= $end) {
$copy[$start] = $nums[$start];
return 0;
}
$length = floor(($end - $start) / 2);
$left = inversePairsCore($copy, $nums, $start, $start + $length);
$right = inversePairsCore($copy, $nums, $start + $length + 1, $end);
$i = $start + $length; //前半段最后一个数字下标
$j = $end; //后半段最后一个数字下标
$indexCopy = $end;
$count = 0;
while ($i >= $start && $j >= $start + $length + 1) {
if ($nums[$i] <= $nums[$j]) {
$copy[$indexCopy--] = $nums[$j--];
} else {
$copy[$indexCopy--] = $nums[$i--];
$count += $j - $start - $length; // 前面的数大于后面的数,存在逆序对,且数目为已排序子数组区间跨度值
}
}
for (; $i >= $start; $i--) {
$copy[$indexCopy--] = $nums[$i];
}
for (; $j >= $start + $length + 1; $j--) {
$copy[$indexCopy--] = $nums[$j];
}
return $left + $right + $count;
}
$nums = [7, 5, 6, 4];
$count = inversePairs($nums);
var_dump($count); // 5