Created
September 14, 2018 13:32
-
-
Save tcstory/a72aa9d62b139b62ac3c42f75005ff3a to your computer and use it in GitHub Desktop.
最大连续子序列之和
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
var data = [-2, 11, -4, 13, -5, -2]; | |
function max(data) { | |
let results = []; | |
for (let i = 0; i < data.length; i++) { | |
if (i === 0) { | |
results[i] = { | |
s: 0, | |
e: 1, | |
value: data[0] | |
}; | |
} else { | |
var max = results[i - 1]; | |
let sum1 = max.value + data[i]; | |
let sum2 = data[i]; | |
if (sum1 > sum2) { | |
results[i] = { | |
s: max.s, | |
e: i + 1, | |
value: sum1 | |
}; | |
} else { | |
results[i] = { | |
s: i, | |
e: i + 1, | |
value: data[i] | |
} | |
} | |
} | |
} | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment