Skip to content

Instantly share code, notes, and snippets.

@tcstory
Created September 14, 2018 13:32
Show Gist options
  • Save tcstory/a72aa9d62b139b62ac3c42f75005ff3a to your computer and use it in GitHub Desktop.
Save tcstory/a72aa9d62b139b62ac3c42f75005ff3a to your computer and use it in GitHub Desktop.
最大连续子序列之和
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