Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 26facf5

Browse files
committed
- Maximum sub array
- Merge sorted array Done and comments added
1 parent 50f2410 commit 26facf5

File tree

2 files changed

+164
-3
lines changed

2 files changed

+164
-3
lines changed
Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,104 @@
11
package com.leetcode.arrays;
22

33
/**
4+
* Level: Easy
5+
* Problem Link: https://leetcode.com/problems/maximum-subarray
6+
*
47
* @author rampatra
58
* @since 2019年04月26日
69
*/
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+
}
Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,73 @@
11
package com.leetcode.arrays;
22

3+
import java.util.Arrays;
4+
35
/**
6+
* Level: Easy
7+
* Problem Link: https://leetcode.com/problems/merge-sorted-array/
8+
*
49
* @author rampatra
510
* @since 2019年04月26日
611
*/
712
public class MergeSortedArray {
8-
}
13+
14+
/**
15+
* Time complexity: O(m*n)
16+
* Runtime: <a href="https://leetcode.com/submissions/detail/225075693/">1 ms</a>.
17+
*
18+
* @param nums1
19+
* @param m
20+
* @param nums2
21+
* @param n
22+
*/
23+
public static void mergeSimple(int[] nums1, int m, int[] nums2, int n) {
24+
int i = 0;
25+
26+
for (int j = 0; j < n; j++) {
27+
// find the index where the element from nums2 need to be inserted
28+
while (i < m + j && nums1[i] < nums2[j]) {
29+
i++;
30+
}
31+
// copy elements from i+1th position to one position right
32+
for (int k = m + j; k > i; k--) { // note: replacing this with System.arraycopy() gives a 0 ms runtime
33+
nums1[k] = nums1[k - 1];
34+
}
35+
nums1[i] = nums2[j];
36+
}
37+
}
38+
39+
/**
40+
* Time complexity: O(m+n)
41+
* Runtime: <a href="https://leetcode.com/submissions/detail/225078165/">0 ms</a>.
42+
*
43+
* @param nums1
44+
* @param m
45+
* @param nums2
46+
* @param n
47+
*/
48+
public static void merge(int[] nums1, int m, int[] nums2, int n) {
49+
for (int i = m + n - 1; i >= 0; i--) {
50+
if (m == 0) {
51+
nums1[i] = nums2[--n];
52+
} else if (n == 0) { // we ran out of nums2 elements so there is nothing left to merge
53+
return;
54+
} else if (nums1[m - 1] > nums2[n - 1]) {
55+
nums1[i] = nums1[--m];
56+
} else {
57+
nums1[i] = nums2[--n];
58+
}
59+
}
60+
}
61+
62+
public static void main(String[] args) {
63+
int[] nums1 = {1, 2, 3, 0, 0, 0};
64+
int[] nums2 = {4, 5, 6};
65+
merge(nums1, 3, nums2, 3);
66+
System.out.println(Arrays.toString(nums1));
67+
68+
nums1 = new int[]{4, 5, 6, 0, 0, 0};
69+
nums2 = new int[]{1, 2, 3};
70+
merge(nums1, 3, nums2, 3);
71+
System.out.println(Arrays.toString(nums1));
72+
}
73+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /