Skip to content

Commit 61d417c

Browse files
authored
Added tasks 3110-3123
1 parent 65b2a3f commit 61d417c

File tree

36 files changed

+1449
-0
lines changed

36 files changed

+1449
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g3101_3200.s3110_score_of_a_string
2+
3+
// #Easy #String #2024_04_27_Time_144_ms_(91.51%)_Space_34.5_MB_(73.58%)
4+
5+
import kotlin.math.abs
6+
7+
class Solution {
8+
fun scoreOfString(s: String): Int {
9+
var sum = 0
10+
for (i in 0 until s.length - 1) {
11+
sum += abs(((s[i].code - '0'.code) - (s[i + 1].code - '0'.code)))
12+
}
13+
return sum
14+
}
15+
}
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,18 @@
1+
package g3101_3200.s3111_minimum_rectangles_to_cover_points
2+
3+
// #Medium #Array #Sorting #Greedy #2024_04_27_Time_701_ms_(96.15%)_Space_115.6_MB_(32.69%)
4+
5+
class Solution {
6+
fun minRectanglesToCoverPoints(points: Array<IntArray>, w: Int): Int {
7+
points.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] }
8+
var res = 0
9+
var last = -1
10+
for (a in points) {
11+
if (a[0] > last) {
12+
res++
13+
last = a[0] + w
14+
}
15+
}
16+
return res
17+
}
18+
}
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,46 @@
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_828_ms_(94.44%)_Space_114.8_MB_(100.00%)
5+
6+
class Solution {
7+
fun minimumTime(n: Int, edges: Array<IntArray>, disappear: IntArray): IntArray {
8+
val dist = IntArray(n)
9+
dist.fill(Int.MAX_VALUE)
10+
var exit = false
11+
var src: Int
12+
var dest: Int
13+
var cost: Int
14+
dist[0] = 0
15+
var i = 0
16+
while (i < n && !exit) {
17+
exit = true
18+
for (edge in edges) {
19+
src = edge[0]
20+
dest = edge[1]
21+
cost = edge[2]
22+
if (dist[src] != -1 && dist[src] != Int.MAX_VALUE &&
23+
dist[src] < disappear[src] && dist[src] + cost < dist[dest]
24+
) {
25+
exit = false
26+
dist[dest] = dist[src] + cost
27+
}
28+
if (dist[dest] != -1 && dist[dest] != Int.MAX_VALUE &&
29+
dist[dest] < disappear[dest] && dist[dest] + cost < dist[src]
30+
) {
31+
exit = false
32+
dist[src] = dist[dest] + cost
33+
}
34+
}
35+
++i
36+
}
37+
i = 0
38+
while (i < dist.size) {
39+
if (dist[i] == Int.MAX_VALUE || dist[i] >= disappear[i]) {
40+
dist[i] = -1
41+
}
42+
++i
43+
}
44+
return dist
45+
}
46+
}
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,21 @@
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_606_ms_(88.89%)_Space_63.3_MB_(83.33%)
5+
6+
class Solution {
7+
fun numberOfSubarrays(nums: IntArray): Long {
8+
val stack = ArrayDeque<IntArray>()
9+
var res: Long = 0
10+
for (a in nums) {
11+
while (stack.isNotEmpty() && stack.last()[0] < a) {
12+
stack.removeLast()
13+
}
14+
if (stack.isEmpty() || stack.last()[0] != a) {
15+
stack.addLast(intArrayOf(a, 0))
16+
}
17+
res += ++stack.last()[1]
18+
}
19+
return res
20+
}
21+
}
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_161_ms_(83.58%)_Space_35_MB_(100.00%)
4+
5+
class Solution {
6+
fun findLatestTime(s: String): String {
7+
val nm = StringBuilder()
8+
if (s[0] == '?' && s[1] == '?') {
9+
nm.append("11")
10+
} else if (s[0] != '?' && s[1] == '?') {
11+
nm.append(s[0])
12+
if (s[0] == '1') {
13+
nm.append("1")
14+
} else {
15+
nm.append("9")
16+
}
17+
} else if (s[0] == '?' && s[1] != '?') {
18+
if (s[1] in '2'..'9') {
19+
nm.append("0")
20+
} else {
21+
nm.append("1")
22+
}
23+
nm.append(s[1])
24+
} else {
25+
nm.append(s[0])
26+
nm.append(s[1])
27+
}
28+
nm.append(":")
29+
if (s[3] == '?' && s[4] == '?') {
30+
nm.append("59")
31+
} else if (s[3] != '?' && s[4] == '?') {
32+
nm.append(s[3])
33+
nm.append("9")
34+
} else if (s[3] == '?' && s[4] != '?') {
35+
nm.append("5")
36+
nm.append(s[4])
37+
} else {
38+
nm.append(s[3])
39+
nm.append(s[4])
40+
}
41+
return nm.toString()
42+
}
43+
}

0 commit comments

Comments
 (0)