bowang0911/leetcode huggingface dataset:
Remove Letter To Equalize Frequency

You are given a **0-indexed** string `word`, consisting of lowercase English letters. You need to select **one** index and **remove** the letter at that index from `word` so that the **frequency** of every letter present in `word` is equal.

Return `true` _if it is possible to remove one letter so that the frequency of all letters in_ `word` _are equal, and_ `false` _otherwise_.

**Note:**

*   The **frequency** of a letter `x` is the number of times it occurs in the string.
*   You **must** remove exactly one letter and cannot chose to do nothing.

**Example 1:**

**Input:** word =  "abcc "
**Output:** true
**Explanation:** Select index 3 and delete it: word becomes  "abc " and each character has a frequency of 1.

**Example 2:**

**Input:** word =  "aazz "
**Output:** false
**Explanation:** We must delete a character, so either the frequency of  "a " is 1 and the frequency of  "z " is 2, or vice versa. It is impossible to make all present letters have equal frequency.

**Constraints:**

*   `2 <= word.length <= 100`
*   `word` consists of lowercase English letters only.
Longest Uploaded Prefix

You are given a stream of `n` videos, each represented by a **distinct** number from `1` to `n` that you need to "upload " to a server. You need to implement a data structure that calculates the length of the **longest uploaded prefix** at various points in the upload process.

We consider `i` to be an uploaded prefix if all videos in the range `1` to `i` (**inclusive**) have been uploaded to the server. The longest uploaded prefix is the **maximum** value of `i` that satisfies this definition.  
  
Implement the `LUPrefix` class:

*   `LUPrefix(int n)` Initializes the object for a stream of `n` videos.
*   `void upload(int video)` Uploads `video` to the server.
*   `int longest()` Returns the length of the **longest uploaded prefix** defined above.

**Example 1:**

**Input**
\[ "LUPrefix ",  "upload ",  "longest ",  "upload ",  "longest ",  "upload ",  "longest "\]
\[\[4\], \[3\], \[\], \[1\], \[\], \[2\], \[\]\]
**Output**
\[null, null, 0, null, 1, null, 3\]

**Explanation**
LUPrefix server = new LUPrefix(4);   // Initialize a stream of 4 videos.
server.upload(3);                    // Upload video 3.
server.longest();                    // Since video 1 has not been uploaded yet, there is no prefix.
                                     // So, we return 0.
server.upload(1);                    // Upload video 1.
server.longest();                    // The prefix \[1\] is the longest uploaded prefix, so we return 1.
server.upload(2);                    // Upload video 2.
server.longest();                    // The prefix \[1,2,3\] is the longest uploaded prefix, so we return 3.

**Constraints:**

*   `1 <= n <= 105`
*   `1 <= video <= n`
*   All values of `video` are **distinct**.
*   At most `2 * 105` calls **in total** will be made to `upload` and `longest`.
*   At least one call will be made to `longest`.
Bitwise XOR of All Pairings

You are given two **0-indexed** arrays, `nums1` and `nums2`, consisting of non-negative integers. There exists another array, `nums3`, which contains the bitwise XOR of **all pairings** of integers between `nums1` and `nums2` (every integer in `nums1` is paired with every integer in `nums2` **exactly once**).

Return _the **bitwise XOR** of all integers in_ `nums3`.

**Example 1:**

**Input:** nums1 = \[2,1,3\], nums2 = \[10,2,5,0\]
**Output:** 13
**Explanation:**
A possible nums3 array is \[8,0,7,2,11,3,4,1,9,1,6,3\].
The bitwise XOR of all these numbers is 13, so we return 13.

**Example 2:**

**Input:** nums1 = \[1,2\], nums2 = \[3,4\]
**Output:** 0
**Explanation:**
All possible pairs of bitwise XORs are nums1\[0\] ^ nums2\[0\], nums1\[0\] ^ nums2\[1\], nums1\[1\] ^ nums2\[0\],
and nums1\[1\] ^ nums2\[1\].
Thus, one possible nums3 array is \[2,5,1,6\].
2 ^ 5 ^ 1 ^ 6 = 0, so we return 0.

**Constraints:**

*   `1 <= nums1.length, nums2.length <= 105`
*   `0 <= nums1[i], nums2[j] <= 109`
Number of Pairs Satisfying Inequality

You are given two **0-indexed** integer arrays `nums1` and `nums2`, each of size `n`, and an integer `diff`. Find the number of **pairs** `(i, j)` such that:

*   `0 <= i < j <= n - 1` **and**
*   `nums1[i] - nums1[j] <= nums2[i] - nums2[j] + diff`.

Return _the **number of pairs** that satisfy the conditions._

**Example 1:**

**Input:** nums1 = \[3,2,5\], nums2 = \[2,2,1\], diff = 1
**Output:** 3
**Explanation:**
There are 3 pairs that satisfy the conditions:
1. i = 0, j = 1: 3 - 2 <= 2 - 2 + 1. Since i < j and 1 <= 1, this pair satisfies the conditions.
2. i = 0, j = 2: 3 - 5 <= 2 - 1 + 1. Since i < j and -2 <= 2, this pair satisfies the conditions.
3. i = 1, j = 2: 2 - 5 <= 2 - 1 + 1. Since i < j and -3 <= 2, this pair satisfies the conditions.
Therefore, we return 3.

**Example 2:**

**Input:** nums1 = \[3,-1\], nums2 = \[-2,2\], diff = -1
**Output:** 0
**Explanation:**
Since there does not exist any pair that satisfies the conditions, we return 0.

**Constraints:**

*   `n == nums1.length == nums2.length`
*   `2 <= n <= 105`
*   `-104 <= nums1[i], nums2[i] <= 104`
*   `-104 <= diff <= 104`
Number of Common Factors

Given two positive integers `a` and `b`, return _the number of **common** factors of_ `a` _and_ `b`.

An integer `x` is a **common factor** of `a` and `b` if `x` divides both `a` and `b`.

**Example 1:**

**Input:** a = 12, b = 6
**Output:** 4
**Explanation:** The common factors of 12 and 6 are 1, 2, 3, 6.

**Example 2:**

**Input:** a = 25, b = 30
**Output:** 2
**Explanation:** The common factors of 25 and 30 are 1, 5.

**Constraints:**

*   `1 <= a, b <= 1000`
Maximum Sum of an Hourglass

You are given an `m x n` integer matrix `grid`.

We define an **hourglass** as a part of the matrix with the following form:

Return _the **maximum** sum of the elements of an hourglass_.

**Note** that an hourglass cannot be rotated and must be entirely contained within the matrix.

**Example 1:**

**Input:** grid = \[\[6,2,1,3\],\[4,2,1,5\],\[9,2,8,7\],\[4,1,2,9\]\]
**Output:** 30
**Explanation:** The cells shown above represent the hourglass with the maximum sum: 6 + 2 + 1 + 2 + 9 + 2 + 8 = 30.

**Example 2:**

**Input:** grid = \[\[1,2,3\],\[4,5,6\],\[7,8,9\]\]
**Output:** 35
**Explanation:** There is only one hourglass in the matrix, with the sum: 1 + 2 + 3 + 5 + 7 + 8 + 9 = 35.

**Constraints:**

*   `m == grid.length`
*   `n == grid[i].length`
*   `3 <= m, n <= 150`
*   `0 <= grid[i][j] <= 106`
Minimize XOR

Given two positive integers `num1` and `num2`, find the positive integer `x` such that:

*   `x` has the same number of set bits as `num2`, and
*   The value `x XOR num1` is **minimal**.

Note that `XOR` is the bitwise XOR operation.

Return _the integer_ `x`. The test cases are generated such that `x` is **uniquely determined**.

The number of **set bits** of an integer is the number of `1`'s in its binary representation.

**Example 1:**

**Input:** num1 = 3, num2 = 5
**Output:** 3
**Explanation:**
The binary representations of num1 and num2 are 0011 and 0101, respectively.
The integer **3** has the same number of set bits as num2, and the value `3 XOR 3 = 0` is minimal.

**Example 2:**

**Input:** num1 = 1, num2 = 12
**Output:** 3
**Explanation:**
The binary representations of num1 and num2 are 0001 and 1100, respectively.
The integer **3** has the same number of set bits as num2, and the value `3 XOR 1 = 2` is minimal.

**Constraints:**

*   `1 <= num1, num2 <= 109`
Maximum Deletions on a String

You are given a string `s` consisting of only lowercase English letters. In one operation, you can:

*   Delete **the entire string** `s`, or
*   Delete the **first** `i` letters of `s` if the first `i` letters of `s` are **equal** to the following `i` letters in `s`, for any `i` in the range `1 <= i <= s.length / 2`.

For example, if `s = "ababc "`, then in one operation, you could delete the first two letters of `s` to get `"abc "`, since the first two letters of `s` and the following two letters of `s` are both equal to `"ab "`.

Return _the **maximum** number of operations needed to delete all of_ `s`.

**Example 1:**

**Input:** s =  "abcabcdabc "
**Output:** 2
**Explanation:**
- Delete the first 3 letters ( "abc ") since the next 3 letters are equal. Now, s =  "abcdabc ".
- Delete all the letters.
We used 2 operations so return 2. It can be proven that 2 is the maximum number of operations needed.
Note that in the second operation we cannot delete  "abc " again because the next occurrence of  "abc " does not happen in the next 3 letters.

**Example 2:**

**Input:** s =  "aaabaab "
**Output:** 4
**Explanation:**
- Delete the first letter ( "a ") since the next letter is equal. Now, s =  "aabaab ".
- Delete the first 3 letters ( "aab ") since the next 3 letters are equal. Now, s =  "aab ".
- Delete the first letter ( "a ") since the next letter is equal. Now, s =  "ab ".
- Delete all the letters.
We used 4 operations so return 4. It can be proven that 4 is the maximum number of operations needed.

**Example 3:**

**Input:** s =  "aaaaa "
**Output:** 5
**Explanation:** In each operation, we can delete the first letter of s.

**Constraints:**

*   `1 <= s.length <= 4000`
*   `s` consists only of lowercase English letters.
Maximize Total Tastiness of Purchased Fruits

You are given two non-negative integer arrays `price` and `tastiness`, both arrays have the same length `n`. You are also given two non-negative integers `maxAmount` and `maxCoupons`.

For every integer `i` in range `[0, n - 1]`:

*   `price[i]` describes the price of `ith` fruit.
*   `tastiness[i]` describes the tastiness of `ith` fruit.

You want to purchase some fruits such that total tastiness is maximized and the total price does not exceed `maxAmount`.

Additionally, you can use a coupon to purchase fruit for **half of its price** (rounded down to the closest integer). You can use at most `maxCoupons` of such coupons.

Return _the maximum total tastiness that can be purchased_.

**Note that:**

*   You can purchase each fruit at most once.
*   You can use coupons on some fruit at most once.

**Example 1:**

**Input:** price = \[10,20,20\], tastiness = \[5,8,8\], maxAmount = 20, maxCoupons = 1
**Output:** 13
**Explanation:** It is possible to make total tastiness 13 in following way:
- Buy first fruit without coupon, so that total price = 0 + 10 and total tastiness = 0 + 5.
- Buy second fruit with coupon, so that total price = 10 + 10 and total tastiness = 5 + 8.
- Do not buy third fruit, so that total price = 20 and total tastiness = 13.
It can be proven that 13 is the maximum total tastiness that can be obtained.

**Example 2:**

**Input:** price = \[10,15,7\], tastiness = \[5,8,20\], maxAmount = 10, maxCoupons = 2
**Output:** 28
**Explanation:** It is possible to make total tastiness 20 in following way:
- Do not buy first fruit, so that total price = 0 and total tastiness = 0.
- Buy second fruit with coupon, so that total price = 0 + 7 and total tastiness = 0 + 8.
- Buy third fruit with coupon, so that total price = 7 + 3 and total tastiness = 8 + 20.
It can be proven that 28 is the maximum total tastiness that can be obtained.

**Constraints:**

*   `n == price.length == tastiness.length`
*   `1 <= n <= 100`
*   `0 <= price[i], tastiness[i], maxAmount <= 1000`
*   `0 <= maxCoupons <= 5`
The Employee That Worked on the Longest Task

There are `n` employees, each with a unique id from `0` to `n - 1`.

You are given a 2D integer array `logs` where `logs[i] = [idi, leaveTimei]` where:

*   `idi` is the id of the employee that worked on the `ith` task, and
*   `leaveTimei` is the time at which the employee finished the `ith` task. All the values `leaveTimei` are **unique**.

Note that the `ith` task starts the moment right after the `(i - 1)th` task ends, and the `0th` task starts at time `0`.

Return _the id of the employee that worked the task with the longest time._ If there is a tie between two or more employees, return _the **smallest** id among them_.

**Example 1:**

**Input:** n = 10, logs = \[\[0,3\],\[2,5\],\[0,9\],\[1,15\]\]
**Output:** 1
**Explanation:** 
Task 0 started at 0 and ended at 3 with 3 units of times.
Task 1 started at 3 and ended at 5 with 2 units of times.
Task 2 started at 5 and ended at 9 with 4 units of times.
Task 3 started at 9 and ended at 15 with 6 units of times.
The task with the longest time is task 3 and the employee with id 1 is the one that worked on it, so we return 1.

**Example 2:**

**Input:** n = 26, logs = \[\[1,1\],\[3,7\],\[2,12\],\[7,17\]\]
**Output:** 3
**Explanation:** 
Task 0 started at 0 and ended at 1 with 1 unit of times.
Task 1 started at 1 and ended at 7 with 6 units of times.
Task 2 started at 7 and ended at 12 with 5 units of times.
Task 3 started at 12 and ended at 17 with 5 units of times.
The tasks with the longest time is task 1. The employees that worked on it is 3, so we return 3.

**Example 3:**

**Input:** n = 2, logs = \[\[0,10\],\[1,20\]\]
**Output:** 0
**Explanation:** 
Task 0 started at 0 and ended at 10 with 10 units of times.
Task 1 started at 10 and ended at 20 with 10 units of times.
The tasks with the longest time are tasks 0 and 1. The employees that worked on them are 0 and 1, so we return the smallest id 0.

**Constraints:**

*   `2 <= n <= 500`
*   `1 <= logs.length <= 500`
*   `logs[i].length == 2`
*   `0 <= idi <= n - 1`
*   `1 <= leaveTimei <= 500`
*   `idi != idi+1`
*   `leaveTimei` are sorted in a strictly increasing order.
Find The Original Array of Prefix Xor

You are given an **integer** array `pref` of size `n`. Find and return _the array_ `arr` _of size_ `n` _that satisfies_:

*   `pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]`.

Note that `^` denotes the **bitwise-xor** operation.

It can be proven that the answer is **unique**.

**Example 1:**

**Input:** pref = \[5,2,0,3,1\]
**Output:** \[5,7,2,3,2\]
**Explanation:** From the array \[5,7,2,3,2\] we have the following:
- pref\[0\] = 5.
- pref\[1\] = 5 ^ 7 = 2.
- pref\[2\] = 5 ^ 7 ^ 2 = 0.
- pref\[3\] = 5 ^ 7 ^ 2 ^ 3 = 3.
- pref\[4\] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1.

**Example 2:**

**Input:** pref = \[13\]
**Output:** \[13\]
**Explanation:** We have pref\[0\] = arr\[0\] = 13.

**Constraints:**

*   `1 <= pref.length <= 105`
*   `0 <= pref[i] <= 106`
Using a Robot to Print the Lexicographically Smallest String

You are given a string `s` and a robot that currently holds an empty string `t`. Apply one of the following operations until `s` and `t` **are both empty**:

*   Remove the **first** character of a string `s` and give it to the robot. The robot will append this character to the string `t`.
*   Remove the **last** character of a string `t` and give it to the robot. The robot will write this character on paper.

Return _the lexicographically smallest string that can be written on the paper._

**Example 1:**

**Input:** s =  "zza "
**Output:**  "azz "
**Explanation:** Let p denote the written string.
Initially p= " ", s= "zza ", t= " ".
Perform first operation three times p= " ", s= " ", t= "zza ".
Perform second operation three times p= "azz ", s= " ", t= " ".

**Example 2:**

**Input:** s =  "bac "
**Output:**  "abc "
**Explanation:** Let p denote the written string.
Perform first operation twice p= " ", s= "c ", t= "ba ". 
Perform second operation twice p= "ab ", s= "c ", t= " ". 
Perform first operation p= "ab ", s= " ", t= "c ". 
Perform second operation p= "abc ", s= " ", t= " ".

**Example 3:**

**Input:** s =  "bdda "
**Output:**  "addb "
**Explanation:** Let p denote the written string.
Initially p= " ", s= "bdda ", t= " ".
Perform first operation four times p= " ", s= " ", t= "bdda ".
Perform second operation four times p= "addb ", s= " ", t= " ".

**Constraints:**

*   `1 <= s.length <= 105`
*   `s` consists of only English lowercase letters.
Paths in Matrix Whose Sum Is Divisible by K

You are given a **0-indexed** `m x n` integer matrix `grid` and an integer `k`. You are currently at position `(0, 0)` and you want to reach position `(m - 1, n - 1)` moving only **down** or **right**.

Return _the number of paths where the sum of the elements on the path is divisible by_ `k`. Since the answer may be very large, return it **modulo** `109 + 7`.

**Example 1:**

**Input:** grid = \[\[5,2,4\],\[3,0,5\],\[0,7,2\]\], k = 3
**Output:** 2
**Explanation:** There are two paths where the sum of the elements on the path is divisible by k.
The first path highlighted in red has a sum of 5 + 2 + 4 + 5 + 2 = 18 which is divisible by 3.
The second path highlighted in blue has a sum of 5 + 3 + 0 + 5 + 2 = 15 which is divisible by 3.

**Example 2:**

**Input:** grid = \[\[0,0\]\], k = 5
**Output:** 1
**Explanation:** The path highlighted in red has a sum of 0 + 0 = 0 which is divisible by 5.

**Example 3:**

**Input:** grid = \[\[7,3,4,9\],\[2,3,6,2\],\[2,3,7,0\]\], k = 1
**Output:** 10
**Explanation:** Every integer is divisible by 1 so the sum of the elements on every possible path is divisible by k.

**Constraints:**

*   `m == grid.length`
*   `n == grid[i].length`
*   `1 <= m, n <= 5 * 104`
*   `1 <= m * n <= 5 * 104`
*   `0 <= grid[i][j] <= 100`
*   `1 <= k <= 50`
Minimum Split Into Subarrays With GCD Greater Than One

You are given an array `nums` consisting of positive integers.

Split the array into **one or more** disjoint subarrays such that:

*   Each element of the array belongs to **exactly one** subarray, and
*   The **GCD** of the elements of each subarray is strictly greater than `1`.

Return _the minimum number of subarrays that can be obtained after the split_.

**Note** that:

*   The **GCD** of a subarray is the largest positive integer that evenly divides all the elements of the subarray.
*   A **subarray** is a contiguous part of the array.

**Example 1:**

**Input:** nums = \[12,6,3,14,8\]
**Output:** 2
**Explanation:** We can split the array into the subarrays: \[12,6,3\] and \[14,8\].
- The GCD of 12, 6 and 3 is 3, which is strictly greater than 1.
- The GCD of 14 and 8 is 2, which is strictly greater than 1.
It can be shown that splitting the array into one subarray will make the GCD = 1.

**Example 2:**

**Input:** nums = \[4,12,6,14\]
**Output:** 1
**Explanation:** We can split the array into only one subarray, which is the whole array.

**Constraints:**

*   `1 <= nums.length <= 2000`
*   `2 <= nums[i] <= 109`
Number of Valid Clock Times

You are given a string of length `5` called `time`, representing the current time on a digital clock in the format `"hh:mm "`. The **earliest** possible time is `"00:00 "` and the **latest** possible time is `"23:59 "`.

In the string `time`, the digits represented by the `?` symbol are **unknown**, and must be **replaced** with a digit from `0` to `9`.

Return _an integer_ `answer`_, the number of valid clock times that can be created by replacing every_ `?` _with a digit from_ `0` _to_ `9`.

**Example 1:**

**Input:** time =  "?5:00 "
**Output:** 2
**Explanation:** We can replace the ? with either a 0 or 1, producing  "05:00 " or  "15:00 ". Note that we cannot replace it with a 2, since the time  "25:00 " is invalid. In total, we have two choices.

**Example 2:**

**Input:** time =  "0?:0? "
**Output:** 100
**Explanation:** Each ? can be replaced by any digit from 0 to 9, so we have 100 total choices.

**Example 3:**

**Input:** time =  "??:?? "
**Output:** 1440
**Explanation:** There are 24 possible choices for the hours, and 60 possible choices for the minutes. In total, we have 24 \* 60 = 1440 choices.

**Constraints:**

*   `time` is a valid string of length `5` in the format `"hh:mm "`.
*   `"00 " <= hh <= "23 "`
*   `"00 " <= mm <= "59 "`
*   Some of the digits might be replaced with `'?'` and need to be replaced with digits from `0` to `9`.
Range Product Queries of Powers

Given a positive integer `n`, there exists a **0-indexed** array called `powers`, composed of the **minimum** number of powers of `2` that sum to `n`. The array is sorted in **non-decreasing** order, and there is **only one** way to form the array.

You are also given a **0-indexed** 2D integer array `queries`, where `queries[i] = [lefti, righti]`. Each `queries[i]` represents a query where you have to find the product of all `powers[j]` with `lefti <= j <= righti`.

Return _an array_ `answers`_, equal in length to_ `queries`_, where_ `answers[i]` _is the answer to the_ `ith` _query_. Since the answer to the `ith` query may be too large, each `answers[i]` should be returned **modulo** `109 + 7`.

**Example 1:**

**Input:** n = 15, queries = \[\[0,1\],\[2,2\],\[0,3\]\]
**Output:** \[2,4,64\]
**Explanation:**
For n = 15, powers = \[1,2,4,8\]. It can be shown that powers cannot be a smaller size.
Answer to 1st query: powers\[0\] \* powers\[1\] = 1 \* 2 = 2.
Answer to 2nd query: powers\[2\] = 4.
Answer to 3rd query: powers\[0\] \* powers\[1\] \* powers\[2\] \* powers\[3\] = 1 \* 2 \* 4 \* 8 = 64.
Each answer modulo 109 + 7 yields the same answer, so \[2,4,64\] is returned.

**Example 2:**

**Input:** n = 2, queries = \[\[0,0\]\]
**Output:** \[2\]
**Explanation:**
For n = 2, powers = \[2\].
The answer to the only query is powers\[0\] = 2. The answer modulo 109 + 7 is the same, so \[2\] is returned.

**Constraints:**

*   `1 <= n <= 109`
*   `1 <= queries.length <= 105`
*   `0 <= starti <= endi < powers.length`
Minimize Maximum of Array

You are given a **0-indexed** array `nums` comprising of `n` non-negative integers.

In one operation, you must:

*   Choose an integer `i` such that `1 <= i < n` and `nums[i] > 0`.
*   Decrease `nums[i]` by 1.
*   Increase `nums[i - 1]` by 1.

Return _the **minimum** possible value of the **maximum** integer of_ `nums` _after performing **any** number of operations_.

**Example 1:**

**Input:** nums = \[3,7,1,6\]
**Output:** 5
**Explanation:**
One set of optimal operations is as follows:
1. Choose i = 1, and nums becomes \[4,6,1,6\].
2. Choose i = 3, and nums becomes \[4,6,2,5\].
3. Choose i = 1, and nums becomes \[5,5,2,5\].
The maximum integer of nums is 5. It can be shown that the maximum number cannot be less than 5.
Therefore, we return 5.

**Example 2:**

**Input:** nums = \[10,1\]
**Output:** 10
**Explanation:**
It is optimal to leave nums as is, and since 10 is the maximum value, we return 10.

**Constraints:**

*   `n == nums.length`
*   `2 <= n <= 105`
*   `0 <= nums[i] <= 109`
Create Components With Same Value

There is an undirected tree with `n` nodes labeled from `0` to `n - 1`.

You are given a **0-indexed** integer array `nums` of length `n` where `nums[i]` represents the value of the `ith` node. You are also given a 2D integer array `edges` of length `n - 1` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree.

You are allowed to **delete** some edges, splitting the tree into multiple connected components. Let the **value** of a component be the sum of **all** `nums[i]` for which node `i` is in the component.

Return _the **maximum** number of edges you can delete, such that every connected component in the tree has the same value._

**Example 1:**

**Input:** nums = \[6,2,2,2,6\], edges = \[\[0,1\],\[1,2\],\[1,3\],\[3,4\]\] 
**Output:** 2 
**Explanation:** The above figure shows how we can delete the edges \[0,1\] and \[3,4\]. The created components are nodes \[0\], \[1,2,3\] and \[4\]. The sum of the values in each component equals 6. It can be proven that no better deletion exists, so the answer is 2.

**Example 2:**

**Input:** nums = \[2\], edges = \[\]
**Output:** 0
**Explanation:** There are no edges to be deleted.

**Constraints:**

*   `1 <= n <= 2 * 104`
*   `nums.length == n`
*   `1 <= nums[i] <= 50`
*   `edges.length == n - 1`
*   `edges[i].length == 2`
*   `0 <= edges[i][0], edges[i][1] <= n - 1`
*   `edges` represents a valid tree.