Skip to content

Commit 0444f8c

Browse files
authored
Added tasks 3783-3805
1 parent 043da4b commit 0444f8c

60 files changed

Lines changed: 2334 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3701_3800.s3783_mirror_distance_of_an_integer;
2+
3+
// #Easy #Math #Mid_Level #Weekly_Contest_481 #2026_05_22_Time_1_ms_(99.88%)_Space_42.74_MB_(18.96%)
4+
5+
public class Solution {
6+
private int rev(int n) {
7+
int a = 0;
8+
while (n > 0) {
9+
a = a * 10 + (n % 10);
10+
n /= 10;
11+
}
12+
return a;
13+
}
14+
15+
public int mirrorDistance(int n) {
16+
int m = rev(n);
17+
return Math.abs(m - n);
18+
}
19+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
3783\. Mirror Distance of an Integer
2+
3+
Easy
4+
5+
You are given an integer `n`.
6+
7+
Define its **mirror distance** as: `abs(n - reverse(n))` where `reverse(n)` is the integer formed by reversing the digits of `n`.
8+
9+
Return an integer denoting the mirror distance of `n`.
10+
11+
`abs(x)` denotes the absolute value of `x`.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 25
16+
17+
**Output:** 27
18+
19+
**Explanation:**
20+
21+
* `reverse(25) = 52`.
22+
* Thus, the answer is `abs(25 - 52) = 27`.
23+
24+
**Example 2:**
25+
26+
**Input:** n = 10
27+
28+
**Output:** 9
29+
30+
**Explanation:**
31+
32+
* `reverse(10) = 01` which is 1.
33+
* Thus, the answer is `abs(10 - 1) = 9`.
34+
35+
**Example 3:**
36+
37+
**Input:** n = 7
38+
39+
**Output:** 0
40+
41+
**Explanation:**
42+
43+
* `reverse(7) = 7`.
44+
* Thus, the answer is `abs(7 - 7) = 0`.
45+
46+
**Constraints:**
47+
48+
* <code>1 <= n <= 10<sup>9</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3701_3800.s3784_minimum_deletion_cost_to_make_all_characters_equal;
2+
3+
// #Medium #Array #String #Hash_Table #Enumeration #Senior #Weekly_Contest_481
4+
// #2026_05_22_Time_3_ms_(94.77%)_Space_104.58_MB_(97.09%)
5+
6+
public class Solution {
7+
public long minCost(String s, int[] cost) {
8+
long[] arr = new long[26];
9+
long m = Integer.MIN_VALUE;
10+
long sum = 0;
11+
for (int i = 0; i < s.length(); i++) {
12+
arr[s.charAt(i) - 'a'] += cost[i];
13+
}
14+
for (long i : arr) {
15+
if (i > m) {
16+
m = i;
17+
}
18+
sum += i;
19+
}
20+
return sum - m;
21+
}
22+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
3784\. Minimum Deletion Cost to Make All Characters Equal
2+
3+
Medium
4+
5+
You are given a string `s` of length `n` and an integer array `cost` of the same length, where `cost[i]` is the cost to **delete** the <code>i<sup>th</sup></code> character of `s`.
6+
7+
You may delete any number of characters from `s` (possibly none), such that the resulting string is **non-empty** and consists of **equal** characters.
8+
9+
Return an integer denoting the **minimum** total deletion cost required.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "aabaac", cost = [1,2,3,4,1,10]
14+
15+
**Output:** 11
16+
17+
**Explanation:**
18+
19+
Deleting the characters at indices 0, 1, 2, 3, 4 results in the string `"c"`, which consists of equal characters, and the total cost is `cost[0] + cost[1] + cost[2] + cost[3] + cost[4] = 1 + 2 + 3 + 4 + 1 = 11`.
20+
21+
**Example 2:**
22+
23+
**Input:** s = "abc", cost = [10,5,8]
24+
25+
**Output:** 13
26+
27+
**Explanation:**
28+
29+
Deleting the characters at indices 1 and 2 results in the string `"a"`, which consists of equal characters, and the total cost is `cost[1] + cost[2] = 5 + 8 = 13`.
30+
31+
**Example 3:**
32+
33+
**Input:** s = "zzzzz", cost = [67,67,67,67,67]
34+
35+
**Output:** 0
36+
37+
**Explanation:**
38+
39+
All characters in `s` are equal, so the deletion cost is 0.
40+
41+
**Constraints:**
42+
43+
* `n == s.length == cost.length`
44+
* <code>1 <= n <= 10<sup>5</sup></code>
45+
* <code>1 <= cost[i] <= 10<sup>9</sup></code>
46+
* `s` consists of lowercase English letters.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g3701_3800.s3785_minimum_swaps_to_avoid_forbidden_values;
2+
3+
// #Hard #Array #Hash_Table #Greedy #Counting #Senior_Staff #Weekly_Contest_481
4+
// #2026_05_22_Time_88_ms_(86.89%)_Space_159.68_MB_(81.97%)
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
public class Solution {
10+
public int minSwaps(int[] nums, int[] forbidden) {
11+
int n = nums.length;
12+
Map<Integer, Integer> map = new HashMap<>();
13+
for (int num : nums) {
14+
map.put(num, map.getOrDefault(num, 0) + 1);
15+
}
16+
for (int num : forbidden) {
17+
map.put(num, map.getOrDefault(num, 0) + 1);
18+
}
19+
for (int val : map.values()) {
20+
if (val > n) {
21+
return -1;
22+
}
23+
}
24+
Map<Integer, Integer> map2 = new HashMap<>();
25+
int total = 0;
26+
for (int i = 0; i < n; i++) {
27+
if (nums[i] == forbidden[i]) {
28+
map2.put(nums[i], map2.getOrDefault(nums[i], 0) + 1);
29+
total++;
30+
}
31+
}
32+
if (total == 0) {
33+
return 0;
34+
}
35+
int maxSwaps = 0;
36+
for (int num : map2.values()) {
37+
maxSwaps = Math.max(maxSwaps, num);
38+
}
39+
return Math.max(maxSwaps, (total + 1) / 2);
40+
}
41+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
3785\. Minimum Swaps to Avoid Forbidden Values
2+
3+
Hard
4+
5+
You are given two integer arrays, `nums` and `forbidden`, each of length `n`.
6+
7+
You may perform the following operation any number of times (including zero):
8+
9+
* Choose two **distinct** indices `i` and `j`, and swap `nums[i]` with `nums[j]`.
10+
11+
Return the **minimum** number of swaps required such that, for every index `i`, the value of `nums[i]` is **not equal** to `forbidden[i]`. If no amount of swaps can ensure that every index avoids its forbidden value, return -1.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3], forbidden = [3,2,1]
16+
17+
**Output:** 1
18+
19+
**Explanation:**
20+
21+
One optimal set of swaps:
22+
23+
* Select indices `i = 0` and `j = 1` in `nums` and swap them, resulting in `nums = [2, 1, 3]`.
24+
* After this swap, for every index `i`, `nums[i]` is not equal to `forbidden[i]`.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [4,6,6,5], forbidden = [4,6,5,5]
29+
30+
**Output:** 2
31+
32+
**Explanation:**
33+
34+
One optimal set of swaps:
35+
36+
* Select indices `i = 0` and `j = 2` in `nums` and swap them, resulting in `nums = [6, 6, 4, 5]`.
37+
* Select indices `i = 1` and `j = 3` in `nums` and swap them, resulting in `nums = [6, 5, 4, 6]`.
38+
* After these swaps, for every index `i`, `nums[i]` is not equal to `forbidden[i]`.
39+
40+
**Example 3:**
41+
42+
**Input:** nums = [7,7], forbidden = [8,7]
43+
44+
**Output:** \-1
45+
46+
**Explanation:**
47+
48+
It is not possible to make `nums[i]` different from `forbidden[i]` for all indices.
49+
50+
**Example 4:**
51+
52+
**Input:** nums = [1,2], forbidden = [2,1]
53+
54+
**Output:** 0
55+
56+
**Explanation:**
57+
58+
No swaps are required because `nums[i]` is already different from `forbidden[i]` for all indices, so the answer is 0.
59+
60+
**Constraints:**
61+
62+
* <code>1 <= n == nums.length == forbidden.length <= 10<sup>5</sup></code>
63+
* <code>1 <= nums[i], forbidden[i] <= 10<sup>9</sup></code>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g3701_3800.s3786_total_sum_of_interaction_cost_in_tree_groups;
2+
3+
// #Hard #Array #Tree #Senior_Staff #Weekly_Contest_481 #Depth_First_Search
4+
// #2026_05_22_Time_82_ms_(90.67%)_Space_296.78_MB_(21.33%)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Solution {
10+
private long totalCost = 0;
11+
private int[][] counts;
12+
private int[] totalInGroup;
13+
private List<List<Integer>> adj;
14+
15+
public long interactionCosts(int n, int[][] edges, int[] group) {
16+
adj = new ArrayList<>();
17+
for (int i = 0; i < n; i++) {
18+
adj.add(new ArrayList<>());
19+
}
20+
for (int[] edge : edges) {
21+
adj.get(edge[0]).add(edge[1]);
22+
adj.get(edge[1]).add(edge[0]);
23+
}
24+
totalInGroup = new int[21];
25+
for (int g : group) {
26+
totalInGroup[g]++;
27+
}
28+
counts = new int[n][21];
29+
dfs(0, -1, group);
30+
return totalCost;
31+
}
32+
33+
private void dfs(int u, int p, int[] group) {
34+
counts[u][group[u]] = 1;
35+
for (int v : adj.get(u)) {
36+
if (v == p) {
37+
continue;
38+
}
39+
dfs(v, u, group);
40+
for (int g = 1; g <= 20; g++) {
41+
if (totalInGroup[g] < 2) {
42+
continue;
43+
}
44+
long inSubtree = counts[v][g];
45+
long outsideSubtree = totalInGroup[g] - inSubtree;
46+
totalCost += inSubtree * outsideSubtree;
47+
counts[u][g] += counts[v][g];
48+
}
49+
}
50+
}
51+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
3786\. Total Sum of Interaction Cost in Tree Groups
2+
3+
Hard
4+
5+
You are given an integer `n` and an undirected tree with `n` nodes numbered from 0 to `n - 1`. This is represented by a 2D array `edges` of length `n - 1`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates an undirected edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.
6+
7+
You are also given an integer array `group` of length `n`, where `group[i]` denotes the group label assigned to node `i`.
8+
9+
* Two nodes `u` and `v` are considered part of the same group if `group[u] == group[v]`.
10+
* The **interaction cost** between `u` and `v` is defined as the number of edges on the unique path connecting them in the tree.
11+
12+
Return an integer denoting the **sum** of interaction costs over all **unordered** pairs `(u, v)` with `u != v` such that `group[u] == group[v]`.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 3, edges = [[0,1],[1,2]], group = [1,1,1]
17+
18+
**Output:** 4
19+
20+
**Explanation:**
21+
22+
**![](https://assets.leetcode.com/uploads/2025/09/24/screenshot-2025-09-24-at-50538-pm.png)**
23+
24+
All nodes belong to group 1. The interaction costs between the pairs of nodes are:
25+
26+
* Nodes `(0, 1)`: 1
27+
* Nodes `(1, 2)`: 1
28+
* Nodes `(0, 2)`: 2
29+
30+
Thus, the total interaction cost is `1 + 1 + 2 = 4`.
31+
32+
**Example 2:**
33+
34+
**Input:** n = 3, edges = [[0,1],[1,2]], group = [3,2,3]
35+
36+
**Output:** 2
37+
38+
**Explanation:**
39+
40+
* Nodes 0 and 2 belong to group 3. The interaction cost between this pair is 2.
41+
* Node 1 belongs to a different group and forms no valid pair. Therefore, the total interaction cost is 2.
42+
43+
**Example 3:**
44+
45+
**Input:** n = 4, edges = [[0,1],[0,2],[0,3]], group = [1,1,4,4]
46+
47+
**Output:** 3
48+
49+
**Explanation:**
50+
51+
![](https://assets.leetcode.com/uploads/2025/09/24/screenshot-2025-09-24-at-51312-pm.png)
52+
53+
Nodes belonging to the same groups and their interaction costs are:
54+
55+
* Group 1: Nodes `(0, 1)`: 1
56+
* Group 4: Nodes `(2, 3)`: 2
57+
58+
Thus, the total interaction cost is `1 + 2 = 3`.
59+
60+
**Example 4:**
61+
62+
**Input:** n = 2, edges = [[0,1]], group = [9,8]
63+
64+
**Output:** 0
65+
66+
**Explanation:**
67+
68+
All nodes belong to different groups and there are no valid pairs. Therefore, the total interaction cost is 0.
69+
70+
**Constraints:**
71+
72+
* <code>1 <= n <= 10<sup>5</sup></code>
73+
* `edges.length == n - 1`
74+
* <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>
75+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code>
76+
* `group.length == n`
77+
* `1 <= group[i] <= 20`
78+
* The input is generated such that `edges` represents a valid tree.

0 commit comments

Comments
 (0)