AlgoPlus
v0.1.0
Loading...
Searching...
No Matches
src
algorithms
dynamic_programming
kadane.h
1
#ifndef KADANE_H
2
#define KADANE_H
3
4
#ifdef __cplusplus
5
#include <climits>
6
#include <cstdint>
7
#include <iostream>
8
#include <vector>
9
#endif
10
17
int64_t kadane(std::vector<int> arr) {
18
int64_t mmax = INT_MIN, temp = 0;
19
for
(int64_t i = 0; i < arr.size(); i++) {
20
temp += arr[i];
21
if
(temp > mmax) {
22
mmax = temp;
23
}
24
if
(temp < 0) {
25
temp = 0;
26
}
27
}
28
return
mmax;
29
}
30
31
#endif
Generated by
1.13.2