-
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathGangBean.java
More file actions
28 lines (27 loc) · 707 Bytes
/
GangBean.java
File metadata and controls
28 lines (27 loc) · 707 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
26
27
class Solution {
public int maxSubArray(int[] nums) {
/**
1. understanding
- integer array nums
- find largest subarray sum
2. starategy
- calculate cumulative sum
- mem[i+1] = num[i+1] + mem[i] if (num[i+1] + mem[i] >= 0) else num[i+1]
3. complexity
- time: O(N)
- space: O(1)
*/
int prev = 0;
int curr = 0;
int max = Integer.MIN_VALUE;
for (int i = 0 ; i < nums.length; i++) {
curr = nums[i];
if (prev >= 0) {
curr += prev;
}
max = Math.max(max, curr);
prev = curr;
}
return max;
}
}