Skip to content

Commit 8fc38b6

Browse files
authored
Added tasks 1855, 1856, 1857, 1859, 1860.
1 parent c170d28 commit 8fc38b6

File tree

15 files changed

+521
-0
lines changed

15 files changed

+521
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g1801_1900.s1855_maximum_distance_between_a_pair_of_values;
2+
3+
// #Medium #Array #Greedy #Binary_Search #Two_Pointers #Binary_Search_I_Day_11
4+
// #2022_05_08_Time_4_ms_(62.20%)_Space_102.5_MB_(38.31%)
5+
6+
public class Solution {
7+
public int maxDistance(int[] n1, int[] n2) {
8+
int n = n1.length;
9+
int m = n2.length;
10+
int po1 = 0;
11+
int po2 = 0;
12+
int res = 0;
13+
while (po1 < n && po2 < m) {
14+
if (n1[po1] > n2[po2]) {
15+
po1++;
16+
} else {
17+
if (po2 != po1) {
18+
res = Math.max(res, po2 - po1);
19+
}
20+
po2++;
21+
}
22+
}
23+
return res;
24+
}
25+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
1855\. Maximum Distance Between a Pair of Values
2+
3+
Medium
4+
5+
You are given two **non-increasing 0-indexed** integer arrays `nums1` and `nums2`.
6+
7+
A pair of indices `(i, j)`, where `0 <= i < nums1.length` and `0 <= j < nums2.length`, is **valid** if both `i <= j` and `nums1[i] <= nums2[j]`. The **distance** of the pair is `j - i`.
8+
9+
Return _the **maximum distance** of any **valid** pair_ `(i, j)`_. If there are no valid pairs, return_ `0`.
10+
11+
An array `arr` is **non-increasing** if `arr[i-1] >= arr[i]` for every `1 <= i < arr.length`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]
16+
17+
**Output:** 2
18+
19+
**Explanation:** The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4). The maximum distance is 2 with pair (2,4).
20+
21+
**Example 2:**
22+
23+
**Input:** nums1 = [2,2,2], nums2 = [10,10,1]
24+
25+
**Output:** 1
26+
27+
**Explanation:** The valid pairs are (0,0), (0,1), and (1,1). The maximum distance is 1 with pair (0,1).
28+
29+
**Example 3:**
30+
31+
**Input:** nums1 = [30,29,19,5], nums2 = [25,25,25,25,25]
32+
33+
**Output:** 2
34+
35+
**Explanation:** The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4). The maximum distance is 2 with pair (2,4).
36+
37+
**Constraints:**
38+
39+
* <code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code>
40+
* <code>1 <= nums1[i], nums2[j] <= 10<sup>5</sup></code>
41+
* Both `nums1` and `nums2` are **non-increasing**.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g1801_1900.s1856_maximum_subarray_min_product;
2+
3+
// #Medium #Array #Stack #Prefix_Sum #Monotonic_Stack
4+
// #2022_05_08_Time_28_ms_(92.10%)_Space_98.4_MB_(72.64%)
5+
6+
public class Solution {
7+
public int maxSumMinProduct(int[] nums) {
8+
int n = nums.length;
9+
int mod = (int) (1e9 + 7);
10+
if (n == 1) {
11+
return (int) (((long) nums[0] * (long) nums[0]) % mod);
12+
}
13+
int[] left = new int[n];
14+
left[0] = -1;
15+
for (int i = 1; i < n; i++) {
16+
int p = i - 1;
17+
while (p >= 0 && nums[p] >= nums[i]) {
18+
p = left[p];
19+
}
20+
left[i] = p;
21+
}
22+
int[] right = new int[n];
23+
right[n - 1] = n;
24+
for (int i = n - 2; i >= 0; i--) {
25+
int p = i + 1;
26+
while (p < n && nums[p] >= nums[i]) {
27+
p = right[p];
28+
}
29+
right[i] = p;
30+
}
31+
long res = 0L;
32+
long[] preSum = new long[n];
33+
preSum[0] = nums[0];
34+
for (int i = 1; i < n; i++) {
35+
preSum[i] = preSum[i - 1] + nums[i];
36+
}
37+
for (int i = 0; i < n; i++) {
38+
long sum =
39+
left[i] == -1 ? preSum[right[i] - 1] : preSum[right[i] - 1] - preSum[left[i]];
40+
long cur = nums[i] * sum;
41+
res = Math.max(cur, res);
42+
}
43+
return (int) (res % mod);
44+
}
45+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
1856\. Maximum Subarray Min-Product
2+
3+
Medium
4+
5+
The **min-product** of an array is equal to the **minimum value** in the array **multiplied by** the array's **sum**.
6+
7+
* For example, the array `[3,2,5]` (minimum value is `2`) has a min-product of `2 * (3+2+5) = 2 * 10 = 20`.
8+
9+
Given an array of integers `nums`, return _the **maximum min-product** of any **non-empty subarray** of_ `nums`. Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
10+
11+
Note that the min-product should be maximized **before** performing the modulo operation. Testcases are generated such that the maximum min-product **without** modulo will fit in a **64-bit signed integer**.
12+
13+
A **subarray** is a **contiguous** part of an array.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [1,2,3,2]
18+
19+
**Output:** 14
20+
21+
**Explanation:** The maximum min-product is achieved with the subarray [2,3,2] (minimum value is 2). 2 \* (2+3+2) = 2 \* 7 = 14.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [2,3,3,1,2]
26+
27+
**Output:** 18
28+
29+
**Explanation:** The maximum min-product is achieved with the subarray [3,3] (minimum value is 3). 3 \* (3+3) = 3 \* 6 = 18.
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [3,1,5,6,4,2]
34+
35+
**Output:** 60
36+
37+
**Explanation:** The maximum min-product is achieved with the subarray [5,6,4] (minimum value is 4). 4 \* (5+6+4) = 4 \* 15 = 60.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
42+
* <code>1 <= nums[i] <= 10<sup>7</sup></code>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package g1801_1900.s1857_largest_color_value_in_a_directed_graph;
2+
3+
// #Hard #Hash_Table #Dynamic_Programming #Graph #Counting #Memoization #Topological_Sort
4+
// #2022_05_08_Time_110_ms_(73.53%)_Space_108.2_MB_(86.97%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
11+
public class Solution {
12+
public int largestPathValue(String colors, int[][] edges) {
13+
int len = colors.length();
14+
List<Integer>[] graph = buildGraph(len, edges);
15+
16+
int[] frequencies = new int[26];
17+
HashMap<Integer, int[]> calculatedFrequencies = new HashMap<>();
18+
int[] status = new int[len];
19+
for (int i = 0; i < len; i++) {
20+
if (status[i] != 0) {
21+
continue;
22+
}
23+
int[] localMax = runDFS(graph, i, calculatedFrequencies, status, colors);
24+
25+
if (localMax[26] == -1) {
26+
Arrays.fill(frequencies, -1);
27+
break;
28+
} else {
29+
for (int color = 0; color < 26; color++) {
30+
frequencies[color] = Math.max(frequencies[color], localMax[color]);
31+
}
32+
}
33+
}
34+
35+
int max = Integer.MIN_VALUE;
36+
for (int freq : frequencies) {
37+
max = Math.max(max, freq);
38+
}
39+
40+
return max;
41+
}
42+
43+
private int[] runDFS(
44+
List<Integer>[] graph,
45+
int node,
46+
HashMap<Integer, int[]> calculatedFrequencies,
47+
int[] status,
48+
String colors) {
49+
if (calculatedFrequencies.containsKey(node)) {
50+
return calculatedFrequencies.get(node);
51+
}
52+
53+
int[] frequencies = new int[27];
54+
if (status[node] == 1) {
55+
frequencies[26] = -1;
56+
return frequencies;
57+
}
58+
59+
status[node] = 1;
60+
for (int neighbour : graph[node]) {
61+
int[] localMax = runDFS(graph, neighbour, calculatedFrequencies, status, colors);
62+
63+
if (localMax[26] == -1) {
64+
return localMax;
65+
}
66+
67+
for (int i = 0; i < 26; i++) {
68+
frequencies[i] = Math.max(frequencies[i], localMax[i]);
69+
}
70+
}
71+
status[node] = 2;
72+
73+
int color = colors.charAt(node) - 'a';
74+
frequencies[color]++;
75+
76+
calculatedFrequencies.put(node, frequencies);
77+
78+
return frequencies;
79+
}
80+
81+
private List<Integer>[] buildGraph(int n, int[][] edges) {
82+
List<Integer>[] graph = new ArrayList[n];
83+
for (int i = 0; i < n; i++) {
84+
graph[i] = new ArrayList<>();
85+
}
86+
87+
for (int[] edge : edges) {
88+
graph[edge[0]].add(edge[1]);
89+
}
90+
91+
return graph;
92+
}
93+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1857\. Largest Color Value in a Directed Graph
2+
3+
Hard
4+
5+
There is a **directed graph** of `n` colored nodes and `m` edges. The nodes are numbered from `0` to `n - 1`.
6+
7+
You are given a string `colors` where `colors[i]` is a lowercase English letter representing the **color** of the <code>i<sup>th</sup></code> node in this graph (**0-indexed**). You are also given a 2D array `edges` where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a **directed edge** from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.
8+
9+
A valid **path** in the graph is a sequence of nodes <code>x<sub>1</sub> -> x<sub>2</sub> -> x<sub>3</sub> -> ... -> x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every `1 <= i < k`. The **color value** of the path is the number of nodes that are colored the **most frequently** occurring color along that path.
10+
11+
Return _the **largest color value** of any valid path in the given graph, or_ `-1` _if the graph contains a cycle_.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/04/21/leet1.png)
16+
17+
**Input:** colors = "abaca", edges = [[0,1],[0,2],[2,3],[3,4]]
18+
19+
**Output:** 3
20+
21+
**Explanation:** The path 0 -> 2 -> 3 -> 4 contains 3 nodes that are colored `"a" (red in the above image)`.
22+
23+
**Example 2:**
24+
25+
![](https://assets.leetcode.com/uploads/2021/04/21/leet2.png)
26+
27+
**Input:** colors = "a", edges = [[0,0]]
28+
29+
**Output:** -1
30+
31+
**Explanation:** There is a cycle from 0 to 0.
32+
33+
**Constraints:**
34+
35+
* `n == colors.length`
36+
* `m == edges.length`
37+
* <code>1 <= n <= 10<sup>5</sup></code>
38+
* <code>0 <= m <= 10<sup>5</sup></code>
39+
* `colors` consists of lowercase English letters.
40+
* <code>0 <= a<sub>j</sub>, b<sub>j</sub> < n</code>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1801_1900.s1859_sorting_the_sentence;
2+
3+
// #Easy #String #Sorting #2022_05_08_Time_2_ms_(50.32%)_Space_42_MB_(55.85%)
4+
5+
import java.util.Map;
6+
import java.util.TreeMap;
7+
8+
public class Solution {
9+
public String sortSentence(String s) {
10+
String[] words = s.split(" ");
11+
TreeMap<Integer, String> treeMap = new TreeMap<>();
12+
for (String word : words) {
13+
int key = Integer.parseInt(word.charAt(word.length() - 1) + "");
14+
treeMap.put(key, word.substring(0, word.length() - 1));
15+
}
16+
StringBuilder sb = new StringBuilder();
17+
for (Map.Entry<Integer, String> entry : treeMap.entrySet()) {
18+
sb.append(entry.getValue());
19+
sb.append(" ");
20+
}
21+
return sb.substring(0, sb.length() - 1);
22+
}
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
1859\. Sorting the Sentence
2+
3+
Easy
4+
5+
A **sentence** is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters.
6+
7+
A sentence can be **shuffled** by appending the **1-indexed word position** to each word then rearranging the words in the sentence.
8+
9+
* For example, the sentence `"This is a sentence"` can be shuffled as `"sentence4 a3 is2 This1"` or `"is2 sentence4 This1 a3"`.
10+
11+
Given a **shuffled sentence** `s` containing no more than `9` words, reconstruct and return _the original sentence_.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "is2 sentence4 This1 a3"
16+
17+
**Output:** "This is a sentence"
18+
19+
**Explanation:** Sort the words in s to their original positions "This1 is2 a3 sentence4", then remove the numbers.
20+
21+
**Example 2:**
22+
23+
**Input:** s = "Myself2 Me1 I4 and3"
24+
25+
**Output:** "Me Myself and I"
26+
27+
**Explanation:** Sort the words in s to their original positions "Me1 Myself2 and3 I4", then remove the numbers.
28+
29+
**Constraints:**
30+
31+
* `2 <= s.length <= 200`
32+
* `s` consists of lowercase and uppercase English letters, spaces, and digits from `1` to `9`.
33+
* The number of words in `s` is between `1` and `9`.
34+
* The words in `s` are separated by a single space.
35+
* `s` contains no leading or trailing spaces.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g1801_1900.s1860_incremental_memory_leak;
2+
3+
// #Medium #Simulation #2022_05_08_Time_5_ms_(78.57%)_Space_41.9_MB_(34.69%)
4+
5+
public class Solution {
6+
public int[] memLeak(int memory1, int memory2) {
7+
int time = 1;
8+
while (memory1 >= time || memory2 >= time) {
9+
if (memory1 >= memory2) {
10+
memory1 -= time;
11+
} else {
12+
memory2 -= time;
13+
}
14+
time++;
15+
}
16+
return new int[] {time, memory1, memory2};
17+
}
18+
}

0 commit comments

Comments
 (0)