矩阵中求最大二维矩阵

1、求一个矩阵中最大的二维矩阵(元素和最大).如:

1 2 0 3 4

2 3 4 5 1

1 1 5 3 0

中最大的是:

4 5

5 3

要求:(1)写出算法;(2)分析时间复杂度;

function struct($arr, $row, $col)
{
    $sum=0;
    for ($i=0;$i<$row-1;$i++){
        for ($j=0;$j<$col;$j++){
            $sum1=$arr[$i][$j]+$arr[$i][$j+1]+$arr[$i+1][$j]+$arr[$i+1][$j+1];
            if($sum1>$sum){
                $row_num=$i;
                $clo_num=$j;
                $sum=$sum1;
            }
        }

    }
    $result=array(array($arr[$row_num][$clo_num],$arr[$row_num][$clo_num+1]),array($arr[$row_num+1][$clo_num],$arr[$row_num+1][$clo_num+1]));
    return $result;
}

$arr = array(array(1, 2, 0, 3, 4), array(2, 3, 4, 5, 1), array(1, 1, 5, 3, 0));
print_r(struct($arr, 3, 5));

  时间复杂度O(n*m)

原文地址:https://www.cnblogs.com/friendwrite/p/10416013.html

时间: 2024-07-29 23:12:36

矩阵中求最大二维矩阵的相关文章

【编程题目】求一个矩阵中最大的二维矩阵(元素和最大)

35.(矩阵)求一个矩阵中最大的二维矩阵(元素和最大).如:1 2 0 3 42 3 4 5 11 1 5 3 0中最大的是:4 55 3要求:(1)写出算法;(2)分析时间复杂度;(3)用 C 写出关键代码 早上灭小题! /* 35.(矩阵) 求一个矩阵中最大的二维矩阵(元素和最大).如: 1 2 0 3 4 2 3 4 5 1 1 1 5 3 0 中最大的是: 4 5 5 3 要求:(1)写出算法;(2)分析时间复杂度;(3)用 C 写出关键代码 */ #include <stdio.h>

IT公司100题-35- 求一个矩阵中最大的二维矩阵(元素和最大)

问题描述: 求一个矩阵中最大的二维矩阵(元素和最大).如: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 中最大的是: 4 5 9 10 分析: 2*2子数组的最大和.遍历求和,时间复杂度为O(mn). 代码实现: 1 // 35.cc 2 #include <iostream> 3 #include <climits> 4 using namespace std; 5 6 int find_max(int (*a)[5], int m, int n) { 7 in

lintcode 容易题:Search a 2D Matrix 搜索二维矩阵

题目: 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] 给出 target = 3,返回 true 挑战 O(log(n) + log(m)) 时间复杂度 解题: 1.最简单的方法就是遍历整个矩阵,时间复杂度:O(log(mn)),这个应该等于O(long(

lintcode 中等题:search a 2d matrix II 搜索二维矩阵II

题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没有重复的整数. 样例 考虑下列矩阵: [     [1, 3, 5, 7],     [2, 4, 7, 8],     [3, 5, 9, 10] ] 给出target = 3,返回 2 挑战 要求O(m+n) 时间复杂度和O(1) 额外空间 解题 直接遍历,时间复杂度是O(MN) public

20140920百度笔试题一道之二维矩阵查找

题目: 有这样一个二维矩阵A[N][N],满足j < k时, 1)a[i][j] < a[i][k]; 2)a[j][i] < a[k][i](其实就数据从左上角到右下角纵横方向上都递减),给定一个数target,如何快速搜索是否在这个矩阵中,是的话输出二维坐标,否则输出Null:(不妨假设数据不重复) 比如  12  34  56  78  90  96 13  35  57  79  91  97 14  36  58  80  93  98 15  37  59  81  94  

【LeetCode-面试算法经典-Java实现】【074-Search a 2D Matrix(搜索二维矩阵)】

[074-Search a 2D Matrix(搜索二维矩阵)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first

[LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

lintcode 搜索二维矩阵

题目:搜索二维矩阵 描述: 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] 给出 target = 3,返回 true ------------------------------------------- 开始用两个二分搜索,先找列,再找行.运行后提示超时... 1 c

搜索二维矩阵II

题目 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没有重复的整数. 解题 和判断一个数是否在这样的二维矩阵中一样,判断找到适合就结束了 这个题目有多个,找到一个的时候还要继续找,直到找完 public class Solution { /** * @param matrix: A list of lists of integers * @param: A number