|
| 1 | +class Solution { |
| 2 | + |
| 3 | + /** |
| 4 | + * @param Integer[][] $points |
| 5 | + * @param Integer $K |
| 6 | + * @return Integer[][] |
| 7 | + */ |
| 8 | + function kClosest($points, $K) { |
| 9 | + if ($points == null || count($points) == 0 || $K == 0) { |
| 10 | + throw new Exception("Input should not be empty"); |
| 11 | + } |
| 12 | + self::quickSelect($points, $K, 0, count($points) - 1); |
| 13 | + return array_slice($points, 0, $K); |
| 14 | + } |
| 15 | + |
| 16 | + function quickSelect(&$points, $K, $low, $high) { |
| 17 | + $partition = self::partition($points, $low, $high); |
| 18 | + if ($partition == $K || $partition == $K - 1) { |
| 19 | + // If the location is K after partition, the points before it can be used; |
| 20 | + // If the location is K-1, the points before it plus itself can be used. |
| 21 | + return; |
| 22 | + } else if ($partition < $K - 1) { |
| 23 | + self::quickSelect($points, $K,$partition + 1, $high); |
| 24 | + } |
| 25 | + else { |
| 26 | + self::quickSelect($points, $K, $low, $partition - 1); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * The low is smaller |
| 32 | + * @param points |
| 33 | + * @param low |
| 34 | + * @param high |
| 35 | + * @@return The index that points are closer before it, and points are further after it. |
| 36 | + */ |
| 37 | + function partition(&$points, $low, $high) { |
| 38 | + $pivotPoint = $points[$high]; |
| 39 | + $partitionIndex = $low; |
| 40 | + |
| 41 | + for ($i = $low; $i < $high; $i++) { |
| 42 | + if (self::compare($points[$i], $pivotPoint) <= 0) { |
| 43 | + self::swap($points, $i, $partitionIndex); |
| 44 | + $partitionIndex++; |
| 45 | + } |
| 46 | + } |
| 47 | + self::swap($points, $partitionIndex, $high); |
| 48 | + return $partitionIndex; |
| 49 | + } |
| 50 | + |
| 51 | + function compare($point1, $point2) { |
| 52 | + return $point1[0] * $point1[0] + $point1[1] * $point1[1] - |
| 53 | + $point2[0] * $point2[0] - $point2[1] * $point2[1]; |
| 54 | + } |
| 55 | + |
| 56 | + function swap(&$points, $location1, $location2) { |
| 57 | + $tempPoint = $points[$location1]; |
| 58 | + $points[$location1] = $points[$location2]; |
| 59 | + $points[$location2] = $tempPoint; |
| 60 | + } |
| 61 | +} |
0 commit comments