Ask the interview question: find the largest two-dimensional matrix in the matrix

topic description

find the largest two-dimensional matrix (element and maximum) in a matrix, such as
12034
23451
11530
the largest is
45
53
requirements: (1) write out the algorithm; (2) analyze time complexity;

sources of topics and their own ideas

A question I encountered in the recent interview, I was interviewing PHP developer

Jan.14,2022

/ *

* find the largest two-dimensional matrix (element and maximum) in a matrix

* @ param unknown $arr two-dimensional matrix

* @ param unknown $row two-dimensional matrix rows

* @ param unknown $col two-dimensional matrix columns

* @ return unknown [] []

* /
php

function test1($arr,$row,$col){
$sum0 = 0;
for ($i=0;$i<$row-1;$iPP){
 for ($j=0;$j<$col-1;$jPP){
 $sum1 = $arr[$i][$j]+$arr[$i][$j+1]+$arr[$i+1][$j]+$arr[$i+1][$j+1];
 if ($sum1 > $sum0){
 $l = $i;
 $m = $j;
 $sum0 = $sum1;
 }
 }
}
$res= array(array($arr[$l][$m],$arr[$l][$m+1]),
array($arr[$l+1][$m],$arr[$l+1][$m+1]));
return $res;
}
$arr = array(array(1,2,0,3,4),array(2,3,4,5,1),array(1,1,5,3,0));
print_r(test1($arr,3, 5));

javascript

            function test1(arr,row,col){
                var sum0 = l = m = 0;
                for (var i=0;i<row-1;iPP){
             for (var j=0;j<col-1;jPP){
             var sum1 = arr[i][j]+arr[i][j+1]+arr[i+1][j]+arr[i+1][j+1];
             if (sum1 > sum0){
             l = i;
             m = j;
             sum0 = sum1;
             }
             }
                }
                var res = [[arr[l][m],arr[l][m+1]],arr[l+1][m],arr[l+1][m+1]];
                return res;
            }
            var arr = [[1,2,0,3,4],[2,3,4,5,1],[1,1,5,3,0]];
            console.log(test1(arr,3, 5));

/**
 * @param $x 
 * @param $y 
 * @param $arr 
 * @return array value
 */
function get_max_value($x,$y,$arr){
    $count=count($arr);
    if($x*$y!=$count){//
        return false;
    }
    $list=array_chunk($arr,$x);//
    $max=0;//0
    for($b1=1;$b1<$y;$b1PP){//y
        for($a1=0;$a1<$x-1;$a1PP){//x
            $s=$list[$b1-1][$a1]+$list[$b1-1][$a1+1]+$list[$b1][$a1]+$list[$b1][$a1+1];
            if($s>$max){
                $max=$s;
                unset($max_arr);
                $max_arr[]=[$list[$b1-1][$a1],$list[$b1-1][$a1+1],$list[$b1][$a1],$list[$b1][$a1+1]];
            }elseif($s==$max){//
                $max_arr[]=[$list[$b1-1][$a1],$list[$b1-1][$a1+1],$list[$b1][$a1],$list[$b1][$a1+1]];
            }
        }
    }
    return $max_arr;
}
$arr=[1 ,2, 0, 3, 4,
2, 3, 4, 5, 1,
1, 1, 5, 3, 0];
print_r(get_max_value(5,3,$arr));
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b40c50-2c555.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b40c50-2c555.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?