Skip to content

Commit 49409ae

Browse files
authored
Added tasks 3110-3123
1 parent fffd59c commit 49409ae

File tree

13 files changed

+1157
-0
lines changed
  • src/main/kotlin/g3101_3200
    • s3110_score_of_a_string
    • s3111_minimum_rectangles_to_cover_points
    • s3112_minimum_time_to_visit_disappearing_nodes
    • s3113_find_the_number_of_subarrays_where_boundary_elements_are_maximum
    • s3114_latest_time_you_can_obtain_after_replacing_characters
    • s3115_maximum_prime_difference
    • s3116_kth_smallest_amount_with_single_denomination_combination
    • s3117_minimum_sum_of_values_by_dividing_array
    • s3120_count_the_number_of_special_characters_i
    • s3121_count_the_number_of_special_characters_ii
    • s3122_minimum_number_of_operations_to_satisfy_conditions
    • s3123_find_edges_in_shortest_paths

13 files changed

+1157
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,18 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3123 |[Find Edges in Shortest Paths](src/main/kotlin/g3101_3200/s3123_find_edges_in_shortest_paths)| Hard | Depth_First_Search, Breadth_First_Search, Heap_Priority_Queue, Graph, Shortest_Path | 931 | 100.00
1820+
| 3122 |[Minimum Number of Operations to Satisfy Conditions](src/main/kotlin/g3101_3200/s3122_minimum_number_of_operations_to_satisfy_conditions)| Medium | Array, Dynamic_Programming, Matrix | 948 | 80.77
1821+
| 3121 |[Count the Number of Special Characters II](src/main/kotlin/g3101_3200/s3121_count_the_number_of_special_characters_ii)| Medium | String, Hash_Table | 274 | 90.74
1822+
| 3120 |[Count the Number of Special Characters I](src/main/kotlin/g3101_3200/s3120_count_the_number_of_special_characters_i)| Easy | String, Hash_Table | 145 | 97.26
1823+
| 3117 |[Minimum Sum of Values by Dividing Array](src/main/kotlin/g3101_3200/s3117_minimum_sum_of_values_by_dividing_array)| Hard | Array, Dynamic_Programming, Binary_Search, Bit_Manipulation, Queue, Segment_Tree | 244 | 100.00
1824+
| 3116 |[Kth Smallest Amount With Single Denomination Combination](src/main/kotlin/g3101_3200/s3116_kth_smallest_amount_with_single_denomination_combination)| Hard | Array, Math, Binary_Search, Bit_Manipulation, Number_Theory, Combinatorics | 158 | 100.00
1825+
| 3115 |[Maximum Prime Difference](src/main/kotlin/g3101_3200/s3115_maximum_prime_difference)| Medium | Array, Math, Number_Theory | 555 | 79.63
1826+
| 3114 |[Latest Time You Can Obtain After Replacing Characters](src/main/kotlin/g3101_3200/s3114_latest_time_you_can_obtain_after_replacing_characters)| Easy | String, Enumeration | 161 | 83.58
1827+
| 3113 |[Find the Number of Subarrays Where Boundary Elements Are Maximum](src/main/kotlin/g3101_3200/s3113_find_the_number_of_subarrays_where_boundary_elements_are_maximum)| Hard | Array, Binary_Search, Stack, Monotonic_Stack | 606 | 88.89
1828+
| 3112 |[Minimum Time to Visit Disappearing Nodes](src/main/kotlin/g3101_3200/s3112_minimum_time_to_visit_disappearing_nodes)| Medium | Array, Heap_Priority_Queue, Graph, Shortest_Path | 828 | 94.44
1829+
| 3111 |[Minimum Rectangles to Cover Points](src/main/kotlin/g3101_3200/s3111_minimum_rectangles_to_cover_points)| Medium | Array, Sorting, Greedy | 701 | 96.15
1830+
| 3110 |[Score of a String](src/main/kotlin/g3101_3200/s3110_score_of_a_string)| Easy | String | 144 | 91.51
18191831
| 3108 |[Minimum Cost Walk in Weighted Graph](src/main/kotlin/g3101_3200/s3108_minimum_cost_walk_in_weighted_graph)| Hard | Array, Bit_Manipulation, Graph, Union_Find | 791 | 100.00
18201832
| 3107 |[Minimum Operations to Make Median of Array Equal to K](src/main/kotlin/g3101_3200/s3107_minimum_operations_to_make_median_of_array_equal_to_k)| Medium | Array, Sorting, Greedy | 554 | 100.00
18211833
| 3106 |[Lexicographically Smallest String After Operations With Constraint](src/main/kotlin/g3101_3200/s3106_lexicographically_smallest_string_after_operations_with_constraint)| Medium | String, Greedy | 162 | 74.19
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3110\. Score of a String
5+
6+
Easy
7+
8+
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.
9+
10+
Return the **score** of `s`.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "hello"
15+
16+
**Output:** 13
17+
18+
**Explanation:**
19+
20+
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`.
21+
22+
**Example 2:**
23+
24+
**Input:** s = "zaz"
25+
26+
**Output:** 50
27+
28+
**Explanation:**
29+
30+
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`.
31+
32+
**Constraints:**
33+
34+
* `2 <= s.length <= 100`
35+
* `s` consists only of lowercase English letters.
36+
37+
## Solution
38+
39+
```kotlin
40+
import kotlin.math.abs
41+
42+
class Solution {
43+
fun scoreOfString(s: String): Int {
44+
var sum = 0
45+
for (i in 0 until s.length - 1) {
46+
sum += abs(((s[i].code - '0'.code) - (s[i + 1].code - '0'.code)))
47+
}
48+
return sum
49+
}
50+
}
51+
```
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3111\. Minimum Rectangles to Cover Points
5+
6+
Medium
7+
8+
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.
9+
10+
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.
11+
12+
A point is considered covered by a rectangle if it lies within or on the boundary of the rectangle.
13+
14+
Return an integer denoting the **minimum** number of rectangles needed so that each point is covered by **at least one** rectangle_._
15+
16+
**Note:** A point may be covered by more than one rectangle.
17+
18+
**Example 1:**
19+
20+
![](https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-33-05.png)
21+
22+
**Input:** points = \[\[2,1],[1,0],[1,4],[1,8],[3,5],[4,6]], w = 1
23+
24+
**Output:** 2
25+
26+
**Explanation:**
27+
28+
The image above shows one possible placement of rectangles to cover the points:
29+
30+
* A rectangle with a lower end at `(1, 0)` and its upper end at `(2, 8)`
31+
* A rectangle with a lower end at `(3, 0)` and its upper end at `(4, 8)`
32+
33+
**Example 2:**
34+
35+
![](https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-18-59-12.png)
36+
37+
**Input:** points = \[\[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6]], w = 2
38+
39+
**Output:** 3
40+
41+
**Explanation:**
42+
43+
The image above shows one possible placement of rectangles to cover the points:
44+
45+
* A rectangle with a lower end at `(0, 0)` and its upper end at `(2, 2)`
46+
* A rectangle with a lower end at `(3, 0)` and its upper end at `(5, 5)`
47+
* A rectangle with a lower end at `(6, 0)` and its upper end at `(6, 6)`
48+
49+
**Example 3:**
50+
51+
![](https://assets.leetcode.com/uploads/2024/03/04/screenshot-from-2024-03-04-20-24-03.png)
52+
53+
**Input:** points = \[\[2,3],[1,2]], w = 0
54+
55+
**Output:** 2
56+
57+
**Explanation:**
58+
59+
The image above shows one possible placement of rectangles to cover the points:
60+
61+
* A rectangle with a lower end at `(1, 0)` and its upper end at `(1, 2)`
62+
* A rectangle with a lower end at `(2, 0)` and its upper end at `(2, 3)`
63+
64+
**Constraints:**
65+
66+
* <code>1 <= points.length <= 10<sup>5</sup></code>
67+
* `points[i].length == 2`
68+
* <code>0 <= x<sub>i</sub> == points[i][0] <= 10<sup>9</sup></code>
69+
* <code>0 <= y<sub>i</sub> == points[i][1] <= 10<sup>9</sup></code>
70+
* <code>0 <= w <= 10<sup>9</sup></code>
71+
* All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.
72+
73+
## Solution
74+
75+
```kotlin
76+
class Solution {
77+
fun minRectanglesToCoverPoints(points: Array<IntArray>, w: Int): Int {
78+
points.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] }
79+
var res = 0
80+
var last = -1
81+
for (a in points) {
82+
if (a[0] > last) {
83+
res++
84+
last = a[0] + w
85+
}
86+
}
87+
return res
88+
}
89+
}
90+
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3112\. Minimum Time to Visit Disappearing Nodes
5+
6+
Medium
7+
8+
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.
9+
10+
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.
11+
12+
**Notice** that the graph might be disconnected and might contain multiple edges.
13+
14+
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`.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2024/03/09/example1.png)
19+
20+
**Input:** n = 3, edges = \[\[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]
21+
22+
**Output:** [0,-1,4]
23+
24+
**Explanation:**
25+
26+
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.
27+
28+
* For node 0, we don't need any time as it is our starting point.
29+
* 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.
30+
* For node 2, we need at least 4 units of time to traverse `edges[2]`.
31+
32+
**Example 2:**
33+
34+
![](https://assets.leetcode.com/uploads/2024/03/09/example2.png)
35+
36+
**Input:** n = 3, edges = \[\[0,1,2],[1,2,1],[0,2,4]], disappear = [1,3,5]
37+
38+
**Output:** [0,2,3]
39+
40+
**Explanation:**
41+
42+
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.
43+
44+
* For node 0, we don't need any time as it is the starting point.
45+
* For node 1, we need at least 2 units of time to traverse `edges[0]`.
46+
* For node 2, we need at least 3 units of time to traverse `edges[0]` and `edges[1]`.
47+
48+
**Example 3:**
49+
50+
**Input:** n = 2, edges = \[\[0,1,1]], disappear = [1,1]
51+
52+
**Output:** [0,-1]
53+
54+
**Explanation:**
55+
56+
Exactly when we reach node 1, it disappears.
57+
58+
**Constraints:**
59+
60+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
61+
* <code>0 <= edges.length <= 10<sup>5</sup></code>
62+
* <code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code>
63+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code>
64+
* <code>1 <= length<sub>i</sub> <= 10<sup>5</sup></code>
65+
* `disappear.length == n`
66+
* <code>1 <= disappear[i] <= 10<sup>5</sup></code>
67+
68+
## Solution
69+
70+
```kotlin
71+
class Solution {
72+
fun minimumTime(n: Int, edges: Array<IntArray>, disappear: IntArray): IntArray {
73+
val dist = IntArray(n)
74+
dist.fill(Int.MAX_VALUE)
75+
var exit = false
76+
var src: Int
77+
var dest: Int
78+
var cost: Int
79+
dist[0] = 0
80+
var i = 0
81+
while (i < n && !exit) {
82+
exit = true
83+
for (edge in edges) {
84+
src = edge[0]
85+
dest = edge[1]
86+
cost = edge[2]
87+
if (dist[src] != -1 && dist[src] != Int.MAX_VALUE &&
88+
dist[src] < disappear[src] && dist[src] + cost < dist[dest]
89+
) {
90+
exit = false
91+
dist[dest] = dist[src] + cost
92+
}
93+
if (dist[dest] != -1 && dist[dest] != Int.MAX_VALUE &&
94+
dist[dest] < disappear[dest] && dist[dest] + cost < dist[src]
95+
) {
96+
exit = false
97+
dist[src] = dist[dest] + cost
98+
}
99+
}
100+
++i
101+
}
102+
i = 0
103+
while (i < dist.size) {
104+
if (dist[i] == Int.MAX_VALUE || dist[i] >= disappear[i]) {
105+
dist[i] = -1
106+
}
107+
++i
108+
}
109+
return dist
110+
}
111+
}
112+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3113\. Find the Number of Subarrays Where Boundary Elements Are Maximum
5+
6+
Hard
7+
8+
You are given an array of **positive** integers `nums`.
9+
10+
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.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [1,4,3,3,2]
15+
16+
**Output:** 6
17+
18+
**Explanation:**
19+
20+
There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:
21+
22+
* 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.
23+
* 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.
24+
* 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.
25+
* 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.
26+
* 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.
27+
* 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.
28+
29+
Hence, we return 6.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [3,3,3]
34+
35+
**Output:** 6
36+
37+
**Explanation:**
38+
39+
There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:
40+
41+
* 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.
42+
* 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.
43+
* 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.
44+
* 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.
45+
* 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.
46+
* 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.
47+
48+
Hence, we return 6.
49+
50+
**Example 3:**
51+
52+
**Input:** nums = [1]
53+
54+
**Output:** 1
55+
56+
**Explanation:**
57+
58+
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.
59+
60+
Hence, we return 1.
61+
62+
**Constraints:**
63+
64+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
65+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
66+
67+
## Solution
68+
69+
```kotlin
70+
class Solution {
71+
fun numberOfSubarrays(nums: IntArray): Long {
72+
val stack = ArrayDeque<IntArray>()
73+
var res: Long = 0
74+
for (a in nums) {
75+
while (stack.isNotEmpty() && stack.last()[0] < a) {
76+
stack.removeLast()
77+
}
78+
if (stack.isEmpty() || stack.last()[0] != a) {
79+
stack.addLast(intArrayOf(a, 0))
80+
}
81+
res += ++stack.last()[1]
82+
}
83+
return res
84+
}
85+
}
86+
```

0 commit comments

Comments
 (0)