Last active
September 20, 2016 00:20
-
-
Save gingeleski/171df4a19fecc5222ba82678bf6702b9 to your computer and use it in GitHub Desktop.
Variant of Kadane's algorithm implemented in Java, using no Math calls
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
public class Main | |
{ | |
public int maxSubArray(final List<Integer> a) | |
{ | |
int ans = a.get(0); | |
int sum = a.get(0); | |
for (int i = 1; i < a.size(); i++) | |
{ | |
if (sum + a.get(i) > a.get(i)) sum += a.get(i); | |
else sum = a.get(i); | |
ans = Math.max(ans, sum); | |
} | |
return ans; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment