17 lines
481 B
Text
17 lines
481 B
Text
with javascript_semantics
|
|
function maxSubseq(sequence s)
|
|
integer maxsum = 0, first = 1, last = 0
|
|
for i=1 to length(s) do
|
|
integer sumsij = 0
|
|
for j=i to length(s) do
|
|
sumsij += s[j]
|
|
if sumsij>maxsum then
|
|
{maxsum,first,last} = {sumsij,i,j}
|
|
end if
|
|
end for
|
|
end for
|
|
return s[first..last]
|
|
end function
|
|
? maxSubseq({-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1})
|
|
? maxSubseq({})
|
|
? maxSubseq({-1, -5, -3})
|