-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
Added implementatin for kadane's algorithm #1031
base: master
Are you sure you want to change the base?
Changes from 3 commits
4e2d751
5bd5f80
d641025
b51df2b
65ecad8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,18 @@ | ||||||||||
/** | ||||||||||
* Simplest Kadane's algorithm implementation. | ||||||||||
* | ||||||||||
* @param {*[]} array | ||||||||||
* @return {number[]} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
*/ | ||||||||||
|
||||||||||
export default function kadane() { | ||||||||||
let current_sum = array[0]; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing out , i forgot to add parameter to function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
let maximum_subarray_sum = array[0]; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
or
Suggested change
|
||||||||||
|
||||||||||
for (let i = 1; i < array.length; i++) { | ||||||||||
current_sum = Math.max(current_sum + array[i], array[i]); | ||||||||||
maximum_subarray_sum = Math.max(maximum_subarray_sum, current_sum); | ||||||||||
} | ||||||||||
|
||||||||||
return maximum_subarray_sum; | ||||||||||
} |
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.