Created
August 12, 2019 11:02
-
-
Save criskgl/e7e072de6d28f553080e9819b24fe15e to your computer and use it in GitHub Desktop.
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 MinSubArraySum { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
int[] a = {1, 3, -2, -1,1}; | |
int result = minSumSubarray(a); | |
System.out.println(result); | |
} | |
public static int minSumSubarray(int[] A){ | |
int n = A.length; | |
int local_min = 0; | |
int global_min = Integer.MAX_VALUE; | |
for(int i = 0; i < n; i++){ | |
int current = A[i]; | |
local_min = Math.min(current, current + local_min); | |
if(local_min < global_min){ | |
global_min = local_min; | |
} | |
} | |
return global_min; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment