Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g3701_3800.s3783_mirror_distance_of_an_integer;

// #Easy #Math #Mid_Level #Weekly_Contest_481 #2026_05_22_Time_1_ms_(99.88%)_Space_42.74_MB_(18.96%)

public class Solution {
private int rev(int n) {
int a = 0;
while (n > 0) {
a = a * 10 + (n % 10);
n /= 10;
}
return a;
}

public int mirrorDistance(int n) {
int m = rev(n);
return Math.abs(m - n);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
3783\. Mirror Distance of an Integer

Easy

You are given an integer `n`.

Define its **mirror distance** as: `abs(n - reverse(n))` where `reverse(n)` is the integer formed by reversing the digits of `n`.

Return an integer denoting the mirror distance of `n`.

`abs(x)` denotes the absolute value of `x`.

**Example 1:**

**Input:** n = 25

**Output:** 27

**Explanation:**

* `reverse(25) = 52`.
* Thus, the answer is `abs(25 - 52) = 27`.

**Example 2:**

**Input:** n = 10

**Output:** 9

**Explanation:**

* `reverse(10) = 01` which is 1.
* Thus, the answer is `abs(10 - 1) = 9`.

**Example 3:**

**Input:** n = 7

**Output:** 0

**Explanation:**

* `reverse(7) = 7`.
* Thus, the answer is `abs(7 - 7) = 0`.

**Constraints:**

* <code>1 <= n <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g3701_3800.s3784_minimum_deletion_cost_to_make_all_characters_equal;

// #Medium #Array #String #Hash_Table #Enumeration #Senior #Weekly_Contest_481
// #2026_05_22_Time_3_ms_(94.77%)_Space_104.58_MB_(97.09%)

public class Solution {
public long minCost(String s, int[] cost) {
long[] arr = new long[26];
long m = Integer.MIN_VALUE;
long sum = 0;
for (int i = 0; i < s.length(); i++) {
arr[s.charAt(i) - 'a'] += cost[i];
}
for (long i : arr) {
if (i > m) {
m = i;
}
sum += i;
}
return sum - m;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
3784\. Minimum Deletion Cost to Make All Characters Equal

Medium

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

You may delete any number of characters from `s` (possibly none), such that the resulting string is **non-empty** and consists of **equal** characters.

Return an integer denoting the **minimum** total deletion cost required.

**Example 1:**

**Input:** s = "aabaac", cost = [1,2,3,4,1,10]

**Output:** 11

**Explanation:**

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

**Example 2:**

**Input:** s = "abc", cost = [10,5,8]

**Output:** 13

**Explanation:**

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

**Example 3:**

**Input:** s = "zzzzz", cost = [67,67,67,67,67]

**Output:** 0

**Explanation:**

All characters in `s` are equal, so the deletion cost is 0.

**Constraints:**

* `n == s.length == cost.length`
* <code>1 <= n <= 10<sup>5</sup></code>
* <code>1 <= cost[i] <= 10<sup>9</sup></code>
* `s` consists of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package g3701_3800.s3785_minimum_swaps_to_avoid_forbidden_values;

// #Hard #Array #Hash_Table #Greedy #Counting #Senior_Staff #Weekly_Contest_481
// #2026_05_22_Time_88_ms_(86.89%)_Space_159.68_MB_(81.97%)

import java.util.HashMap;
import java.util.Map;

public class Solution {
public int minSwaps(int[] nums, int[] forbidden) {
int n = nums.length;
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
for (int num : forbidden) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
for (int val : map.values()) {
if (val > n) {
return -1;
}
}
Map<Integer, Integer> map2 = new HashMap<>();
int total = 0;
for (int i = 0; i < n; i++) {
if (nums[i] == forbidden[i]) {
map2.put(nums[i], map2.getOrDefault(nums[i], 0) + 1);
total++;
}
}
if (total == 0) {
return 0;
}
int maxSwaps = 0;
for (int num : map2.values()) {
maxSwaps = Math.max(maxSwaps, num);
}
return Math.max(maxSwaps, (total + 1) / 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
3785\. Minimum Swaps to Avoid Forbidden Values

Hard

You are given two integer arrays, `nums` and `forbidden`, each of length `n`.

You may perform the following operation any number of times (including zero):

* Choose two **distinct** indices `i` and `j`, and swap `nums[i]` with `nums[j]`.

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.

**Example 1:**

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

**Output:** 1

**Explanation:**

One optimal set of swaps:

* Select indices `i = 0` and `j = 1` in `nums` and swap them, resulting in `nums = [2, 1, 3]`.
* After this swap, for every index `i`, `nums[i]` is not equal to `forbidden[i]`.

**Example 2:**

**Input:** nums = [4,6,6,5], forbidden = [4,6,5,5]

**Output:** 2

**Explanation:**

One optimal set of swaps:

* Select indices `i = 0` and `j = 2` in `nums` and swap them, resulting in `nums = [6, 6, 4, 5]`.
* Select indices `i = 1` and `j = 3` in `nums` and swap them, resulting in `nums = [6, 5, 4, 6]`.
* After these swaps, for every index `i`, `nums[i]` is not equal to `forbidden[i]`.

**Example 3:**

**Input:** nums = [7,7], forbidden = [8,7]

**Output:** \-1

**Explanation:**

It is not possible to make `nums[i]` different from `forbidden[i]` for all indices.

**Example 4:**

**Input:** nums = [1,2], forbidden = [2,1]

**Output:** 0

**Explanation:**

No swaps are required because `nums[i]` is already different from `forbidden[i]` for all indices, so the answer is 0.

**Constraints:**

* <code>1 <= n == nums.length == forbidden.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i], forbidden[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package g3701_3800.s3786_total_sum_of_interaction_cost_in_tree_groups;

// #Hard #Array #Tree #Senior_Staff #Weekly_Contest_481 #Depth_First_Search
// #2026_05_22_Time_82_ms_(90.67%)_Space_296.78_MB_(21.33%)

import java.util.ArrayList;
import java.util.List;

public class Solution {
private long totalCost = 0;
private int[][] counts;
private int[] totalInGroup;
private List<List<Integer>> adj;

public long interactionCosts(int n, int[][] edges, int[] group) {
adj = new ArrayList<>();
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}
for (int[] edge : edges) {
adj.get(edge[0]).add(edge[1]);
adj.get(edge[1]).add(edge[0]);
}
totalInGroup = new int[21];
for (int g : group) {
totalInGroup[g]++;
}
counts = new int[n][21];
dfs(0, -1, group);
return totalCost;
}

private void dfs(int u, int p, int[] group) {
counts[u][group[u]] = 1;
for (int v : adj.get(u)) {
if (v == p) {
continue;
}
dfs(v, u, group);
for (int g = 1; g <= 20; g++) {
if (totalInGroup[g] < 2) {
continue;
}
long inSubtree = counts[v][g];
long outsideSubtree = totalInGroup[g] - inSubtree;
totalCost += inSubtree * outsideSubtree;
counts[u][g] += counts[v][g];
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
3786\. Total Sum of Interaction Cost in Tree Groups

Hard

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

You are also given an integer array `group` of length `n`, where `group[i]` denotes the group label assigned to node `i`.

* Two nodes `u` and `v` are considered part of the same group if `group[u] == group[v]`.
* The **interaction cost** between `u` and `v` is defined as the number of edges on the unique path connecting them in the tree.

Return an integer denoting the **sum** of interaction costs over all **unordered** pairs `(u, v)` with `u != v` such that `group[u] == group[v]`.

**Example 1:**

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

**Output:** 4

**Explanation:**

**![](https://assets.leetcode.com/uploads/2025/09/24/screenshot-2025-09-24-at-50538-pm.png)**

All nodes belong to group 1. The interaction costs between the pairs of nodes are:

* Nodes `(0, 1)`: 1
* Nodes `(1, 2)`: 1
* Nodes `(0, 2)`: 2

Thus, the total interaction cost is `1 + 1 + 2 = 4`.

**Example 2:**

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

**Output:** 2

**Explanation:**

* Nodes 0 and 2 belong to group 3. The interaction cost between this pair is 2.
* Node 1 belongs to a different group and forms no valid pair. Therefore, the total interaction cost is 2.

**Example 3:**

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

**Output:** 3

**Explanation:**

![](https://assets.leetcode.com/uploads/2025/09/24/screenshot-2025-09-24-at-51312-pm.png)

Nodes belonging to the same groups and their interaction costs are:

* Group 1: Nodes `(0, 1)`: 1
* Group 4: Nodes `(2, 3)`: 2

Thus, the total interaction cost is `1 + 2 = 3`.

**Example 4:**

**Input:** n = 2, edges = [[0,1]], group = [9,8]

**Output:** 0

**Explanation:**

All nodes belong to different groups and there are no valid pairs. Therefore, the total interaction cost is 0.

**Constraints:**

* <code>1 <= n <= 10<sup>5</sup></code>
* `edges.length == n - 1`
* <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code>
* `group.length == n`
* `1 <= group[i] <= 20`
* The input is generated such that `edges` represents a valid tree.
Loading
Loading