Created
February 25, 2017 13:50
-
-
Save razchiriac/af93e1b317471ccbbd7310dee1717193 to your computer and use it in GitHub Desktop.
Kadane // source https://jsbin.com/cabuqok
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Kadane</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
kadaneLS = (A) => { | |
var maxCurrent = A[0]; | |
var maxGlobal = A[0]; | |
for (var i = 1; i < A.length; i++) { | |
maxCurrent = Math.max(A[i], maxCurrent + A[i]); | |
if (maxCurrent > maxGlobal) maxGlobal = maxCurrent; | |
} | |
return maxGlobal; | |
}; | |
A = [-2,3,2,-1]; | |
var maxSum = kadaneLS(A); | |
console.log(maxSum); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">kadaneLS = (A) => { | |
var maxCurrent = A[0]; | |
var maxGlobal = A[0]; | |
for (var i = 1; i < A.length; i++) { | |
maxCurrent = Math.max(A[i], maxCurrent + A[i]); | |
if (maxCurrent > maxGlobal) maxGlobal = maxCurrent; | |
} | |
return maxGlobal; | |
}; | |
A = [-2,3,2,-1]; | |
var maxSum = kadaneLS(A); | |
console.log(maxSum);</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kadaneLS = (A) => { | |
var maxCurrent = A[0]; | |
var maxGlobal = A[0]; | |
for (var i = 1; i < A.length; i++) { | |
maxCurrent = Math.max(A[i], maxCurrent + A[i]); | |
if (maxCurrent > maxGlobal) maxGlobal = maxCurrent; | |
} | |
return maxGlobal; | |
}; | |
A = [-2,3,2,-1]; | |
var maxSum = kadaneLS(A); | |
console.log(maxSum); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment