-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution3.java
More file actions
85 lines (71 loc) · 2.09 KB
/
Solution3.java
File metadata and controls
85 lines (71 loc) · 2.09 KB
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
84
85
/**
* code by : 老王
* https://github.com/simplemain
*/
import java.util.Arrays;
public class Solution3
{
public static void main(String[] args)
{
final int[] nums1 = {1, 2, 3};
final int[] nums2 = {-1};
final Solution sol = new Solution3().new Solution();
final double result = sol.findMedianSortedArrays(nums1, nums2);
System.out.printf("%g\n", result);
}
class Solution
{
public double findMedianSortedArrays(int[] nums1, int[] nums2)
{
final ArrayWrapper a = new ArrayWrapper(nums1.length <= nums2.length ? nums1 : nums2);
final ArrayWrapper b = new ArrayWrapper(nums1.length > nums2.length ? nums1 : nums2);
final int aLength = a.len();
final int bLength = b.len();
final int totalCount = aLength + bLength;
final int leftCount = (totalCount + 1) / 2;
for (int aHead = 0, aTail = aLength - 1; aHead <= aTail; )
{
final int aMid = (aHead + aTail) / 2;
final int aLeftCount = aMid + 1;
final int bLeftCount = leftCount >= aLeftCount ? leftCount - aLeftCount : 0;
final int leftRealCount = aLeftCount + bLeftCount;
if (leftRealCount < leftCount)
{
aHead = aMid + 1;
}
else if (leftRealCount > leftCount)
{
aTail = aMid;
}
else
{
int aLeftValue = a.get(aMid);
int aRightValue = a.get(aMid + 1);
int bLeftValue = b.get(bLeftCount - 1);
int bRightValue = b.get(bLeftCount);
if (aLeftValue > bRightValue)
{
aTail = aMid;
}
else if (aRightValue < bLeftValue)
{
aHead = aMid + 1;
}
else // (aLeftValue <= bRightValue && bLeftValue <= aRightValue)
{
return totalCount % 2 == 0 ? (Math.max(aLeftValue, bLeftValue) + Math.min(aRightValue, bRightValue)) * 0.5 : Math.max(aLeftValue, bLeftValue);
}
}
}
return 0;
}
private class ArrayWrapper
{
final int[] array;
final int len;
ArrayWrapper(int[] array) { this.array = array; len = array.length + 2; }
int len() { return len; }
int get(int idx) { return idx >= 1 && idx < len - 1 ? array[idx - 1] : (idx <= 0 ? Integer.MIN_VALUE : Integer.MAX_VALUE); }
}
}
}