Skip to content

Commit 95dea0f

Browse files
committed
add 1031 java
1 parent 00d3121 commit 95dea0f

File tree

6 files changed

+279
-7
lines changed

6 files changed

+279
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@
316316
1007 | [Minimum Domino Rotations For Equal Row](https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/description/) | [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/minimumDominoRotationsForEqualRow.java) | _O(n)_ | _O(1)_ | Medium |google, array, dp| AGAIN (1)
317317
1014 | [Best Sightseeing Pair](https://leetcode.com/problems/best-sightseeing-pair/) | [Python](./leetcode_python/Array/best_sightseeing_pair.py) | | | Medium |dp, array, good basic, Spotify| AGAIN (not start)
318318
1027 | [Longest Arithmetic Subsequence](https://leetcode.com/problems/longest-arithmetic-subsequence/) | [Python](./leetcode_python/Array/longest-arithmetic-subsequence.py) | | | Medium |dp, hash table, trick, google, amazon| AGAIN*** (not start)
319+
1031 | [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MaximumSumOfTwoNonOverlappingSubarrays.java)
320+
| [Python](./leetcode_python/Array/longest-arithmetic-subsequence.py) | | | Medium|prefix sum, sliding window, google| AGAIN*** (1)
319321
1041 | [Robot Bounded In Circle](https://leetcode.com/problems/robot-bounded-in-circle/) | [Python](./leetcode_python/Array/robot-bounded-in-circle.py) | | | Medium |array, math, `amazon`| AGAIN** (2)
320322
1109 | [ Corporate Flight Bookings](https://leetcode.com/problems/corporate-flight-bookings/) | [Python](./leetcode_python/Array/corporate-flight-bookings.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/CorporateFlightBookings.java) | | |Medium |LC 370, difference array, good basic, `amazon`, google|AGAIN********* (MUST) (3)
321323
1248 | [Count Number of Nice Subarrays](https://leetcode.com/problems/count-number-of-nice-subarrays/) | [Python](./leetcode_python/Array/count-number-of-nice-subarrays.py) | | | Medium |LC 828, hash map, Prefix sum, dict, windown, good basic, deque, array, `amazon`| AGAIN************** (3) (MUST)

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2025-01-04
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2025-01-01
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20250104: 1031
12
20250101: 729,731
23
20241231: 769,817,855(again)
34
20241229: 498(again),934

data/to_review.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2025-02-28 -> ['1031']
12
2025-02-25 -> ['729,731']
23
2025-02-24 -> ['769,817,855(again)']
34
2025-02-22 -> ['498(again),934']
@@ -6,33 +7,34 @@
67
2025-02-15 -> ['369,311']
78
2025-02-14 -> ['370']
89
2025-02-13 -> ['815,871,593,1109']
9-
2025-02-07 -> ['560,523']
10+
2025-02-07 -> ['1031', '560,523']
1011
2025-02-04 -> ['729,731']
1112
2025-02-03 -> ['769,817,855(again)']
1213
2025-02-01 -> ['498(again),934', '304,853,325']
1314
2025-01-31 -> ['379,173']
1415
2025-01-30 -> ['079,212(todo),362,849']
1516
2025-01-26 -> ['370(todo)']
16-
2025-01-25 -> ['369,311']
17+
2025-01-25 -> ['1031', '369,311']
1718
2025-01-24 -> ['370', '34,767']
1819
2025-01-23 -> ['815,871,593,1109']
1920
2025-01-22 -> ['729,731']
2021
2025-01-21 -> ['769,817,855(again)']
2122
2025-01-20 -> ['722,380']
2223
2025-01-19 -> ['498(again),934', '33,81']
2324
2025-01-18 -> ['379,173']
24-
2025-01-17 -> ['079,212(todo),362,849', '560,523', '253']
25+
2025-01-17 -> ['1031', '079,212(todo),362,849', '560,523', '253']
2526
2025-01-16 -> ['776,31']
2627
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2728
2025-01-14 -> ['729,731', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2829
2025-01-13 -> ['769,817,855(again)']
29-
2025-01-12 -> ['369,311']
30+
2025-01-12 -> ['1031', '369,311']
3031
2025-01-11 -> ['498(again),934', '370', '304,853,325', '394']
3132
2025-01-10 -> ['379,173', '815,871,593,1109', '833,950']
32-
2025-01-09 -> ['729,731', '079,212(todo),362,849']
33+
2025-01-09 -> ['1031', '729,731', '079,212(todo),362,849']
3334
2025-01-08 -> ['769,817,855(again)']
34-
2025-01-06 -> ['729,731', '498(again),934']
35-
2025-01-05 -> ['769,817,855(again)', '379,173', '370(todo)']
35+
2025-01-07 -> ['1031']
36+
2025-01-06 -> ['1031', '729,731', '498(again),934']
37+
2025-01-05 -> ['1031', '769,817,855(again)', '379,173', '370(todo)']
3638
2025-01-04 -> ['729,731', '079,212(todo),362,849', '369,311', '560,523', '53,210,207']
3739
2025-01-03 -> ['729,731', '769,817,855(again)', '498(again),934', '370', '34,767', '444']
3840
2025-01-02 -> ['729,731', '769,817,855(again)', '379,173', '815,871,593,1109', '1188,130,855(again)']
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package LeetCodeJava.Array;
2+
3+
// https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays/description/
4+
/**
5+
* 1031. Maximum Sum of Two Non-Overlapping Subarrays
6+
* Medium
7+
* Topics
8+
* Companies
9+
* Hint
10+
* 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.
11+
*
12+
* The array with length firstLen could occur before or after the array with length secondLen, but they have to be non-overlapping.
13+
*
14+
* A subarray is a contiguous part of an array.
15+
*
16+
*
17+
*
18+
* Example 1:
19+
*
20+
* Input: nums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2
21+
* Output: 20
22+
* Explanation: One choice of subarrays is [9] with length 1, and [6,5] with length 2.
23+
* Example 2:
24+
*
25+
* Input: nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2
26+
* Output: 29
27+
* Explanation: One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.
28+
* Example 3:
29+
*
30+
* Input: nums = [2,1,5,6,0,9,5,0,3,8], firstLen = 4, secondLen = 3
31+
* Output: 31
32+
* Explanation: One choice of subarrays is [5,6,0,9] with length 4, and [0,3,8] with length 3.
33+
*
34+
*
35+
* Constraints:
36+
*
37+
* 1 <= firstLen, secondLen <= 1000
38+
* 2 <= firstLen + secondLen <= 1000
39+
* firstLen + secondLen <= nums.length <= 1000
40+
* 0 <= nums[i] <= 1000
41+
*
42+
*/
43+
public class MaximumSumOfTwoNonOverlappingSubarrays {
44+
45+
// V0
46+
// public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
47+
//
48+
// }
49+
50+
// V1
51+
// IDEA: PREFIX SUM
52+
// https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays/solutions/1489581/java-easy-to-understand-prefix-sum-by-rm-5d2z/
53+
public int maxSumTwoNoOverlap_1(int[] A, int L, int M) {
54+
int sums[] = new int[A.length + 1];
55+
56+
for (int i = 1; i <= A.length; i++)
57+
sums[i] = A[i - 1] + sums[i - 1];
58+
59+
int maxLval = 0;
60+
int ans = 0;
61+
for (int i = L; i <= A.length - M; i++) {
62+
maxLval = Math.max(maxLval, sums[i] - sums[i - L]);
63+
ans = Math.max(ans, sums[i + M] - sums[i] + maxLval);
64+
}
65+
int maxRval = 0;
66+
for (int i = M; i <= A.length - L; i++) {
67+
maxRval = Math.max(maxRval, sums[i] - sums[i - M]);
68+
ans = Math.max(ans, sums[i + L] - sums[i] + maxRval);
69+
}
70+
return ans;
71+
}
72+
73+
// V2
74+
// IDEA: PREFIX SUM
75+
// https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays/solutions/5171249/java-clean-solution-using-prefix-sum-det-lxok/
76+
// Drive Function
77+
public int maxSumTwoNoOverlap_2(int[] nums, int firstLen, int secondLen) {
78+
int[] prefix = new int[nums.length + 1];
79+
for (int i = 0; i < nums.length; i++) {
80+
prefix[i + 1] = prefix[i] + nums[i];
81+
}
82+
83+
return Math.max(solveHelper(prefix, firstLen, secondLen),
84+
solveHelper(prefix, secondLen, firstLen));
85+
}
86+
87+
private int solveHelper(int[] prefix, int len1, int len2) {
88+
int res = 0;
89+
int maxLength = Integer.MIN_VALUE;
90+
for (int i = len1 + len2; i < prefix.length; i++) {
91+
maxLength = Math.max(maxLength,
92+
prefix[i - len2] - prefix[i - len2 - len1]);
93+
res = Math.max(res, maxLength + prefix[i] - prefix[i - len2]);
94+
}
95+
return res;
96+
}
97+
98+
// V3
99+
// https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays/solutions/913234/on-thought-process-java-by-133c7-wn3a/
100+
public int maxSumTwoNoOverlap_3(int[] A, int L, int M) {
101+
int n = A.length;
102+
if (n == 0) {
103+
return 0;
104+
}
105+
if (L + M > n) {
106+
throw new IllegalArgumentException();
107+
}
108+
109+
int[] Ls = new int[n];
110+
int[] Ms = new int[n];
111+
112+
int lSum = 0;
113+
int mSum = 0;
114+
115+
for (int i = 0; i <= n - L; i++) {
116+
if (i == 0) {
117+
for (int j = 0; j < L; j++) {
118+
lSum += A[j];
119+
}
120+
} else {
121+
lSum = lSum - A[i - 1] + A[i + L - 1];
122+
}
123+
124+
Ls[i] = lSum;
125+
}
126+
127+
for (int i = 0; i <= n - M; i++) {
128+
if (i == 0) {
129+
for (int j = 0; j < M; j++) {
130+
mSum += A[j];
131+
}
132+
} else {
133+
mSum = mSum - A[i - 1] + A[i + M - 1];
134+
}
135+
136+
Ms[i] = mSum;
137+
}
138+
139+
int[] lmax = new int[n + 1];
140+
int[] mmax = new int[n + 1];
141+
142+
for (int i = n - 1; i >= 0; i--) {
143+
lmax[i] = Math.max(lmax[i + 1], Ls[i]);
144+
mmax[i] = Math.max(mmax[i + 1], Ms[i]);
145+
}
146+
147+
int ans = 0;
148+
for (int i = 0; i <= n - L - 1; i++) {
149+
ans = Math.max(ans, Ls[i] + mmax[i + L]);
150+
}
151+
for (int i = 0; i <= n - M - 1; i++) {
152+
ans = Math.max(ans, Ms[i] + lmax[i + M]);
153+
}
154+
155+
return ans;
156+
}
157+
158+
// V5
159+
// https://leetcode.ca/2018-09-26-1031-Maximum-Sum-of-Two-Non-Overlapping-Subarrays/
160+
public int maxSumTwoNoOverlap_5(int[] nums, int firstLen, int secondLen) {
161+
int n = nums.length;
162+
int[] s = new int[n + 1];
163+
for (int i = 0; i < n; ++i) {
164+
s[i + 1] = s[i] + nums[i];
165+
}
166+
int ans = 0;
167+
for (int i = firstLen, t = 0; i + secondLen - 1 < n; ++i) {
168+
t = Math.max(t, s[i] - s[i - firstLen]);
169+
ans = Math.max(ans, t + s[i + secondLen] - s[i]);
170+
}
171+
for (int i = secondLen, t = 0; i + firstLen - 1 < n; ++i) {
172+
t = Math.max(t, s[i] - s[i - secondLen]);
173+
ans = Math.max(ans, t + s[i + firstLen] - s[i]);
174+
}
175+
return ans;
176+
}
177+
178+
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,5 +1568,91 @@ public boolean book(int startTime, int endTime) {
15681568
}
15691569
}
15701570

1571+
// LC 1031
1572+
// https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays/
1573+
// 4.21 - 4.40 pm
1574+
/**
1575+
* return the maximum sum of elements in two non-overlapping
1576+
* subarrays with lengths firstLen and secondLen.
1577+
*
1578+
* The array with length firstLen could occur before or after
1579+
* the array with length secondLen,
1580+
* but they have to be non-overlapping.
1581+
*
1582+
*
1583+
* A subarray is a contiguous part of an array.
1584+
*
1585+
*
1586+
* IDEA: PREFIX SUM
1587+
*
1588+
* exp 1)
1589+
*
1590+
* Input: nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2
1591+
*
1592+
* -> prefix sum = [3,11,12,15,17,18,26,37,0]
1593+
*
1594+
* Sum(i,j) = preSum(j+1) - preSum(i)
1595+
*
1596+
* -> pre(15) - pre(3) = 12
1597+
*
1598+
*/
1599+
public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
1600+
1601+
// edge
1602+
if(nums.length == 3){
1603+
return firstLen + secondLen;
1604+
}
1605+
1606+
// prefix Sum
1607+
List<Integer> prefixSum = new ArrayList<>();
1608+
prefixSum.add(0);
1609+
int curSum = 0;
1610+
for(int i = 0; i < nums.length; i++){
1611+
curSum += nums[i];
1612+
prefixSum.add(curSum);
1613+
}
1614+
1615+
int longerLen = 0;
1616+
int len = 0;
1617+
if(firstLen > secondLen){
1618+
longerLen = firstLen;
1619+
len = secondLen;
1620+
}else{
1621+
len = firstLen;
1622+
longerLen = secondLen;
1623+
}
1624+
1625+
int firstLenVal = 0;
1626+
int secondLenVal = 0;
1627+
1628+
// Set<Integer> seletectedIdx = new HashSet<>();
1629+
int seletectedIdx = -1;
1630+
1631+
// get "max" val of firstLen
1632+
for(int i = firstLen; i < nums.length - firstLen; i++){ // ???
1633+
int cur = prefixSum.get(i+1) - prefixSum.get(i - firstLen);
1634+
if (firstLenVal == 0){
1635+
seletectedIdx = i;
1636+
firstLenVal = cur;
1637+
}else if (cur > firstLenVal){
1638+
seletectedIdx = i;
1639+
firstLenVal = cur;
1640+
}
1641+
}
1642+
1643+
// get "max" val of secondLen
1644+
for(int i = secondLen; i < nums.length - secondLen; i++){ // ???
1645+
if (i != seletectedIdx){
1646+
int cur = prefixSum.get(i+1) - prefixSum.get(i - secondLen);
1647+
secondLenVal = Math.max(secondLenVal, cur);
1648+
}
1649+
}
1650+
1651+
System.out.println(">>> firstLenVal = " + firstLenVal);
1652+
System.out.println(">>> secondLenVal = " + secondLenVal);
1653+
1654+
return firstLenVal + secondLenVal;
1655+
}
1656+
15711657

15721658
}

0 commit comments

Comments
 (0)