mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
@ -1,6 +1,6 @@
|
||||
/* Kadane's algorithm is one of the most efficient ways to
|
||||
* calculate the maximum contiguous subarray sum for a given array.
|
||||
* Below is the implementation of kadanes's algorithm along with
|
||||
* Below is the implementation of Kadane's algorithm along with
|
||||
* some sample test cases.
|
||||
* There might be a special case in this problem if al the elements
|
||||
* of the given array are negative. In such a case, the maximum negative
|
||||
@ -10,14 +10,14 @@
|
||||
*/
|
||||
|
||||
export function kadaneAlgo (array) {
|
||||
let cummulativeSum = 0
|
||||
let cumulativeSum = 0
|
||||
let maxSum = Number.NEGATIVE_INFINITY // maxSum has the least possible value
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
cummulativeSum = cummulativeSum + array[i]
|
||||
if (maxSum < cummulativeSum) {
|
||||
maxSum = cummulativeSum
|
||||
} else if (cummulativeSum < 0) {
|
||||
cummulativeSum = 0
|
||||
cumulativeSum = cumulativeSum + array[i]
|
||||
if (maxSum < cumulativeSum) {
|
||||
maxSum = cumulativeSum
|
||||
} else if (cumulativeSum < 0) {
|
||||
cumulativeSum = 0
|
||||
}
|
||||
}
|
||||
return maxSum
|
||||
|
@ -63,7 +63,7 @@ const uniquePaths2 = (obstacles) => {
|
||||
grid[0][j] = 1
|
||||
}
|
||||
// Fill the rest of grid by dynamic programming
|
||||
// using following reccurent formula:
|
||||
// using following recurrent formula:
|
||||
// K[i][j] = K[i - 1][j] + K[i][j - 1]
|
||||
for (let i = 1; i < rows; i++) {
|
||||
for (let j = 1; j < columns; j++) {
|
||||
|
Reference in New Issue
Block a user