Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8644139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:13:21+00:00 2026-06-12T12:13:21+00:00

I came across the following Dynamic Programming problem. You have a grid of integers

  • 0

I came across the following Dynamic Programming problem.

You have a grid of integers (so including negative numbers). Find the rectangle that has the largest sum of numbers.

Any idea how to do it for a whole matrix?

I solved it for a single array, so I pretty much followed what longest increasing subsequnce does, but only for contiguous numbers.

def array_largest_block(sequence)
  len = sequence.size
  parents = [nil]*len
  my_largest = sequence
  largest = sequence.max

  for index in (1...len)
    if my_largest[index] < my_largest[index] + my_largest[index - 1]
      my_largest[index] = my_largest[index] + my_largest[index - 1]
      parents[index] = index - 1
      largest = [largest, my_largest[index]].max
    end
  end

  end_index_of_largest_block = my_largest.find_index(largest)
  i = end_index_of_largest_block
  res = []
  res << sequence[i]
  while !parents[i].nil?
    i = parents[i]
    res << sequence[i]
  end
  return {l_sum: largest, start: i, end: end_index_of_largest_block}
end

So My thinking is,

  1. find the sum of each square in the matrix (just 1×1 squares)
  2. save the max for a possible answer
  3. Run the same thing starting from smallest possible rectangle and calculate all of them until you find the max. Which is the DB part.

Any ideas? Or if you guys don’t knwo the exact solution, which DP type algorithm should i look at?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:13:22+00:00Added an answer on June 12, 2026 at 12:13 pm

    This can be done in O(N^3), where N is the size of the matrix.

    You basically choose the left and right column of the rectangle and then scan through the rows in linear time(using precomputed sums).

    int totalBestSum = -10000000;
    for (int leftCol = 1; leftCol <= N; leftCol++)
       for (int rightCol = leftCol; rightCol <= N; rightCol++)
       {
          int curSum = 0, curBestSum = -10000000;
          for (int row = 1; row <= N; row++) {
             int rowSum = sumBetween(leftCol, rightCol, row);
             curSum += rowSum;
             if (curSum > curBestSum) curBestSum = curSum;
             if (curSum < 0) curSum = 0;                   
          }
    
          if (curBestSum > totalBestSum) totalBestSum = curBestSum;
       } 
    

    sumBetween is a function returning the sum of the numbers on a particular row between two columns. It can be implemented in constant time, using precomputed sums.

    int sumBetween(int leftCol, int rightCol, int row)
    {
        return sum[row][rightCol] - sum[row][leftCol - 1];
    }
    

    To compute the sum array:

    for (int row = 1; row <= N; row++)
       for (int col = 1; col <= N; col++)
          sum[row][col] = sum[row][col - 1] + matrix[row][col];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
I came across the following weird chunk of code.Imagine you have the following typedef:
I came across an interesting problem today whilst implementing a feature into a dynamic
I was working with spring-ehcache-annotations and came across following scenario: I have a simple
I came across the following issue: I have a main simple page on which
i came across the following program for calculating large factorials(numbers as big as 100)..
I came across the following code in a book recently. It says that we
I came across the following problem while reading ...just cant get the logic behind
I came across following code and don't know what does having from twice mean
I came across the following code : int i; for(; scanf(%s, &i);) printf(hello); As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.