Skip to content

Commit d84097d

Browse files
authored
Added tasks 3110-3123
1 parent 779865d commit d84097d

File tree

36 files changed

+1408
-0
lines changed

36 files changed

+1408
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g3101_3200.s3110_score_of_a_string;
2+
3+
// #Easy #String #2024_04_27_Time_1_ms_(99.93%)_Space_41.4_MB_(99.03%)
4+
5+
public class Solution {
6+
public int scoreOfString(String s) {
7+
int sum = 0;
8+
for (int i = 0; i < s.length() - 1; i++) {
9+
sum += Math.abs((s.charAt(i) - '0') - (s.charAt(i + 1) - '0'));
10+
}
11+
return sum;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
3110\. Score of a String
2+
3+
Easy
4+
5+
You are given a string `s`. The **score** of a string is defined as the sum of the absolute difference between the **ASCII** values of adjacent characters.
6+
7+
Return the **score** of `s`.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "hello"
12+
13+
**Output:** 13
14+
15+
**Explanation:**
16+
17+
The **ASCII** values of the characters in `s` are: `'h' = 104`, `'e' = 101`, `'l' = 108`, `'o' = 111`. So, the score of `s` would be `|104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13`.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "zaz"
22+
23+
**Output:** 50
24+
25+
**Explanation:**
26+
27+
The **ASCII** values of the characters in `s` are: `'z' = 122`, `'a' = 97`. So, the score of `s` would be `|122 - 97| + |97 - 122| = 25 + 25 = 50`.
28+
29+
**Constraints:**
30+
31+
* `2 <= s.length <= 100`
32+
* `s` consists only of lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3101_3200.s3111_minimum_rectangles_to_cover_points;
2+
3+
// #Medium #Array #Sorting #Greedy #2024_04_27_Time_4_ms_(99.55%)_Space_97.4_MB_(47.05%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int minRectanglesToCoverPoints(int[][] points, int w) {
9+
Arrays.sort(points, (a, b) -> a[0] - b[0]);
10+
int res = 0;
11+
int last = -1;
12+
for (int[] a : points) {
13+
if (a[0] > last) {
14+
res++;
15+
last = a[0] + w;
16+
}
17+
}
18+
return res;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
3111\. Minimum Rectangles to Cover Points
2+
3+
Medium
4+
5+
You are given a 2D integer array `points`, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. You are also given an integer `w`. Your task is to **cover** **all** the given points with rectangles.
6+
7+
Each rectangle has its lower end at some point <code>(x<sub>1</sub>, 0)</code> and its upper end at some point <code>(x<sub>2</sub>, y<sub>2</sub>)</code>, where <code>x<sub>1</sub> <= x<sub>2</sub></code>, <code>y<sub>2</sub> >= 0</code>, and the condition <code>x<sub>2</sub> - x<sub>1</sub> <= w</code> **must** be satisfied for each rectangle.
8+
9+
A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.
10+
11+
Return an integer denoting the **minimum** number of rectangles needed so that each point is covered by **at least one** rectangle_._
12+
13+
**Note:** A point may be covered by more than one rectangle.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png)
18+
19+
**Input:** points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1
20+
21+
**Output:** 2
22+
23+
**Explanation:**
24+
25+
The image above shows one possible placement of rectangles to cover the points:
26+
27+
* A rectangle with a lower end at `(1, 0)` and its upper end at `(2, 8)`
28+
* A rectangle with a lower end at `(3, 0)` and its upper end at `(4, 8)`
29+
30+
**Example 2:**
31+
32+
![](https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png)
33+
34+
**Input:** points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2
35+
36+
**Output:** 3
37+
38+
**Explanation:**
39+
40+
The image above shows one possible placement of rectangles to cover the points:
41+
42+
* A rectangle with a lower end at `(0, 0)` and its upper end at `(2, 2)`
43+
* A rectangle with a lower end at `(3, 0)` and its upper end at `(5, 5)`
44+
* A rectangle with a lower end at `(6, 0)` and its upper end at `(6, 6)`
45+
46+
**Example 3:**
47+
48+
![](https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png)
49+
50+
**Input:** points = [[2,3],[1,2]], w = 0
51+
52+
**Output:** 2
53+
54+
**Explanation:**
55+
56+
The image above shows one possible placement of rectangles to cover the points:
57+
58+
* A rectangle with a lower end at `(1, 0)` and its upper end at `(1, 2)`
59+
* A rectangle with a lower end at `(2, 0)` and its upper end at `(2, 3)`
60+
61+
**Constraints:**
62+
63+
* <code>1 <= points.length <= 10<sup>5</sup></code>
64+
* `points[i].length == 2`
65+
* <code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code>
66+
* <code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code>
67+
* <code>0 <= w <= 10<sup>9</sup></code>
68+
* All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g3101_3200.s3112_minimum_time_to_visit_disappearing_nodes;
2+
3+
// #Medium #Array #Heap_Priority_Queue #Graph #Shortest_Path
4+
// #2024_04_27_Time_10_ms_(100.00%)_Space_85.4_MB_(99.80%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int[] minimumTime(int n, int[][] edges, int[] disappear) {
10+
int[] dist = new int[n];
11+
Arrays.fill(dist, Integer.MAX_VALUE);
12+
boolean exit = false;
13+
int i;
14+
int src;
15+
int dest;
16+
int cost;
17+
dist[0] = 0;
18+
for (i = 0; i < n && !exit; ++i) {
19+
exit = true;
20+
for (int[] edge : edges) {
21+
src = edge[0];
22+
dest = edge[1];
23+
cost = edge[2];
24+
if (dist[src] != -1
25+
&& dist[src] != Integer.MAX_VALUE
26+
&& dist[src] < disappear[src]
27+
&& dist[src] + cost < dist[dest]) {
28+
exit = false;
29+
dist[dest] = dist[src] + cost;
30+
}
31+
if (dist[dest] != -1
32+
&& dist[dest] != Integer.MAX_VALUE
33+
&& dist[dest] < disappear[dest]
34+
&& dist[dest] + cost < dist[src]) {
35+
exit = false;
36+
dist[src] = dist[dest] + cost;
37+
}
38+
}
39+
}
40+
for (i = 0; i < dist.length; ++i) {
41+
if (dist[i] == Integer.MAX_VALUE || dist[i] >= disappear[i]) {
42+
dist[i] = -1;
43+
}
44+
}
45+
return dist;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
3112\. Minimum Time to Visit Disappearing Nodes
2+
3+
Medium
4+
5+
There is an undirected graph of `n` nodes. You are given a 2D array `edges`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> describes an edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with a traversal time of <code>length<sub>i</sub></code> units.
6+
7+
Additionally, you are given an array `disappear`, where `disappear[i]` denotes the time when the node `i` disappears from the graph and you won't be able to visit it.
8+
9+
**Notice** that the graph might be disconnected and might contain multiple edges.
10+
11+
Return the array `answer`, with `answer[i]` denoting the **minimum** units of time required to reach node `i` from node 0. If node `i` is **unreachable** from node 0 then `answer[i]` is `-1`.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2024/03/09/example1.png)
16+
17+
**Input:** n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]
18+
19+
**Output:** [0,-1,4]
20+
21+
**Explanation:**
22+
23+
We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.
24+
25+
* For node 0, we don't need any time as it is our starting point.
26+
* For node 1, we need at least 2 units of time to traverse `edges[0]`. Unfortunately, it disappears at that moment, so we won't be able to visit it.
27+
* For node 2, we need at least 4 units of time to traverse `edges[2]`.
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2024/03/09/example2.png)
32+
33+
**Input:** n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]
34+
35+
**Output:** [0,2,3]
36+
37+
**Explanation:**
38+
39+
We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.
40+
41+
* For node 0, we don't need any time as it is the starting point.
42+
* For node 1, we need at least 2 units of time to traverse `edges[0]`.
43+
* For node 2, we need at least 3 units of time to traverse `edges[0]` and `edges[1]`.
44+
45+
**Example 3:**
46+
47+
**Input:** n = 2, edges = [[0,1,1]], disappear = [1,1]
48+
49+
**Output:** [0,-1]
50+
51+
**Explanation:**
52+
53+
Exactly when we reach node 1, it disappears.
54+
55+
**Constraints:**
56+
57+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
58+
* <code>0 <= edges.length <= 10<sup>5</sup></code>
59+
* <code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code>
60+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code>
61+
* <code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code>
62+
* `disappear.length == n`
63+
* <code>1 <= disappear[i] <= 10<sup>5</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3101_3200.s3113_find_the_number_of_subarrays_where_boundary_elements_are_maximum;
2+
3+
// #Hard #Array #Binary_Search #Stack #Monotonic_Stack
4+
// #2024_04_27_Time_13_ms_(98.83%)_Space_60.4_MB_(67.66%)
5+
6+
import java.util.ArrayDeque;
7+
8+
public class Solution {
9+
public long numberOfSubarrays(int[] nums) {
10+
ArrayDeque<int[]> stack = new ArrayDeque<>();
11+
long res = 0;
12+
for (int a : nums) {
13+
while (!stack.isEmpty() && stack.peek()[0] < a) {
14+
stack.pop();
15+
}
16+
if (stack.isEmpty() || stack.peek()[0] != a) {
17+
stack.push(new int[] {a, 0});
18+
}
19+
res += ++stack.peek()[1];
20+
}
21+
return res;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
3113\. Find the Number of Subarrays Where Boundary Elements Are Maximum
2+
3+
Hard
4+
5+
You are given an array of **positive** integers `nums`.
6+
7+
Return the number of subarrays of `nums`, where the **first** and the **last** elements of the subarray are _equal_ to the **largest** element in the subarray.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,4,3,3,2]
12+
13+
**Output:** 6
14+
15+
**Explanation:**
16+
17+
There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:
18+
19+
* subarray <code>[**<ins>1</ins>**,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.
20+
* subarray <code>[1,<ins>**4**</ins>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.
21+
* subarray <code>[1,4,<ins>**3**</ins>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.
22+
* subarray <code>[1,4,3,<ins>**3**</ins>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.
23+
* subarray <code>[1,4,3,3,<ins>**2**</ins>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.
24+
* subarray <code>[1,4,<ins>**3,3**</ins>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.
25+
26+
Hence, we return 6.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [3,3,3]
31+
32+
**Output:** 6
33+
34+
**Explanation:**
35+
36+
There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:
37+
38+
* subarray <code>[<ins>**3**</ins>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.
39+
* subarray <code>[3,**<ins>3</ins>**,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.
40+
* subarray <code>[3,3,<ins>**3**</ins>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.
41+
* subarray <code>[**<ins>3,3</ins>**,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.
42+
* subarray <code>[3,<ins>**3,3**</ins>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.
43+
* subarray <code>[<ins>**3,3,3**</ins>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.
44+
45+
Hence, we return 6.
46+
47+
**Example 3:**
48+
49+
**Input:** nums = [1]
50+
51+
**Output:** 1
52+
53+
**Explanation:**
54+
55+
There is a single subarray of `nums` which is <code>[**<ins>1</ins>**]</code>, with its largest element 1. The first element is 1 and the last element is also 1.
56+
57+
Hence, we return 1.
58+
59+
**Constraints:**
60+
61+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
62+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g3101_3200.s3114_latest_time_you_can_obtain_after_replacing_characters;
2+
3+
// #Easy #String #Enumeration #2024_04_27_Time_1_ms_(100.00%)_Space_42.5_MB_(85.42%)
4+
5+
public class Solution {
6+
public String findLatestTime(String s) {
7+
StringBuilder nm = new StringBuilder();
8+
if (s.charAt(0) == '?' && s.charAt(1) == '?') {
9+
nm.append("11");
10+
} else if (s.charAt(0) != '?' && s.charAt(1) == '?') {
11+
nm.append(s.charAt(0));
12+
if (s.charAt(0) == '1') {
13+
nm.append("1");
14+
} else {
15+
nm.append("9");
16+
}
17+
} else if (s.charAt(0) == '?' && s.charAt(1) != '?') {
18+
if (s.charAt(1) >= '2' && s.charAt(1) <= '9') {
19+
nm.append("0");
20+
} else {
21+
nm.append("1");
22+
}
23+
nm.append(s.charAt(1));
24+
} else {
25+
nm.append(s.charAt(0));
26+
nm.append(s.charAt(1));
27+
}
28+
nm.append(":");
29+
if (s.charAt(3) == '?' && s.charAt(4) == '?') {
30+
nm.append("59");
31+
} else if (s.charAt(3) != '?' && s.charAt(4) == '?') {
32+
nm.append(s.charAt(3));
33+
nm.append("9");
34+
} else if (s.charAt(3) == '?' && s.charAt(4) != '?') {
35+
nm.append("5");
36+
nm.append(s.charAt(4));
37+
} else {
38+
nm.append(s.charAt(3));
39+
nm.append(s.charAt(4));
40+
}
41+
return nm.toString();
42+
}
43+
}

0 commit comments

Comments
 (0)