maxsubarray: initial commit
This commit is contained in:
parent
25ad456187
commit
1b33db721a
2 changed files with 40 additions and 0 deletions
1
algorithms/dynamic_programming/maxsubarray/README.md
Normal file
1
algorithms/dynamic_programming/maxsubarray/README.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
<https://www.hackerrank.com/challenges/maxsubarray>
|
||||||
39
algorithms/dynamic_programming/maxsubarray/solution.cpp
Normal file
39
algorithms/dynamic_programming/maxsubarray/solution.cpp
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
size_t T;
|
||||||
|
cin >> T;
|
||||||
|
|
||||||
|
while (T--) {
|
||||||
|
|
||||||
|
size_t N;
|
||||||
|
cin >> N;
|
||||||
|
|
||||||
|
int v;
|
||||||
|
cin >> v;
|
||||||
|
N--;
|
||||||
|
|
||||||
|
long long max_sum_cont = v;
|
||||||
|
long long max_sum_cont_tmp = v;
|
||||||
|
long long max_sum_noncont = v;
|
||||||
|
|
||||||
|
while (N--) {
|
||||||
|
|
||||||
|
cin >> v;
|
||||||
|
|
||||||
|
max_sum_cont_tmp = max((long long)v, max_sum_cont_tmp + v);
|
||||||
|
max_sum_cont = max(max_sum_cont, max_sum_cont_tmp);
|
||||||
|
|
||||||
|
max_sum_noncont = max((long long)v, max_sum_noncont + (v > 0 ? v : 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << max_sum_cont << ' ' << max_sum_noncont << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue