[Codewars] Maximum subarray sum

Maximum subarray sum

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
int maxSequence(const std::vector<int>& arr){
int res = 0, mi = 0, now = 0;
for(int i = 0; i < arr.size(); i++) {
now += arr[i];
res = std::max(res, now - mi);
mi = std::min(mi, now);
}
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/06/07/PS/Codewars/maximum-subarray-sum/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.