-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathContainerWithMostWater.php
45 lines (39 loc) · 1003 Bytes
/
ContainerWithMostWater.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
<?php
declare(strict_types=1);
namespace leetcode;
class ContainerWithMostWater
{
public static function maxArea(array $height): int
{
$n = count($height);
if ($n <= 0) {
return 0;
}
[$x, $y, $ans] = [0, $n - 1, 0];
while ($x !== $y) {
if ($height[$x] < $height[$y]) {
$area = $height[$x] * ($y - $x);
$x++;
} else {
$area = $height[$y] * ($y - $x);
$y--;
}
$ans = max($ans, $area);
}
return $ans;
}
public static function maxArea2(array $height): int
{
$n = count($height);
if ($n <= 0) {
return 0;
}
[$x, $y, $ans] = [0, $n - 1, 0];
while ($x < $y) {
$area = min($height[$x], $height[$y]) * ($y - $x);
$ans = max($ans, $area);
$height[$x] < $height[$y] ? $x++ : $y--;
}
return $ans;
}
}