kvtoraman's blog

By kvtoraman, 10 years ago, In English

You will read the number N (1<=N<=100) than i will give you N*N numbers. I want you to find the rectangle which is has the most sum and print its sum. Input: 5 1 2 3 4 5 1 -5 -20 -40 4 -8 -7 5 6 2 -5 2 -4 -8 9 1 -5 -8 -5 2 Output: 22 Is this problem has a solution better than o(n^4) ?

 
 
 
 
  • Vote: I like it
  • +3
  • Vote: I do not like it

»
10 years ago, # |
  Vote: I like it +6 Vote: I do not like it

There is O(n3) solution. I'm not sure that it is optimal solution, but it is easy. Let's fix the upper and the lower rows of answer (O(n2)). Let's replace each column by its sum. Now we have a sequence of integers and we have to find subsegment with maximal sum. It is a standart problem and it can be solved greedily by two pointers method (O(n)). Hint: use prefix sums.

»
10 years ago, # |
  Vote: I like it +4 Vote: I do not like it
»
10 years ago, # |
  Vote: I like it +3 Vote: I do not like it
  • »
    »
    10 years ago, # ^ |
      Vote: I like it -6 Vote: I do not like it

    Thanks but any english version?

    • »
      »
      »
      10 years ago, # ^ |
        Vote: I like it +7 Vote: I do not like it

      I sudgest you to use google translate or look for another solution in the Internet. Or you can learn russian :)

»
10 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

if you find english version give link please

»
10 years ago, # |
  Vote: I like it 0 Vote: I do not like it

and me :)