-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1031.java
More file actions
169 lines (167 loc) · 5.58 KB
/
Copy path1031.java
File metadata and controls
169 lines (167 loc) · 5.58 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
// 1031. Maximum Sum of Two Non-Overlapping Subarrays
//
// Given an integer array nums and two integers firstLen and secondLen, return the maximum sum of elements in two non-overlapping subarrays with lengths firstLen and secondLen.
//
// The array with length firstLen could occur before or after the array with length secondLen, but they have to be non-overlapping.
//
// A subarray is a contiguous part of an array.
//
//
//
// Example 1:
//
// Input: nums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2
// Output: 20
// Explanation: One choice of subarrays is [9] with length 1, and [6,5] with length 2.
// Example 2:
//
// Input: nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2
// Output: 29
// Explanation: One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.
// Example 3:
//
// Input: nums = [2,1,5,6,0,9,5,0,3,8], firstLen = 4, secondLen = 3
// Output: 31
// Explanation: One choice of subarrays is [5,6,0,9] with length 4, and [0,3,8] with length 3.
//
//
// Constraints:
//
// 1 <= firstLen, secondLen <= 1000
// 2 <= firstLen + secondLen <= 1000
// firstLen + secondLen <= nums.length <= 1000
// 0 <= nums[i] <= 1000
//
// Runtime 3 ms Beats 35.47%
// Memory 41.3 MB Beats 87.92%
class Solution {
public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
int[] memory = new int[nums.length];
int sumB = 0;
for (int i = 0; i < secondLen; i++) {
sumB = sumB + nums[i];
}
memory[0] = sumB;
int indexB = 0;
while (indexB < nums.length - secondLen) {
sumB = sumB - nums[indexB] + nums[indexB + secondLen];
indexB++;
memory[indexB] = sumB;
}
int indexA = 0;
int sumA = 0;
int max = 0;
for (int i = 0; i < firstLen - 1; i++) {
sumA = sumA + nums[i];
}
while (indexA <= nums.length - firstLen) {
sumA = sumA + nums[indexA + firstLen - 1];
int maxB = 0;
int leftIndex = indexA - secondLen;
if (leftIndex >= 0) {
for (int i = 0; i <= leftIndex; i++) {
maxB = Math.max(maxB, memory[i]);
}
}
int rightIndex = indexA + firstLen;
if (rightIndex <= nums.length - secondLen) {
for (int i = rightIndex; i <= nums.length - secondLen; i++) {
maxB = Math.max(maxB, memory[i]);
}
}
max = Math.max(sumA + maxB, max);
sumA = sumA - nums[indexA];
indexA++;
}
return max;
}
}
// Runtime 2 ms Beats 52.8%
// Memory 41.3 MB Beats 83.77%
class Solution {
public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
int[] memory = new int[nums.length];
int sumB = 0;
for (int i = 0; i < secondLen; i++) {
sumB = sumB + nums[i];
}
memory[0] = sumB;
int indexB = 0;
while (indexB < nums.length - secondLen) {
sumB = sumB - nums[indexB] + nums[indexB + secondLen];
indexB++;
memory[indexB] = sumB;
}
int indexA = 0;
int sumA = 0;
int max = 0;
int leftIndex = indexA - secondLen;
int lastLeftMax = memory[0];
for (int i = 0; i < firstLen - 1; i++) {
sumA = sumA + nums[i];
}
while (indexA <= nums.length - firstLen) {
sumA = sumA + nums[indexA + firstLen - 1];
int maxB = 0;
if (leftIndex >= 0) {
lastLeftMax = Math.max(lastLeftMax, memory[leftIndex]);
maxB = Math.max(maxB, lastLeftMax);
}
int rightIndex = indexA + firstLen;
if (rightIndex <= nums.length - secondLen) {
for (int i = rightIndex; i <= nums.length - secondLen; i++) {
maxB = Math.max(maxB, memory[i]);
}
}
max = Math.max(sumA + maxB, max);
sumA = sumA - nums[indexA];
indexA++;
leftIndex++;
}
return max;
}
}
// Runtime 2 ms Beats 52.8%
// Memory 41.7 MB Beats 65.28%
class Solution {
public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
int max1 = findMaxSum(nums, firstLen, secondLen);
int max2 = findMaxSum(nums, secondLen, firstLen);
return Math.max(max1, max2);
}
private int findMaxSum(int[] nums, int leftLen, int rightLen) {
int[] leftMax = new int[nums.length];
int[] rightMax = new int[nums.length];
int sum = 0;
for (int i = 0; i < leftLen - 1; i++) {
sum = sum + nums[i];
}
for (int i = leftLen - 1; i < nums.length; i++) {
sum = sum + nums[i];
if (i > 0) {
leftMax[i] = Math.max(leftMax[i - 1], sum);
} else {
leftMax[i] = sum;
}
sum = sum - nums[i - leftLen + 1];
}
sum = 0;
for (int i = nums.length - 1; i > nums.length - rightLen; i--) {
sum = sum + nums[i];
}
for (int i = nums.length - rightLen; i >= 0; i--) {
sum = sum + nums[i];
if (i < nums.length - 1) {
rightMax[i] = Math.max(rightMax[i + 1], sum);
} else {
rightMax[i] = sum;
}
sum = sum - nums[i + rightLen - 1];
}
int max = 0;
for (int i = leftLen - 1; i < nums.length - rightLen; i++) {
max = Math.max(max, leftMax[i] + rightMax[i + 1]);
}
return max;
}
}