-
Notifications
You must be signed in to change notification settings - Fork 138
Added #9 largest-non-adjacent-sum #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
||
DP[0] = array[0] | ||
DP[1] = max(array[0] , array[1]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens when len(array)==1 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and also it fails when len(array)==0.
def maximumNonAdjacentSum(array): | ||
|
||
DP = [0]*len(array) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
space is O(n). It is not a constant space. If you look closely on the algorithm, you are using only three at any instance, DP_i, DP_i_1, DP_i_2. You can rewrite the alogithm to do that
for i in range(2,len(array)): | ||
|
||
DP[i] = max3( array[i] , array[i] + DP[i-2] , DP[i-1]) | ||
print(DP) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as pointed out above rewrite this line to use constant space
Will make the necessary changes and raise a new PR again. Thanks for the feedback. |
largest-non-adjacent-sum was solved using Dynamic Programming approach.