Posts: 2,695
Threads: 12
Joined: Feb 2020
If you have two contiguous slices, a and b, the sum of a + b is only greater than the sum of slice b if the sum of slice a is positive. In other words, the slice with the maximum sum will only start with slice a if the sum of slice a is positive.
In my program subsum is the sum of slice a, and value is the sum of slice b (a slice containing just the next value). The program adds values to slice a, checking if the new sum is the highest sum seen so far. If the sum of slice a is less than zero it is discarded because including a will only decrease the sum of future slices. I also discard a when the sum of a is zero, choosing shorter slices over longer slices of the same value.
This is a good interview question.
Posts: 3,294
Threads: 45
Joined: Jan 2018
Oct292021, 06:54 AM
(This post was last modified: Oct292021, 07:43 AM by Gribouillis.)
A new version for python >= 3.8
from operator import add, sub
from itertools import accumulate, tee
def gribmax2(iterable):
s, t = tee(accumulate(iterable, add, initial=0))
return max(map(sub, s, accumulate(t, min))) It seems to agree with Deanhystad's version on 100000 arrays of length 100 with values in the range(1000, 1000).
It took me a while to reach this quintessence of a function. I hope it will become a must in interviews from now on!
Posts: 411
Threads: 47
Joined: Dec 2017
Oct292021, 03:48 PM
(This post was last modified: Oct292021, 03:50 PM by DPaul.)
@ Griboullis, @ deanhystad
From time to time, an interesting challenge pops up :)
For the aged students amongst us, could you provide one or
two solutions, from arrays len(100), that you know are correct.
That way we can test our homegrown algorithms.
As always, I get the feeling that this is not so complicated,
but I am often wrong.
Thanks
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Posts: 2,695
Threads: 12
Joined: Feb 2020
Oct292021, 04:12 PM
(This post was last modified: Oct292021, 04:12 PM by deanhystad.)
Generate random lists of numbers and use the posted code(s) to compute the answer. You don't need an answer sheet when you have code that generates the correct answer.
Personally I think a lot of insight is gained by solving this problem by hand. I blanked on this problem until I wrote a list of numbers and a list of sums. I looked at that for about 10 seconds and the solution was immediately obvious.
Posts: 411
Threads: 47
Joined: Dec 2017
(Oct292021, 04:12 PM)deanhystad Wrote: Generate random lists of numbers and use the posted code(s) to compute the answer. You don't need an answer sheet when you have code that generates the correct answer.
Personally I think a lot of insight is gained by solving this problem by hand. I blanked on this problem until I wrote a list of numbers and a list of sums. I looked at that for about 10 seconds and the solution was immediately obvious.
Well , I did that of course, but maybe I'm missing something.
What should x = [1,2,3,4] yield ?
As I understand it : 1
Or are single values not allowed , although it is a "subarray".
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Posts: 2,695
Threads: 12
Joined: Feb 2020
There is an error in my code in that it starts with the assumption that the max sum will be greater than 0. It would be better if it initialized maxsum to [array[0], start, start]. That allows it to work for the case were all numbers in the list are negative. The correct answer for [1, 2, 3, 4] is [1, 0, 0] for my code.
I'm not sure how to go about fixing Griboullis' code. The problem is that with all negative numbers, sv and min(mv, sv) are always the same, so best = max(best, 0). Starting with max = array[0] does not fix the problem.
Posts: 411
Threads: 47
Joined: Dec 2017
(Oct292021, 05:59 PM)deanhystad Wrote: There is an error in my code in that it starts with the assumption that the max sum will be greater than 0. It would be better if it initialized maxsum to [array[0], start, start]. That allows it to work for the case were all numbers in the list are negative. The correct answer for [1, 2, 3, 4] is [1, 0, 0] for my code.
I'm not sure how to go about fixing Griboullis' code. The problem is that with all negative numbers, sv and min(mv, sv) are always the same, so best = max(best, 0). Starting with max = array[0] does not fix the problem.
That's why I asked the question.
Still, I'm checking my solution for larger arrays,
will publish it when i'm sure.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Posts: 3,294
Threads: 45
Joined: Jan 2018
For me the correct answer for [1, 2, 3, 4] is 0, the sum of the empty list.
Posts: 411
Threads: 47
Joined: Dec 2017
Oct302021, 05:38 AM
(This post was last modified: Oct302021, 05:39 AM by DPaul.)
(Oct292021, 07:08 PM)Gribouillis Wrote: For me the correct answer for [1, 2, 3, 4] is 0, the sum of the empty list.
Could you elaborate please?
Do you see this as a mathematical concept or a practical consideration ?
Because the random generator (100,100) could very well produce an all negative array.
thx,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Posts: 411
Threads: 47
Joined: Dec 2017
I ran several tests with both proposed codes, up to len(arr) = 100,
and I get the same results.
So for what it's worth:
maxArr = [max(arr)]
for number in range(len(arr)):
for grouping in range(2,len(arr)number+1):
maxArr.append(sum(arr[number:(number+grouping)]))
print(max(maxArr)) Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
