Skip to content

Added tasks 3110-3123 #1749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/g3101_3200/s3110_score_of_a_string/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package g3101_3200.s3110_score_of_a_string;

// #Easy #String #2024_04_27_Time_1_ms_(99.93%)_Space_41.4_MB_(99.03%)

public class Solution {
public int scoreOfString(String s) {
int sum = 0;
for (int i = 0; i < s.length() - 1; i++) {
sum += Math.abs((s.charAt(i) - '0') - (s.charAt(i + 1) - '0'));
}
return sum;
}
}
32 changes: 32 additions & 0 deletions src/main/java/g3101_3200/s3110_score_of_a_string/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
3110\. Score of a String

Easy

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.

Return the **score** of `s`.

**Example 1:**

**Input:** s = "hello"

**Output:** 13

**Explanation:**

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`.

**Example 2:**

**Input:** s = "zaz"

**Output:** 50

**Explanation:**

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`.

**Constraints:**

* `2 <= s.length <= 100`
* `s` consists only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g3101_3200.s3111_minimum_rectangles_to_cover_points;

// #Medium #Array #Sorting #Greedy #2024_04_27_Time_4_ms_(99.55%)_Space_97.4_MB_(47.05%)

import java.util.Arrays;

public class Solution {
public int minRectanglesToCoverPoints(int[][] points, int w) {
Arrays.sort(points, (a, b) -> a[0] - b[0]);
int res = 0;
int last = -1;
for (int[] a : points) {
if (a[0] > last) {
res++;
last = a[0] + w;
}
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
3111\. Minimum Rectangles to Cover Points

Medium

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.

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.

A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.

Return an integer denoting the **minimum** number of rectangles needed so that each point is covered by **at least one** rectangle_._

**Note:** A point may be covered by more than one rectangle.

**Example 1:**

![](https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png)

**Input:** points = [[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1

**Output:** 2

**Explanation:**

The image above shows one possible placement of rectangles to cover the points:

* A rectangle with a lower end at `(1, 0)` and its upper end at `(2, 8)`
* A rectangle with a lower end at `(3, 0)` and its upper end at `(4, 8)`

**Example 2:**

![](https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png)

**Input:** points = [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2

**Output:** 3

**Explanation:**

The image above shows one possible placement of rectangles to cover the points:

* A rectangle with a lower end at `(0, 0)` and its upper end at `(2, 2)`
* A rectangle with a lower end at `(3, 0)` and its upper end at `(5, 5)`
* A rectangle with a lower end at `(6, 0)` and its upper end at `(6, 6)`

**Example 3:**

![](https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png)

**Input:** points = [[2,3],[1,2]], w = 0

**Output:** 2

**Explanation:**

The image above shows one possible placement of rectangles to cover the points:

* A rectangle with a lower end at `(1, 0)` and its upper end at `(1, 2)`
* A rectangle with a lower end at `(2, 0)` and its upper end at `(2, 3)`

**Constraints:**

* <code>1 <= points.length <= 10<sup>5</sup></code>
* `points[i].length == 2`
* <code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code>
* <code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code>
* <code>0 <= w <= 10<sup>9</sup></code>
* All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package g3101_3200.s3112_minimum_time_to_visit_disappearing_nodes;

// #Medium #Array #Heap_Priority_Queue #Graph #Shortest_Path
// #2024_04_27_Time_10_ms_(100.00%)_Space_85.4_MB_(99.80%)

import java.util.Arrays;

public class Solution {
public int[] minimumTime(int n, int[][] edges, int[] disappear) {
int[] dist = new int[n];
Arrays.fill(dist, Integer.MAX_VALUE);
boolean exit = false;
int i;
int src;
int dest;
int cost;
dist[0] = 0;
for (i = 0; i < n && !exit; ++i) {
exit = true;
for (int[] edge : edges) {
src = edge[0];
dest = edge[1];
cost = edge[2];
if (dist[src] != -1
&& dist[src] != Integer.MAX_VALUE
&& dist[src] < disappear[src]
&& dist[src] + cost < dist[dest]) {
exit = false;
dist[dest] = dist[src] + cost;
}
if (dist[dest] != -1
&& dist[dest] != Integer.MAX_VALUE
&& dist[dest] < disappear[dest]
&& dist[dest] + cost < dist[src]) {
exit = false;
dist[src] = dist[dest] + cost;
}
}
}
for (i = 0; i < dist.length; ++i) {
if (dist[i] == Integer.MAX_VALUE || dist[i] >= disappear[i]) {
dist[i] = -1;
}
}
return dist;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
3112\. Minimum Time to Visit Disappearing Nodes

Medium

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.

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.

**Notice** that the graph might be disconnected and might contain multiple edges.

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`.

**Example 1:**

![](https://assets.leetcode.com/uploads/2024/03/09/example1.png)

**Input:** n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]

**Output:** [0,-1,4]

**Explanation:**

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.

* For node 0, we don't need any time as it is our starting point.
* 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.
* For node 2, we need at least 4 units of time to traverse `edges[2]`.

**Example 2:**

![](https://assets.leetcode.com/uploads/2024/03/09/example2.png)

**Input:** n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]

**Output:** [0,2,3]

**Explanation:**

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.

* For node 0, we don't need any time as it is the starting point.
* For node 1, we need at least 2 units of time to traverse `edges[0]`.
* For node 2, we need at least 3 units of time to traverse `edges[0]` and `edges[1]`.

**Example 3:**

**Input:** n = 2, edges = [[0,1,1]], disappear = [1,1]

**Output:** [0,-1]

**Explanation:**

Exactly when we reach node 1, it disappears.

**Constraints:**

* <code>1 <= n <= 5 * 10<sup>4</sup></code>
* <code>0 <= edges.length <= 10<sup>5</sup></code>
* <code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code>
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code>
* <code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code>
* `disappear.length == n`
* <code>1 <= disappear[i] <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3101_3200.s3113_find_the_number_of_subarrays_where_boundary_elements_are_maximum;

// #Hard #Array #Binary_Search #Stack #Monotonic_Stack
// #2024_04_27_Time_13_ms_(98.83%)_Space_60.4_MB_(67.66%)

import java.util.ArrayDeque;

public class Solution {
public long numberOfSubarrays(int[] nums) {
ArrayDeque<int[]> stack = new ArrayDeque<>();
long res = 0;
for (int a : nums) {
while (!stack.isEmpty() && stack.peek()[0] < a) {
stack.pop();
}
if (stack.isEmpty() || stack.peek()[0] != a) {
stack.push(new int[] {a, 0});
}
res += ++stack.peek()[1];
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
3113\. Find the Number of Subarrays Where Boundary Elements Are Maximum

Hard

You are given an array of **positive** integers `nums`.

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.

**Example 1:**

**Input:** nums = [1,4,3,3,2]

**Output:** 6

**Explanation:**

There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:

* 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.
* 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.
* 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.
* 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.
* 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.
* 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.

Hence, we return 6.

**Example 2:**

**Input:** nums = [3,3,3]

**Output:** 6

**Explanation:**

There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:

* 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.
* 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.
* 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.
* 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.
* 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.
* 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.

Hence, we return 6.

**Example 3:**

**Input:** nums = [1]

**Output:** 1

**Explanation:**

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.

Hence, we return 1.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package g3101_3200.s3114_latest_time_you_can_obtain_after_replacing_characters;

// #Easy #String #Enumeration #2024_04_27_Time_1_ms_(100.00%)_Space_42.5_MB_(85.42%)

public class Solution {
public String findLatestTime(String s) {
StringBuilder nm = new StringBuilder();
if (s.charAt(0) == '?' && s.charAt(1) == '?') {
nm.append("11");
} else if (s.charAt(0) != '?' && s.charAt(1) == '?') {
nm.append(s.charAt(0));
if (s.charAt(0) == '1') {
nm.append("1");
} else {
nm.append("9");
}
} else if (s.charAt(0) == '?' && s.charAt(1) != '?') {
if (s.charAt(1) >= '2' && s.charAt(1) <= '9') {
nm.append("0");
} else {
nm.append("1");
}
nm.append(s.charAt(1));
} else {
nm.append(s.charAt(0));
nm.append(s.charAt(1));
}
nm.append(":");
if (s.charAt(3) == '?' && s.charAt(4) == '?') {
nm.append("59");
} else if (s.charAt(3) != '?' && s.charAt(4) == '?') {
nm.append(s.charAt(3));
nm.append("9");
} else if (s.charAt(3) == '?' && s.charAt(4) != '?') {
nm.append("5");
nm.append(s.charAt(4));
} else {
nm.append(s.charAt(3));
nm.append(s.charAt(4));
}
return nm.toString();
}
}
Loading
Loading