- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxSubArr.java
More file actions
Latest commit
20 lines (17 loc) · 542 Bytes
/
Copy pathMaxSubArr.java
File metadata and controls
20 lines (17 loc) · 542 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Link: https://leetcode.com/problems/maximum-subarray/
publicclassMaxSubArr {
publicintmaxSubArray(int[] nums) {
intsum = nums[0];
intmax = nums[0];
for (inti = 1; i < nums.length; i++) {
sum = sum > 0 ? sum + nums[i] : nums[i];
max = Math.max(max, sum);
}
returnmax;
}
publicstaticvoidmain(String[] args){
int[] num = {-2,1,-3,4,-1,2,1,-5,4};
MaxSubArrobj = newMaxSubArr();
System.out.println(obj.maxSubArray(num));
}
}