代码:
int maxsub(int* v,int n) {
int s = 0;
int maxn = INT_MIN;
int minn = INT_MAX;
for (int i = 0; i < n; i++) {
s += v[i];
maxn = max(maxn, s);
minn = min(minn, s);
}
return maxn - minn;
}
想法:
想着去优化,可以时间复杂度和空间复杂度都最小
代码:
int maxsub(int* v,int n) {
int s = 0;
int maxn = INT_MIN;
int minn = INT_MAX;
for (int i = 0; i < n; i++) {
s += v[i];
maxn = max(maxn, s);
minn = min(minn, s);
}
return maxn - minn;
}
想法:
想着去优化,可以时间复杂度和空间复杂度都最小