-
Notifications
You must be signed in to change notification settings - Fork 0
/
295.php
87 lines (74 loc) · 1.93 KB
/
295.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
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
86
87
<?php
class MedianFinder {
public $arr;
/**
* initialize your data structure here.
*/
function __construct() {
$this->arr = [];
}
/**
* @param Integer $num
* @return NULL
*/
function addNum($num) {
if (count($this->arr) == 0) {
$this->arr[] = $num;
return;
}
$left = 0;
$right = count($this->arr) - 1;
if ($this->arr[0] >= $num) {
array_unshift($this->arr, $num);
return;
}
if ($this->arr[$right] <= $num) {
$this->arr[] = $num;
return;
}
while ($left < $right) {
$mid = (int)($left + $right) / 2;
if ($num <= $this->arr[$mid]) {
$right = $mid;
} else {
$left = $mid + 1;
}
}
for ($i = count($this->arr); $i > $left; $i --) {
$this->arr[$i] = $this->arr[$i - 1];
}
$this->arr[$left] = $num;
return;
}
/**
* @return Float
*/
function findMedian() {
$len = count($this->arr);
if ($len & 1) {
return $this->arr[(int)($len / 2)];
}
return (float)($this->arr[$len / 2 - 1] + $this->arr[$len / 2]) / 2;
}
}
/**
* Your MedianFinder object will be instantiated and called as such:
* $obj = MedianFinder();
* $obj->addNum($num);
* $ret_2 = $obj->findMedian();
*/
$obj = new MedianFinder();
$obj->addNum(1);var_dump($obj->arr);
var_dump($obj->findMedian());
$obj->addNum(2);var_dump($obj->arr);
var_dump($obj->findMedian());
$obj->addNum(3);var_dump($obj->arr);
var_dump($obj->findMedian());
$obj->addNum(14);var_dump($obj->arr);
var_dump($obj->findMedian());
$obj->addNum(35);var_dump($obj->arr);
var_dump($obj->findMedian());
$obj->addNum(19);var_dump($obj->arr);
var_dump($obj->findMedian());
$obj->addNum(34);var_dump($obj->arr);
var_dump($obj->findMedian());