Skip to content

Commit 42e7129

Browse files
committed
weekly-240
Signed-off-by: ashKIK <[email protected]>
1 parent 51c8c4a commit 42e7129

4 files changed

+152
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
// https://leetcode.com/contest/weekly-contest-240/problems/largest-color-value-in-a-directed-graph/
3+
4+
import java.util.ArrayList;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
import java.util.Queue;
8+
9+
public class LargestColorValueInADirectedGraph {
10+
11+
// Topological sort + DP
12+
public int largestPathValue(String colors, int[][] edges) {
13+
int n = colors.length();
14+
List<List<Integer>> childToParent = new ArrayList<>();
15+
List<List<Integer>> parentToChildren = new ArrayList<>();
16+
int[] outDegree = new int[n];
17+
for (int i = 0; i < n; i++) {
18+
childToParent.add(new ArrayList<>());
19+
parentToChildren.add(new ArrayList<>());
20+
}
21+
for (int[] edge : edges) {
22+
int from = edge[0];
23+
int to = edge[1];
24+
childToParent.get(to).add(from);
25+
parentToChildren.get(from).add(to);
26+
outDegree[from]++;
27+
}
28+
Queue<Integer> queue = new LinkedList<>();
29+
int[][] dp = new int[n][26];
30+
for (int i = 0; i < n; i++) {
31+
if (outDegree[i] == 0) {
32+
queue.add(i);
33+
}
34+
}
35+
int res = 0;
36+
int count = n;
37+
while (!queue.isEmpty()) {
38+
int node = queue.poll();
39+
count--;
40+
int nodeColor = colors.charAt(node) - 'a';
41+
for (int i = 0; i < 26; i++) {
42+
for (int prev : parentToChildren.get(node)) {
43+
dp[node][i] = Math.max(dp[node][i], dp[prev][i]);
44+
}
45+
}
46+
dp[node][nodeColor]++;
47+
res = Math.max(res, dp[node][nodeColor]);
48+
for (int parent : childToParent.get(node)) {
49+
outDegree[parent]--;
50+
if (outDegree[parent] == 0) {
51+
queue.add(parent);
52+
}
53+
}
54+
}
55+
return count > 0 ? -1 : res;
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
// https://leetcode.com/contest/weekly-contest-240/problems/maximum-distance-between-a-pair-of-values/
3+
4+
public class MaximumDistanceBetweenAPairOfValues {
5+
6+
public int maxDistance(int[] nums1, int[] nums2) {
7+
int i = 0;
8+
int j = 0;
9+
int max = 0;
10+
while (i < nums1.length) {
11+
while (j < nums2.length && nums1[i] <= nums2[j]) {
12+
j++;
13+
}
14+
max = Math.max(max, j - i - 1);
15+
i++;
16+
}
17+
return max;
18+
}
19+
}

weekly-240/MaximumPopulationYear.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
// https://leetcode.com/contest/weekly-contest-240/problems/maximum-population-year/
3+
4+
public class MaximumPopulationYear {
5+
6+
public int maximumPopulation(int[][] logs) {
7+
int maxPopSoFar = 0;
8+
int year = 0;
9+
for (int i = 1950; i <= 2050; i++) {
10+
int pop = 0;
11+
for (int[] log : logs) {
12+
if (i >= log[0] && i < log[1]) {
13+
pop++;
14+
}
15+
}
16+
if (pop > maxPopSoFar) {
17+
maxPopSoFar = pop;
18+
year = i;
19+
}
20+
}
21+
return year;
22+
}
23+
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
// https://leetcode.com/contest/weekly-contest-240/problems/maximum-subarray-min-product/
3+
4+
import java.util.Stack;
5+
6+
public class MaximumSubarrayMinProduct {
7+
8+
public int maxSumMinProduct(int[] nums) {
9+
int n = nums.length;
10+
int[] leftBound = new int[n + 1];
11+
int[] rightBound = new int[n + 1];
12+
Stack<Integer> stack = new Stack<>();
13+
for (int i = 0; i < n; i++) {
14+
while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) {
15+
stack.pop();
16+
}
17+
if (stack.isEmpty()) {
18+
leftBound[i] = 0;
19+
} else {
20+
leftBound[i] = stack.peek() + 1;
21+
}
22+
stack.push(i);
23+
}
24+
stack.clear();
25+
for (int i = n - 1; i >= 0; i--) {
26+
while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) {
27+
stack.pop();
28+
}
29+
if (stack.isEmpty()) {
30+
rightBound[i] = n - 1;
31+
} else {
32+
rightBound[i] = stack.peek() - 1;
33+
}
34+
stack.push(i);
35+
}
36+
long[] preSum = new long[n + 1];
37+
for (int i = 0; i < n; i++) {
38+
preSum[i + 1] = preSum[i] + nums[i];
39+
}
40+
long maxProduct = 0;
41+
for (int i = 0; i < n; i++) {
42+
maxProduct = Math.max(maxProduct, nums[i] * getSum(preSum, leftBound[i], rightBound[i]));
43+
}
44+
return (int) (maxProduct % 1000000007);
45+
}
46+
47+
private long getSum(long[] preSum, int left, int right) {
48+
if (left > right) {
49+
return 0;
50+
}
51+
return preSum[right + 1] - preSum[left];
52+
}
53+
}

0 commit comments

Comments
 (0)