Question 100: Evaluate the maximum two-dimensional matrix (element and maximum) in a matrix)

Source: Internet
Author: User

I. Question:

Returns the largest two-dimensional matrix (element and maximum) in a matrix. For example:
1 2 0 3 4
2 3 4 5 1
1 1 5 3 0
The largest in:
4 5
5 3
Requirements: (1) write algorithms; (2) analyze time complexity; (3) use C to write key code

Ii. Analysis:

Assume that the result of the maximum sub-matrix is the sub-matrix from row R to row K and from column I to column J,
As shown below (ARI indicates a [r] [I], assuming that the array subscript starts from 1 ):

| A11 ...... A1i ...... A1j ...... A1N |
| A21 ...... A2i ...... A2j ...... A2n |
.....

| AR1 ...... Ari ...... ARJ ...... Arn | row R
...
...... |
V
| Ak1 ...... Aki ...... Akj ...... Akn | row K...

.....
| An1 ...... Ani ...... Anj ...... Ann |

Then, we add the same columns in each row from row R to row K. We can get a one-dimensional array as follows:
(AR1 + ...... + Ak1, AR2 + ...... + Ak2 ,......, Arn + ...... + Akn)
From this we can see that the last thing we want is the maximum sub-segment and problem of this one-dimensional array,
At this point, we have converted the problem into the problem that has been solved above.

3. Source Code (the following source code is the code for finding the maximum submatrix of N rows of n columns)

# Include <iostream> <PRE name = "code" class = "html"> using namespace STD; int maxsubarray (int A [], int N) {int B = 0, sum = A [0]; for (INT I = 0; I <n; I ++) {If (B> 0) B + = A [I]; else B = A [I]; If (B> sum) sum = B;} return sum;} int maxsubmatrix (INT array [] [3], int N) {int I, J, K, max = 0, sum =-100000000; int B [3]; for (I = 0; I <n; I ++) {for (k = 0; k <n; k ++) // initialize B [] {B [k] = 0 ;}for (j = I; j <N; j ++) // Add row I to row J, and obtain the maximum value {for (k = 0; k <n; k ++) for each addition) {B [k] + = array [J] [k];} max = maxsubarray (B, k); If (max> sum) {sum = max ;}}} return sum;} int main () {int n = 3; int array [3] [3] = {1, 2, 3}, {-1,-2, -3 },{ 4, 5, 6 }}; cout <"maxsum:" <maxsubmatrix (array, n) <Endl ;}</PRE> <p>The algorithm complexity is O (n * n)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.