Skip to content

Commit 7fb7c92

Browse files
committed
180423
1 parent 246d2bb commit 7fb7c92

File tree

4 files changed

+272
-1
lines changed

4 files changed

+272
-1
lines changed

src/Main.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import leetcode.editor.en.Q152.MaximumProductSubarray;
12
import leetcode.editor.en.Q1548.TheMostSimilarPathInAGraph;
3+
import leetcode.editor.en.Q1567.MaximumLengthOfSubarrayWithPositiveProduct;
24
import leetcode.editor.en.Q1639.NumberOfWaysToFormATargetStringGivenADictionary;
35
import leetcode.editor.en.Q2218.MaximumValueOfKCoinsFromPiles;
46
import leetcode.editor.en.Q2402.MeetingRoomsIii;
@@ -21,7 +23,7 @@
2123

2224
public class Main {
2325
public static void main(String[] args) throws IOException {
24-
System.out.println(new CousinsInBinaryTreeIi().replaceValueInTree());
26+
System.out.println(new MaximumLengthOfSubarrayWithPositiveProduct().getMaxLen(toIntArray("[1,2,3,5,-6,4,0,10]\n")));
2527

2628
}
2729

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package leetcode.editor.en.Q152;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//Given an integer array nums, find a subarray that has the largest product,
8+
//and return the product.
9+
//
10+
// The test cases are generated so that the answer will fit in a 32-bit integer.
11+
//
12+
//
13+
//
14+
// Example 1:
15+
//
16+
//
17+
//Input: nums = [2,3,-2,4]
18+
//Output: 6
19+
//Explanation: [2,3] has the largest product 6.
20+
//
21+
//
22+
// Example 2:
23+
//
24+
//
25+
//Input: nums = [-2,0,-1]
26+
//Output: 0
27+
//Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
28+
//
29+
//
30+
//
31+
// Constraints:
32+
//
33+
//
34+
// 1 <= nums.length <= 2 * 10⁴
35+
// -10 <= nums[i] <= 10
36+
// The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit
37+
//integer.
38+
//
39+
//
40+
// 👍 15669 👎 473
41+
42+
43+
//leetcode submit region begin(Prohibit modification and deletion)
44+
class Solution {
45+
46+
int max = 0;
47+
48+
public int maxProduct(int[] nums) {
49+
max = Arrays.stream(nums).min().getAsInt();
50+
maxProduct(0, nums);
51+
return max;
52+
}
53+
54+
public int[] maxProduct(int i, int[] nums) {
55+
if (i == nums.length) {
56+
return new int[]{1, 1};
57+
}
58+
int num = nums[i];
59+
int[] next = maxProduct(i + 1, nums);
60+
int newMax = Math.max(num, num * next[0]);
61+
newMax = Math.max(newMax, num * next[1]);
62+
int newMin = Math.min(num, num * next[0]);
63+
newMin = Math.min(newMin, num * next[1]);
64+
max = Math.max(max, newMax);
65+
66+
return new int[]{newMax, newMin};
67+
}
68+
69+
}
70+
//leetcode submit region end(Prohibit modification and deletion)
71+
72+
73+
public class MaximumProductSubarray extends Solution {
74+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package leetcode.editor.en.Q1567;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//Given an array of integers nums, find the maximum length of a subarray where
8+
//the product of all its elements is positive.
9+
//
10+
// A subarray of an array is a consecutive sequence of zero or more values
11+
//taken out of that array.
12+
//
13+
// Return the maximum length of a subarray with positive product.
14+
//
15+
//
16+
// Example 1:
17+
//
18+
//
19+
//Input: nums = [1,-2,-3,4]
20+
//Output: 4
21+
//Explanation: The array nums already has a positive product of 24.
22+
//
23+
//
24+
// Example 2:
25+
//
26+
//
27+
//Input: nums = [0,1,-2,-3,-4]
28+
//Output: 3
29+
//Explanation: The longest subarray with positive product is [1,-2,-3] which
30+
//has a product of 6.
31+
//Notice that we cannot include 0 in the subarray since that'll make the
32+
//product 0 which is not positive.
33+
//
34+
// Example 3:
35+
//
36+
//
37+
//Input: nums = [-1,-2,-3,0,1]
38+
//Output: 2
39+
//Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3].
40+
//
41+
//
42+
//
43+
//
44+
// Constraints:
45+
//
46+
//
47+
// 1 <= nums.length <= 10⁵
48+
// -10⁹ <= nums[i] <= 10⁹
49+
//
50+
//
51+
// 👍 2146 👎 57
52+
53+
54+
//leetcode submit region begin(Prohibit modification and deletion)
55+
class Solution {
56+
57+
public int getMaxLen(int[] nums) {
58+
59+
int[] nextNeg = new int[nums.length];
60+
Arrays.fill(nextNeg, -1);
61+
LinkedList<Integer> stack = new LinkedList<>();
62+
for (int i = 0; i < nums.length; i++) {
63+
if (nums[i] != 0) {
64+
nums[i] = (nums[i] > 0 ? 1 : -1);
65+
} else {
66+
stack.clear();
67+
continue;
68+
}
69+
while (!stack.isEmpty() && nums[i] < 0) {
70+
nextNeg[stack.pollLast()] = i;
71+
}
72+
stack.add(i);
73+
}
74+
75+
76+
return getMaxLen(0, nums, nextNeg);
77+
78+
79+
}
80+
81+
82+
public int getMaxLen(int left, int[] nums, int[] nextNeg) {
83+
if (left >= nums.length) {
84+
return 0;
85+
}
86+
int right = left;
87+
int prod = 1;
88+
int max = 0;
89+
while (right < nums.length) {
90+
if (nums[right] == 0) {
91+
return Math.max(getMaxLen(right + 1, nums, nextNeg), max);
92+
}
93+
prod = prod * (nums[right] > 0 ? 1 : -1);
94+
if (prod > 0) {
95+
max = Math.max(max, right - left + 1);
96+
} else {
97+
if (nextNeg[right] == -1) {
98+
//There are no new negs ahead
99+
while (left <= right && prod < 0) {
100+
prod /= nums[left];
101+
left++;
102+
}
103+
}
104+
}
105+
right++;
106+
}
107+
return max;
108+
}
109+
}
110+
//leetcode submit region end(Prohibit modification and deletion)
111+
112+
113+
public class MaximumLengthOfSubarrayWithPositiveProduct extends Solution {
114+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package leetcode.editor.en.Q1768;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//You are given two strings word1 and word2. Merge the strings by adding
8+
//letters in alternating order, starting with word1. If a string is longer than the
9+
//other, append the additional letters onto the end of the merged string.
10+
//
11+
// Return the merged string.
12+
//
13+
//
14+
// Example 1:
15+
//
16+
//
17+
//Input: word1 = "abc", word2 = "pqr"
18+
//Output: "apbqcr"
19+
//Explanation: The merged string will be merged as so:
20+
//word1: a b c
21+
//word2: p q r
22+
//merged: a p b q c r
23+
//
24+
//
25+
// Example 2:
26+
//
27+
//
28+
//Input: word1 = "ab", word2 = "pqrs"
29+
//Output: "apbqrs"
30+
//Explanation: Notice that as word2 is longer, "rs" is appended to the end.
31+
//word1: a b
32+
//word2: p q r s
33+
//merged: a p b q r s
34+
//
35+
//
36+
// Example 3:
37+
//
38+
//
39+
//Input: word1 = "abcd", word2 = "pq"
40+
//Output: "apbqcd"
41+
//Explanation: Notice that as word1 is longer, "cd" is appended to the end.
42+
//word1: a b c d
43+
//word2: p q
44+
//merged: a p b q c d
45+
//
46+
//
47+
//
48+
// Constraints:
49+
//
50+
//
51+
// 1 <= word1.length, word2.length <= 100
52+
// word1 and word2 consist of lowercase English letters.
53+
//
54+
//
55+
// 👍 1412 👎 22
56+
57+
58+
//leetcode submit region begin(Prohibit modification and deletion)
59+
class Solution {
60+
public String mergeAlternately(String word1, String word2) {
61+
int i = 0;
62+
int j = 0;
63+
StringBuilder ans = new StringBuilder();
64+
while (i < word1.length() && j < word2.length()) {
65+
ans.append(word1.charAt(i++));
66+
ans.append(word2.charAt(j++));
67+
}
68+
while (i < word1.length()) {
69+
ans.append(word1.charAt(i++));
70+
}
71+
while (j < word2.length()) {
72+
ans.append(word2.charAt(j++));
73+
}
74+
return ans.toString();
75+
}
76+
}
77+
//leetcode submit region end(Prohibit modification and deletion)
78+
79+
80+
public class MergeStringsAlternately extends Solution {
81+
}

0 commit comments

Comments
 (0)