1
1
package com .leetcode .arrays ;
2
2
3
3
/**
4
+ * Level: Easy
5
+ * Problem Link: https://leetcode.com/problems/maximum-subarray
6
+ *
4
7
* @author rampatra
5
8
* @since 2019-04-26
6
9
*/
7
- public class MaximumSubarray {
8
- }
10
+ public class MaximumSubArray {
11
+
12
+ /**
13
+ * Time complexity: O(n)
14
+ * Runtime: <a href="https://leetcode.com/submissions/detail/225130466/">0 ms</a>.
15
+ *
16
+ * @param nums
17
+ * @return
18
+ */
19
+ public static int maxSubArray (int [] nums ) {
20
+ if (nums .length == 0 ) {
21
+ return 0 ;
22
+ }
23
+
24
+ int consecutiveSum = nums [0 ];
25
+ int maxSum = nums [0 ];
26
+
27
+ for (int i = 1 ; i < nums .length ; i ++) {
28
+ consecutiveSum += nums [i ];
29
+
30
+ /* if the current number is larger than the summation of all the
31
+ previous numbers then start from current number */
32
+ if (nums [i ] > consecutiveSum ) {
33
+ consecutiveSum = nums [i ];
34
+ }
35
+
36
+ if (consecutiveSum > maxSum ) {
37
+ maxSum = consecutiveSum ;
38
+ }
39
+ }
40
+
41
+ return maxSum ;
42
+ }
43
+
44
+
45
+ /**
46
+ * Divide and Conquer approach.
47
+ * Time complexity: O(n log n). See Master's Theorem to understand how.
48
+ * Runtime: <a href="https://leetcode.com/submissions/detail/225140123/">1 ms</a>.
49
+ *
50
+ * @param nums
51
+ * @return
52
+ */
53
+ public static int maxSubArrayDivideAndConquer (int [] nums ) {
54
+ return maxSubArrayDivideAndConquer (nums , 0 , nums .length - 1 );
55
+ }
56
+
57
+ public static int maxSubArrayDivideAndConquer (int [] nums , int start , int end ) {
58
+ if (start == end ) {
59
+ return nums [start ];
60
+ }
61
+
62
+ int mid = start + (end - start ) / 2 ;
63
+ int leftSASum = maxSubArrayDivideAndConquer (nums , start , mid );
64
+ int rightSASum = maxSubArrayDivideAndConquer (nums , mid + 1 , end );
65
+
66
+ int leftSum = Integer .MIN_VALUE ;
67
+ int rightSum = Integer .MIN_VALUE ;
68
+
69
+ // compute consecutive sum from mid towards start
70
+ int sum = 0 ;
71
+ for (int i = mid ; i >= start ; i --) {
72
+ sum += nums [i ];
73
+ if (sum > leftSum ) {
74
+ leftSum = sum ;
75
+ }
76
+ }
77
+
78
+ // compute consecutive sum from mid towards end
79
+ sum = 0 ;
80
+ for (int i = mid + 1 ; i <= end ; i ++) {
81
+ sum += nums [i ];
82
+ if (sum > rightSum ) {
83
+ rightSum = sum ;
84
+ }
85
+ }
86
+
87
+ // return the max of left sub-array, right sub-array, and the consecutive sum between start and end via mid
88
+ return Math .max (Math .max (leftSASum , rightSASum ), leftSum + rightSum );
89
+ }
90
+
91
+ public static void main (String [] args ) {
92
+ System .out .println (maxSubArray (new int []{3 }));
93
+ System .out .println (maxSubArray (new int []{-3 }));
94
+ System .out .println (maxSubArray (new int []{-2 , 1 , -3 , 4 , -1 , 2 , 1 , -5 , 4 }));
95
+ System .out .println (maxSubArray (new int []{4 , -5 , 1 , 2 , -1 , 4 , -3 , 1 , -2 }));
96
+
97
+ System .out .println ("----" );
98
+
99
+ System .out .println (maxSubArrayDivideAndConquer (new int []{3 }));
100
+ System .out .println (maxSubArrayDivideAndConquer (new int []{-3 }));
101
+ System .out .println (maxSubArrayDivideAndConquer (new int []{-2 , 1 , -3 , 4 , -1 , 2 , 1 , -5 , 4 }));
102
+ System .out .println (maxSubArrayDivideAndConquer (new int []{4 , -5 , 1 , 2 , -1 , 4 , -3 , 1 , -2 }));
103
+ }
104
+ }
0 commit comments