d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Algorithm Problem > 25 Fg For Help
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
May 29 2013 06:46pm
Here's the problem, my solution is below, wondering if I did this right.



a) Iterate through m of the array, getting the max contiguous sum from 0 to n for each m in the array, save the largest sum into T

b.)
Code
t = 0
for i = 1 to m do
       m = o
       for j = 1 to n do
           s = 0
           for k = j to n do
               s = s + a[k]
               m = max( m , s )
           end for
       end for
       t = max( m, t )
end for


c) (summation from i = 1 to m) (summation of j = 1 to n) (summation of k = i to n) 1

d) kinda stuck here

I got:

(summation from i = 1 to m) (summation of j = 1 to n) (summation of k = i to n) 1 = (summation from i = 1 to m) (summation of j = 1 to n) n - i + 1 = (summation from i = 1 to m) ????


This post was edited by lopelurag on May 29 2013 06:49pm
Member
Posts: 16,662
Joined: Nov 24 2007
Gold: 15,245.00
Trader: Trusted
May 29 2013 11:03pm
When you write "a[k]", do you mean a[i;k] ?

If not, I don't understand the problem.

If yes, your algorithm finds the maximum horizontal finishing continuous sum. And not the maximum sum of a rectangle.

A brute algorithm could be :
A continuous rectangle is defined by 4 integers : 1 =< i =< k =< m and 1 =< j =< l =< n.
(i,k) and (j,l) are the coordonates of 2 opposite corners of the rectangle.

Code
S = 0
For i = 1 to m
  For j = 1 to n
     For k = i to m
        For l = j to n
           T = sum in the rectangle (i;k)-(j;l)    <- this part requires another, easy, loop
           S = max (S;T)
        end
     end
  end
end
Display S


??

To be continued...
Go Back To Homework Help Topic List
Add Reply New Topic New Poll