-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxSubArray.java
More file actions
197 lines (150 loc) · 5.4 KB
/
Copy pathMaxSubArray.java
File metadata and controls
197 lines (150 loc) · 5.4 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package leetcode.arrays;
import java.util.stream.IntStream;
/* Given an integer array nums, find the
subarray
with the largest sum, and return its sum.
Example 1:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum 6.
Example 2:
Input: nums = [1]
Output: 1
Explanation: The subarray [1] has the largest sum 1.
Example 3:
Input: nums = [5,4,-1,7,8]
Output: 23
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
Constraints:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach,
which is more subtle.
*/
// Kadane's Algorithm
// Time Complexity: O(n)
// Space Complexity: O(1)
public class MaxSubArray {
public int maxSubArray(int[] nums) {
int maxSum = Integer.MIN_VALUE;
int currentSum = 0;
for (int num : nums) {
currentSum += num;
maxSum = Math.max(maxSum, currentSum);
if (currentSum < 0) {
currentSum = 0;
}
}
return maxSum;
/*
* int max=Integer.MIN_VALUE;
* int sum = 0;
* for(int i=0; i<nums.length; i++) {
* sum=Math.max(nums[i], nums[i]+sum);
* max=Math.max(sum,max);
* }
* return max;
*/
}
/*
* Explanation of Java 8 Features Used
* 1. IntStream.of(nums).forEach()
* • Converts the array into a stream for iteration.
* • Uses forEach() to process each element.
* 2. Using an array {maxSum, currentSum} instead of mutable variables
* • Since Java streams don’t allow modification of local variables inside
* lambdas, we store maxSum and currentSum in an array.
* 3. Functional Kadane’s Logic Inside forEach()
* • currentSum = max(num, currentSum + num);
* • maxSum = max(maxSum, currentSum);
*
* Why Not Use reduce()?
*
* reduce() is mainly for aggregating results (like sum or product). Since we
* need to track two values (maxSum and currentSum),
* reduce() becomes impractical.
*/
// Java 8
public int maxSubArrayLeetcode(int[] nums) {
int[] result = { Integer.MIN_VALUE, 0 }; // {maxSum, currentSum}
IntStream.of(nums).forEach(num -> {
result[1] = Math.max(num, result[1] + num); // currentSum = max(num, currentSum + num)
result[0] = Math.max(result[0], result[1]); // maxSum = max(maxSum, currentSum)
});
return result[0];
}
// Divide and Conquer Approach
// Divide & Conquer Solution (O(n log n))
public int maxSubArrayDQ(int[] nums) {
return maxSubArrayHelper(nums, 0, nums.length - 1);
}
private int maxSubArrayHelper(int[] nums, int left, int right) {
if (left == right)
return nums[left]; // Base case
int mid = left + (right - left) / 2;
// Find max subarray sum in left and right halves
int leftSum = maxSubArrayHelper(nums, left, mid);
int rightSum = maxSubArrayHelper(nums, mid + 1, right);
// Find max crossing sum
int crossSum = maxCrossingSum(nums, left, mid, right);
return Math.max(leftSum, Math.max(rightSum, crossSum));
}
private int maxCrossingSum(int[] nums, int left, int mid, int right) {
int leftSum = Integer.MIN_VALUE, rightSum = Integer.MIN_VALUE;
int sum = 0;
// Find max sum from mid to left
for (int i = mid; i >= left; i--) {
sum += nums[i];
leftSum = Math.max(leftSum, sum);
}
sum = 0;
// Find max sum from mid+1 to right
for (int i = mid + 1; i <= right; i++) {
sum += nums[i];
rightSum = Math.max(rightSum, sum);
}
return leftSum + rightSum; // Best sum across mid
}
public static void main(String[] args) {
MaxSubArray solution = new MaxSubArray();
int[] nums1 = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
System.out.println(solution.maxSubArray(nums1)); // Output: 6
int[] nums2 = { 1 };
System.out.println(solution.maxSubArray(nums2)); // Output: 1
int[] nums3 = { 5, 4, -1, 7, 8 };
System.out.println(solution.maxSubArray(nums3)); // Output: 23
int[] nums4 = { -1, -2, -3, -4 };
System.out.println(solution.maxSubArray(nums4)); // Output: -1
// Divide & Conquer Approach
MaxSubArray solution1 = new MaxSubArray();
int[] nums = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
System.out.println(solution1.maxSubArrayDQ(nums)); // Output: 6
}
// public static void main(String[] args) {
// }
}
// class Solution {
// public int maxSubArray(int[] nums) {
// int res = nums[0];
// int total = 0;
// for (int n : nums) {
// if (total < 0) {
// total = 0;
// }
// total += n;
// res = Math.max(res, total);
// }
// return res;
// }
// public static void main(String[] args) {
// MaxSubArray solution = new MaxSubArray();
// int[] nums1 = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
// System.out.println(solution.maxSubArray(nums1)); // Output: 6
// int[] nums2 = { 1 };
// System.out.println(solution.maxSubArray(nums2)); // Output: 1
// int[] nums3 = { 5, 4, -1, 7, 8 };
// System.out.println(solution.maxSubArray(nums3)); // Output: 23
// int[] nums4 = { -1, -2, -3, -4 };
// System.out.println(solution.maxSubArray(nums4)); // Output: -1
// }
// }