Skip to content

Commit 3abd21a

Browse files
authored
Added tasks 3423-3430
1 parent bd1485b commit 3abd21a

File tree

24 files changed

+977
-0
lines changed

24 files changed

+977
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3401_3500.s3423_maximum_difference_between_adjacent_elements_in_a_circular_array
2+
3+
// #Easy #2025_01_19_Time_2_(100.00%)_Space_38.80_(100.00%)
4+
5+
import kotlin.math.abs
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maxAdjacentDistance(nums: IntArray): Int {
10+
var maxDiff = 0
11+
for (i in nums.indices) {
12+
val nextIndex = (i + 1) % nums.size
13+
val diff = abs((nums[i] - nums[nextIndex]))
14+
maxDiff = max(maxDiff, diff)
15+
}
16+
return maxDiff
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
3423\. Maximum Difference Between Adjacent Elements in a Circular Array
2+
3+
Easy
4+
5+
Given a **circular** array `nums`, find the **maximum** absolute difference between adjacent elements.
6+
7+
**Note**: In a circular array, the first and last elements are adjacent.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,4]
12+
13+
**Output:** 3
14+
15+
**Explanation:**
16+
17+
Because `nums` is circular, `nums[0]` and `nums[2]` are adjacent. They have the maximum absolute difference of `|4 - 1| = 3`.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [-5,-10,-5]
22+
23+
**Output:** 5
24+
25+
**Explanation:**
26+
27+
The adjacent elements `nums[0]` and `nums[1]` have the maximum absolute difference of `|-5 - (-10)| = 5`.
28+
29+
**Constraints:**
30+
31+
* `2 <= nums.length <= 100`
32+
* `-100 <= nums[i] <= 100`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3401_3500.s3424_minimum_cost_to_make_arrays_identical
2+
3+
// #Medium #Array #Sorting #Greedy #2025_01_23_Time_38_(100.00%)_Space_64.36_(97.14%)
4+
5+
import kotlin.math.abs
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun minCost(arr: IntArray, brr: IntArray, k: Long): Long {
10+
val n = arr.size
11+
var sum1: Long = 0
12+
var sum2: Long
13+
for (i in 0..<n) {
14+
sum1 = sum1 + abs((arr[i] - brr[i]))
15+
}
16+
if (k < sum1) {
17+
arr.sort()
18+
brr.sort()
19+
sum2 = k
20+
for (i in 0..<n) {
21+
sum2 = sum2 + abs((arr[i] - brr[i]))
22+
}
23+
} else {
24+
return sum1
25+
}
26+
return min(sum1, sum2)
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3424\. Minimum Cost to Make Arrays Identical
2+
3+
Medium
4+
5+
You are given two integer arrays `arr` and `brr` of length `n`, and an integer `k`. You can perform the following operations on `arr` _any_ number of times:
6+
7+
* Split `arr` into _any_ number of **contiguous** **non-empty subarrays** and rearrange these subarrays in _any order_. This operation has a fixed cost of `k`.
8+
* Choose any element in `arr` and add or subtract a positive integer `x` to it. The cost of this operation is `x`.
9+
10+
11+
Return the **minimum** total cost to make `arr` **equal** to `brr`.
12+
13+
**Example 1:**
14+
15+
**Input:** arr = [-7,9,5], brr = [7,-2,-5], k = 2
16+
17+
**Output:** 13
18+
19+
**Explanation:**
20+
21+
* Split `arr` into two contiguous subarrays: `[-7]` and `[9, 5]` and rearrange them as `[9, 5, -7]`, with a cost of 2.
22+
* Subtract 2 from element `arr[0]`. The array becomes `[7, 5, -7]`. The cost of this operation is 2.
23+
* Subtract 7 from element `arr[1]`. The array becomes `[7, -2, -7]`. The cost of this operation is 7.
24+
* Add 2 to element `arr[2]`. The array becomes `[7, -2, -5]`. The cost of this operation is 2.
25+
26+
The total cost to make the arrays equal is `2 + 2 + 7 + 2 = 13`.
27+
28+
**Example 2:**
29+
30+
**Input:** arr = [2,1], brr = [2,1], k = 0
31+
32+
**Output:** 0
33+
34+
**Explanation:**
35+
36+
Since the arrays are already equal, no operations are needed, and the total cost is 0.
37+
38+
**Constraints:**
39+
40+
* <code>1 <= arr.length == brr.length <= 10<sup>5</sup></code>
41+
* <code>0 <= k <= 2 * 10<sup>10</sup></code>
42+
* <code>-10<sup>5</sup> <= arr[i] <= 10<sup>5</sup></code>
43+
* <code>-10<sup>5</sup> <= brr[i] <= 10<sup>5</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package g3401_3500.s3425_longest_special_path
2+
3+
// #Hard #2025_01_19_Time_106_(100.00%)_Space_187.68_(100.00%)
4+
5+
class Solution {
6+
private lateinit var adj: Array<ArrayList<IntArray>>
7+
private lateinit var nums: IntArray
8+
private lateinit var dist: IntArray
9+
private lateinit var lastOccur: IntArray
10+
private lateinit var pathStack: ArrayList<Int>
11+
private var minIndex = 0
12+
private var maxLen = 0
13+
private var minNodesForMaxLen = 0
14+
15+
fun longestSpecialPath(edges: Array<IntArray>, nums: IntArray): IntArray {
16+
val n = nums.size
17+
this.nums = nums
18+
adj = Array<ArrayList<IntArray>>(n) { ArrayList<IntArray>() }
19+
for (i in 0..<n) {
20+
adj[i] = ArrayList<IntArray>()
21+
}
22+
for (e in edges) {
23+
val u = e[0]
24+
val v = e[1]
25+
val w = e[2]
26+
adj[u].add(intArrayOf(v, w))
27+
adj[v].add(intArrayOf(u, w))
28+
}
29+
dist = IntArray(n)
30+
buildDist(0, -1, 0)
31+
var maxVal = 0
32+
for (`val` in nums) {
33+
if (`val` > maxVal) {
34+
maxVal = `val`
35+
}
36+
}
37+
lastOccur = IntArray(maxVal + 1)
38+
lastOccur.fill(-1)
39+
pathStack = ArrayList<Int>()
40+
minIndex = 0
41+
maxLen = 0
42+
minNodesForMaxLen = Int.Companion.MAX_VALUE
43+
dfs(0, -1)
44+
return intArrayOf(maxLen, minNodesForMaxLen)
45+
}
46+
47+
private fun buildDist(u: Int, parent: Int, currDist: Int) {
48+
dist[u] = currDist
49+
for (edge in adj[u]) {
50+
val v = edge[0]
51+
val w = edge[1]
52+
if (v == parent) {
53+
continue
54+
}
55+
buildDist(v, u, currDist + w)
56+
}
57+
}
58+
59+
private fun dfs(u: Int, parent: Int) {
60+
val stackPos = pathStack.size
61+
pathStack.add(u)
62+
val `val` = nums[u]
63+
val oldPos = lastOccur[`val`]
64+
val oldMinIndex = minIndex
65+
lastOccur[`val`] = stackPos
66+
if (oldPos >= minIndex) {
67+
minIndex = oldPos + 1
68+
}
69+
if (minIndex <= stackPos) {
70+
val ancestor = pathStack[minIndex]
71+
val pathLength = dist[u] - dist[ancestor]
72+
val pathNodes = stackPos - minIndex + 1
73+
if (pathLength > maxLen) {
74+
maxLen = pathLength
75+
minNodesForMaxLen = pathNodes
76+
} else if (pathLength == maxLen && pathNodes < minNodesForMaxLen) {
77+
minNodesForMaxLen = pathNodes
78+
}
79+
}
80+
for (edge in adj[u]) {
81+
val v = edge[0]
82+
if (v == parent) {
83+
continue
84+
}
85+
dfs(v, u)
86+
}
87+
pathStack.removeAt(pathStack.size - 1)
88+
lastOccur[`val`] = oldPos
89+
minIndex = oldMinIndex
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
3425\. Longest Special Path
2+
3+
Hard
4+
5+
You are given an undirected tree rooted at node `0` with `n` nodes numbered from `0` to `n - 1`, represented by a 2D array `edges` of length `n - 1`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> indicates an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> with length <code>length<sub>i</sub></code>. You are also given an integer array `nums`, where `nums[i]` represents the value at node `i`.
6+
7+
A **special path** is defined as a **downward** path from an ancestor node to a descendant node such that all the values of the nodes in that path are **unique**.
8+
9+
**Note** that a path may start and end at the same node.
10+
11+
Return an array `result` of size 2, where `result[0]` is the **length** of the **longest** special path, and `result[1]` is the **minimum** number of nodes in all _possible_ **longest** special paths.
12+
13+
**Example 1:**
14+
15+
**Input:** edges = [[0,1,2],[1,2,3],[1,3,5],[1,4,4],[2,5,6]], nums = [2,1,2,1,3,1]
16+
17+
**Output:** [6,2]
18+
19+
**Explanation:**
20+
21+
#### In the image below, nodes are colored by their corresponding values in `nums`
22+
23+
![](https://assets.leetcode.com/uploads/2024/11/02/tree3.jpeg)
24+
25+
The longest special paths are `2 -> 5` and `0 -> 1 -> 4`, both having a length of 6. The minimum number of nodes across all longest special paths is 2.
26+
27+
**Example 2:**
28+
29+
**Input:** edges = [[1,0,8]], nums = [2,2]
30+
31+
**Output:** [0,1]
32+
33+
**Explanation:**
34+
35+
![](https://assets.leetcode.com/uploads/2024/11/02/tree4.jpeg)
36+
37+
The longest special paths are `0` and `1`, both having a length of 0. The minimum number of nodes across all longest special paths is 1.
38+
39+
**Constraints:**
40+
41+
* <code>2 <= n <= 5 * 10<sup>4</sup></code>
42+
* `edges.length == n - 1`
43+
* `edges[i].length == 3`
44+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < n</code>
45+
* <code>1 <= length<sub>i</sub> <= 10<sup>3</sup></code>
46+
* `nums.length == n`
47+
* <code>0 <= nums[i] <= 5 * 10<sup>4</sup></code>
48+
* The input is generated such that `edges` represents a valid tree.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g3401_3500.s3426_manhattan_distances_of_all_arrangements_of_pieces
2+
3+
// #Hard #2025_01_19_Time_21_(100.00%)_Space_34.61_(100.00%)
4+
5+
class Solution {
6+
private fun comb(a: Long, b: Long, mod: Long): Long {
7+
if (b > a) {
8+
return 0
9+
}
10+
var numer: Long = 1
11+
var denom: Long = 1
12+
for (i in 0..<b) {
13+
numer = numer * (a - i) % mod
14+
denom = denom * (i + 1) % mod
15+
}
16+
var denomInv: Long = 1
17+
var exp = mod - 2
18+
while (exp > 0) {
19+
if (exp % 2 > 0) {
20+
denomInv = denomInv * denom % mod
21+
}
22+
denom = denom * denom % mod
23+
exp /= 2
24+
}
25+
return numer * denomInv % mod
26+
}
27+
28+
fun distanceSum(m: Int, n: Int, k: Int): Int {
29+
var res: Long = 0
30+
val mod: Long = 1000000007
31+
val base = comb(m.toLong() * n - 2, k - 2L, mod)
32+
for (d in 1..<n) {
33+
res = (res + d.toLong() * (n - d) % mod * m % mod * m % mod) % mod
34+
}
35+
for (d in 1..<m) {
36+
res = (res + d.toLong() * (m - d) % mod * n % mod * n % mod) % mod
37+
}
38+
return (res * base % mod).toInt()
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3426\. Manhattan Distances of All Arrangements of Pieces
2+
3+
Hard
4+
5+
You are given three integers `m`, `n`, and `k`.
6+
7+
There is a rectangular grid of size `m × n` containing `k` identical pieces. Return the sum of Manhattan distances between every pair of pieces over all **valid arrangements** of pieces.
8+
9+
A **valid arrangement** is a placement of all `k` pieces on the grid with **at most** one piece per cell.
10+
11+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
12+
13+
The Manhattan Distance between two cells <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and <code>(x<sub>j</sub>, y<sub>j</sub>)</code> is <code>|x<sub>i</sub> - x<sub>j</sub>| + |y<sub>i</sub> - y<sub>j</sub>|</code>.
14+
15+
**Example 1:**
16+
17+
**Input:** m = 2, n = 2, k = 2
18+
19+
**Output:** 8
20+
21+
**Explanation:**
22+
23+
The valid arrangements of pieces on the board are:
24+
25+
![](https://assets.leetcode.com/uploads/2024/12/25/4040example1.drawio)![](https://assets.leetcode.com/uploads/2024/12/25/untitled-diagramdrawio.png)
26+
27+
* In the first 4 arrangements, the Manhattan distance between the two pieces is 1.
28+
* In the last 2 arrangements, the Manhattan distance between the two pieces is 2.
29+
30+
Thus, the total Manhattan distance across all valid arrangements is `1 + 1 + 1 + 1 + 2 + 2 = 8`.
31+
32+
**Example 2:**
33+
34+
**Input:** m = 1, n = 4, k = 3
35+
36+
**Output:** 20
37+
38+
**Explanation:**
39+
40+
The valid arrangements of pieces on the board are:
41+
42+
![](https://assets.leetcode.com/uploads/2024/12/25/4040example2drawio.png)
43+
44+
* The first and last arrangements have a total Manhattan distance of `1 + 1 + 2 = 4`.
45+
* The middle two arrangements have a total Manhattan distance of `1 + 2 + 3 = 6`.
46+
47+
The total Manhattan distance between all pairs of pieces across all arrangements is `4 + 6 + 6 + 4 = 20`.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= m, n <= 10<sup>5</sup></code>
52+
* <code>2 <= m * n <= 10<sup>5</sup></code>
53+
* `2 <= k <= m * n`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g3401_3500.s3427_sum_of_variable_length_subarrays
2+
3+
// #Easy #Array #Prefix_Sum #2025_01_22_Time_0_(100.00%)_Space_43.77_(58.41%)
4+
5+
class Solution {
6+
fun subarraySum(nums: IntArray): Int {
7+
var res = nums[0]
8+
for (i in 1..<nums.size) {
9+
val j = i - nums[i] - 1
10+
nums[i] += nums[i - 1]
11+
res += nums[i] - (if (j < 0) 0 else nums[j])
12+
}
13+
return res
14+
}
15+
}

0 commit comments

Comments
 (0)