forked from Alexchent/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_array.php
55 lines (49 loc) · 1.01 KB
/
max_array.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
<?php
/**
* Created by PhpStorm.
* User: chentao
* Date: 2019/1/10
* Time: 2:21 PM
*/
/**
* 求一个矩阵中最大的二维矩阵(元素和最大)。如:
* 1 2 0 3 4
* 2 3 4 5 1
* 1 1 5 3 0
* 中最大的是:
* 4 5
* 5 3
*/
$arr = [
[1,2,0,3,4],
[2,3,4,5,1],
[1,1,5,3,0]
];
$res = find_max_array($arr);
var_dump($res);
/**
* @param $arr
* @return array
* 时间复杂度 O(n²)
*/
function find_max_array($arr)
{
$maxSum = 0; //最大二维矩阵的元素和
$x = 0; $y = 0; //最大二维矩阵的左上坐标点
$row = count($arr) -1;
$col = count($arr[0]) -1;
for($i=0; $i<$row; $i++) {
for ($j=0;$j< $col; $j++) {
$tmp = $arr[$i][$j] + $arr[$i][$j+1] + $arr[$i+1][$j] + $arr[$i+1][$j+1];
if ($tmp > $maxSum) {
$maxSum = $tmp;
$x = $i;
$y = $j;
}
}
}
return array(
[$arr[$x][$y], $arr[$x][$y+1]],
[$arr[$x+1][$y], $arr[$x+1][$y+1]],
);
}