-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxSubArray.java
More file actions
25 lines (23 loc) · 856 Bytes
/
Copy pathMaxSubArray.java
File metadata and controls
25 lines (23 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution
{
public int maxSubArray(int[] nums)
{
int maxSum = Integer.MIN_VALUE; //instantiate minimum interger val
int currSum = 0; //set the current subArray sum to 0
//loop through array and compare contiguous values to the current sum
for (int i = 0; i < nums.length; i++)
{
//
if (currSum + nums[i] > nums[i])
{
currSum += nums[i]; //if current sum and next contiguous value is higher than that val, set it to current sum
}
else
{ //else set current sum to the next contiguous val, as it's greater
currSum = nums[i];
}
maxSum = Math.max(maxSum, currSum); //assigne currSum to maxSum
}
return maxSum; // return maxSum
}
}