Problems Flashcards

1
Q

You are given an array of integers and your aim is to find maximum sum contiguous sub-array consisting of at least one element.

Ex - [-2,1,-3,4,-1,2,1,-5,4] should return 6

A

anything negative, is just going to make it more negative
so we wont want to include them in any sum
if we step through we want to keep track of the maximum of all the values
but we also want to keep track of a cumulative sum.
if that sum drops below zero, then we have already see it’s maximum
and we keep track of it in the maximum

sumation = 0
maximum = 0
for index, value in enumerate.items():
    sumation += value
    # initialize the maximum
    if index == 0:
        maximum = value
maximum = max(maximum, sumation, value)
    # we've captured the maximum already, so we reset the sumation
    if sumation < 0:
        sumation = 0
return maximum
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

describe the Longest alternating subsequence algorithm

A

It’s an algorithm that returns a subsequence that alternates greater than and less than.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly