-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediumOfTwoSortedArray.scala
64 lines (54 loc) · 1.72 KB
/
MediumOfTwoSortedArray.scala
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
package algorithm.divideAndConquer
/*
https://leetcode.com/problems/median-of-two-sorted-arrays/description/
*/
object MediumOfTwoSortedArray {
def apply(list1: Array[Int],
list1Length: Int,
list2: Array[Int],
list2Length: Int): Double = {
if ((list1Length + list2Length) % 2 == 1 ) {
findKth(list1, list2, (list1Length + list2Length) / 2, 0, list1Length - 1, 0, list2Length - 1)
} else {
(
findKth(list1, list2, (list1Length + list2Length) / 2, 0, list1Length - 1, 0, list2Length - 1)
+ findKth(list1, list2, (list1Length + list2Length) / 2 -1, 0, list1Length - 1, 0, list2Length - 1)
) * 0.5
}
}
private def findKth(listA: Array[Int],
listB: Array[Int],
k : Int,
aStart: Int,
aEnd: Int,
bStart: Int,
bEnd: Int
): Int = {
val aLength = aEnd - aStart + 1
val bLength = bEnd - bStart + 1
if (aLength == 0) {
return listB(bStart + k)
}
if (bLength == 0) {
return listA(aStart + k)
}
if (k == 0) {
return Math.min(listA(aStart), listB(bStart))
}
var aMid = aLength * k / (aLength + bLength)
var bMid = k - aMid - 1
aMid = aMid + aStart
bMid = bMid + bStart
if (listA(aMid) > listB(bMid)) {
val newK = k - (bMid - bStart + 1)
val newAEnd = aMid
val newBStart = bMid + 1
findKth(listA, listB, newK, aStart, newAEnd, newBStart, bEnd)
} else {
val newK = k - (aMid - aStart + 1)
val newAStart = aMid + 1
val newBEnd = bMid
findKth(listA, listB, newK, newAStart, aEnd, bStart, newBEnd)
}
}
}