-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61923b3
commit dd09549
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Author: Krzysztof Bochenek | ||
// Email: kpbochenek@gmail.com | ||
// -------------------------------- | ||
#include <stdio.h> | ||
#include <vector> | ||
#include <unordered_map> | ||
#include <map> | ||
#include <unordered_set> | ||
#include <set> | ||
#include <stack> | ||
#include <math.h> | ||
#include <algorithm> | ||
#include <string> | ||
#include <iostream> | ||
|
||
// --------------------- | ||
|
||
typedef long long int ll; | ||
typedef unsigned long long ull; | ||
|
||
using namespace std; | ||
|
||
// --------------------- | ||
|
||
template<class T> | ||
void print_vec(const vector<T> &v, string desc = "") { | ||
cout << desc << " "; | ||
for (T e: v) { | ||
cout << e << " "; | ||
} | ||
cout << endl; | ||
} | ||
|
||
// --------------------- | ||
|
||
class Solution { | ||
public: | ||
int maxSubArray(vector<int>& nums) { | ||
int bestMax = 0; | ||
int ptMax = nums[0]; | ||
int currentMax = 0; | ||
|
||
for (int n: nums) { | ||
currentMax = max(currentMax + n, 0); | ||
bestMax = max(bestMax, currentMax); | ||
ptMax = max(ptMax, n); | ||
} | ||
return bestMax == 0 ? ptMax : bestMax; | ||
} | ||
}; |