bowang0911/leetcode huggingface dataset:
Design a Number Container System

Design a number container system that can do the following:

*   **Insert** or **Replace** a number at the given index in the system.
*   **Return** the smallest index for the given number in the system.

Implement the `NumberContainers` class:

*   `NumberContainers()` Initializes the number container system.
*   `void change(int index, int number)` Fills the container at `index` with the `number`. If there is already a number at that `index`, replace it.
*   `int find(int number)` Returns the smallest index for the given `number`, or `-1` if there is no index that is filled by `number` in the system.

**Example 1:**

**Input**
\[ "NumberContainers ",  "find ",  "change ",  "change ",  "change ",  "change ",  "find ",  "change ",  "find "\]
\[\[\], \[10\], \[2, 10\], \[1, 10\], \[3, 10\], \[5, 10\], \[10\], \[1, 20\], \[10\]\]
**Output**
\[null, -1, null, null, null, null, 1, null, 2\]

**Explanation**
NumberContainers nc = new NumberContainers();
nc.find(10); // There is no index that is filled with number 10. Therefore, we return -1.
nc.change(2, 10); // Your container at index 2 will be filled with number 10.
nc.change(1, 10); // Your container at index 1 will be filled with number 10.
nc.change(3, 10); // Your container at index 3 will be filled with number 10.
nc.change(5, 10); // Your container at index 5 will be filled with number 10.
nc.find(10); // Number 10 is at the indices 1, 2, 3, and 5. Since the smallest index that is filled with 10 is 1, we return 1.
nc.change(1, 20); // Your container at index 1 will be filled with number 20. Note that index 1 was filled with 10 and then replaced with 20. 
nc.find(10); // Number 10 is at the indices 2, 3, and 5. The smallest index that is filled with 10 is 2. Therefore, we return 2.

**Constraints:**

*   `1 <= index, number <= 109`
*   At most `105` calls will be made **in total** to `change` and `find`.
Shortest Impossible Sequence of Rolls

You are given an integer array `rolls` of length `n` and an integer `k`. You roll a `k` sided dice numbered from `1` to `k`, `n` times, where the result of the `ith` roll is `rolls[i]`.

Return _the length of the **shortest** sequence of rolls that **cannot** be taken from_ `rolls`.

A **sequence of rolls** of length `len` is the result of rolling a `k` sided dice `len` times.

**Note** that the sequence taken does not have to be consecutive as long as it is in order.

**Example 1:**

**Input:** rolls = \[4,2,1,2,3,3,2,4,1\], k = 4
**Output:** 3
**Explanation:** Every sequence of rolls of length 1, \[1\], \[2\], \[3\], \[4\], can be taken from rolls.
Every sequence of rolls of length 2, \[1, 1\], \[1, 2\], ..., \[4, 4\], can be taken from rolls.
The sequence \[1, 4, 2\] cannot be taken from rolls, so we return 3.
Note that there are other sequences that cannot be taken from rolls.

**Example 2:**

**Input:** rolls = \[1,1,2,2\], k = 2
**Output:** 2
**Explanation:** Every sequence of rolls of length 1, \[1\], \[2\], can be taken from rolls.
The sequence \[2, 1\] cannot be taken from rolls, so we return 2.
Note that there are other sequences that cannot be taken from rolls but \[2, 1\] is the shortest.

**Example 3:**

**Input:** rolls = \[1,1,3,2,2,2,3,3\], k = 4
**Output:** 1
**Explanation:** The sequence \[4\] cannot be taken from rolls, so we return 1.
Note that there are other sequences that cannot be taken from rolls but \[4\] is the shortest.

**Constraints:**

*   `n == rolls.length`
*   `1 <= n <= 105`
*   `1 <= rolls[i] <= k <= 105`
First Letter to Appear Twice

Given a string `s` consisting of lowercase English letters, return _the first letter to appear **twice**_.

**Note**:

*   A letter `a` appears twice before another letter `b` if the **second** occurrence of `a` is before the **second** occurrence of `b`.
*   `s` will contain at least one letter that appears twice.

**Example 1:**

**Input:** s =  "abccbaacz "
**Output:**  "c "
**Explanation:**
The letter 'a' appears on the indexes 0, 5 and 6.
The letter 'b' appears on the indexes 1 and 4.
The letter 'c' appears on the indexes 2, 3 and 7.
The letter 'z' appears on the index 8.
The letter 'c' is the first letter to appear twice, because out of all the letters the index of its second occurrence is the smallest.

**Example 2:**

**Input:** s =  "abcdd "
**Output:**  "d "
**Explanation:**
The only letter that appears twice is 'd' so we return 'd'.

**Constraints:**

*   `2 <= s.length <= 100`
*   `s` consists of lowercase English letters.
*   `s` has at least one repeated letter.
Equal Row and Column Pairs

Given a **0-indexed** `n x n` integer matrix `grid`, _return the number of pairs_ `(ri, cj)` _such that row_ `ri` _and column_ `cj` _are equal_.

A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array).

**Example 1:**

**Input:** grid = \[\[3,2,1\],\[1,7,6\],\[2,7,7\]\]
**Output:** 1
**Explanation:** There is 1 equal row and column pair:
- (Row 2, Column 1): \[2,7,7\]

**Example 2:**

**Input:** grid = \[\[3,1,2,2\],\[1,4,4,5\],\[2,4,2,2\],\[2,4,2,2\]\]
**Output:** 3
**Explanation:** There are 3 equal row and column pairs:
- (Row 0, Column 0): \[3,1,2,2\]
- (Row 2, Column 2): \[2,4,2,2\]
- (Row 3, Column 2): \[2,4,2,2\]

**Constraints:**

*   `n == grid.length == grid[i].length`
*   `1 <= n <= 200`
*   `1 <= grid[i][j] <= 105`
Number of Excellent Pairs

You are given a **0-indexed** positive integer array `nums` and a positive integer `k`.

A pair of numbers `(num1, num2)` is called **excellent** if the following conditions are satisfied:

*   **Both** the numbers `num1` and `num2` exist in the array `nums`.
*   The sum of the number of set bits in `num1 OR num2` and `num1 AND num2` is greater than or equal to `k`, where `OR` is the bitwise **OR** operation and `AND` is the bitwise **AND** operation.

Return _the number of **distinct** excellent pairs_.

Two pairs `(a, b)` and `(c, d)` are considered distinct if either `a != c` or `b != d`. For example, `(1, 2)` and `(2, 1)` are distinct.

**Note** that a pair `(num1, num2)` such that `num1 == num2` can also be excellent if you have at least **one** occurrence of `num1` in the array.

**Example 1:**

**Input:** nums = \[1,2,3,1\], k = 3
**Output:** 5
**Explanation:** The excellent pairs are the following:
- (3, 3). (3 AND 3) and (3 OR 3) are both equal to (11) in binary. The total number of set bits is 2 + 2 = 4, which is greater than or equal to k = 3.
- (2, 3) and (3, 2). (2 AND 3) is equal to (10) in binary, and (2 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
- (1, 3) and (3, 1). (1 AND 3) is equal to (01) in binary, and (1 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
So the number of excellent pairs is 5.

**Example 2:**

**Input:** nums = \[5,1,1\], k = 10
**Output:** 0
**Explanation:** There are no excellent pairs for this array.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `1 <= nums[i] <= 109`
*   `1 <= k <= 60`
Maximum Number of Books You Can Take

You are given a **0-indexed** integer array `books` of length `n` where `books[i]` denotes the number of books on the `ith` shelf of a bookshelf.

You are going to take books from a **contiguous** section of the bookshelf spanning from `l` to `r` where `0 <= l <= r < n`. For each index `i` in the range `l <= i < r`, you must take **strictly fewer** books from shelf `i` than shelf `i + 1`.

Return _the **maximum** number of books you can take from the bookshelf._

**Example 1:**

**Input:** books = \[8,5,2,7,9\]
**Output:** 19
**Explanation:**
- Take 1 book from shelf 1.
- Take 2 books from shelf 2.
- Take 7 books from shelf 3.
- Take 9 books from shelf 4.
You have taken 19 books, so return 19.
It can be proven that 19 is the maximum number of books you can take.

**Example 2:**

**Input:** books = \[7,0,3,4,5\]
**Output:** 12
**Explanation:**
- Take 3 books from shelf 2.
- Take 4 books from shelf 3.
- Take 5 books from shelf 4.
You have taken 12 books so return 12.
It can be proven that 12 is the maximum number of books you can take.

**Example 3:**

**Input:** books = \[8,2,3,7,3,4,0,1,4,3\]
**Output:** 13
**Explanation:**
- Take 1 book from shelf 0.
- Take 2 books from shelf 1.
- Take 3 books from shelf 2.
- Take 7 books from shelf 3.
You have taken 13 books so return 13.
It can be proven that 13 is the maximum number of books you can take.

**Constraints:**

*   `1 <= books.length <= 105`
*   `0 <= books[i] <= 105`
Make Array Zero by Subtracting Equal Amounts

You are given a non-negative integer array `nums`. In one operation, you must:

*   Choose a positive integer `x` such that `x` is less than or equal to the **smallest non-zero** element in `nums`.
*   Subtract `x` from every **positive** element in `nums`.

Return _the **minimum** number of operations to make every element in_ `nums` _equal to_ `0`.

**Example 1:**

**Input:** nums = \[1,5,0,3,5\]
**Output:** 3
**Explanation:**
In the first operation, choose x = 1. Now, nums = \[0,4,0,2,4\].
In the second operation, choose x = 2. Now, nums = \[0,2,0,0,2\].
In the third operation, choose x = 2. Now, nums = \[0,0,0,0,0\].

**Example 2:**

**Input:** nums = \[0\]
**Output:** 0
**Explanation:** Each element in nums is already 0 so no operations are needed.

**Constraints:**

*   `1 <= nums.length <= 100`
*   `0 <= nums[i] <= 100`
Maximum Number of Groups Entering a Competition

You are given a positive integer array `grades` which represents the grades of students in a university. You would like to enter **all** these students into a competition in **ordered** non-empty groups, such that the ordering meets the following conditions:

*   The sum of the grades of students in the `ith` group is **less than** the sum of the grades of students in the `(i + 1)th` group, for all groups (except the last).
*   The total number of students in the `ith` group is **less than** the total number of students in the `(i + 1)th` group, for all groups (except the last).

Return _the **maximum** number of groups that can be formed_.

**Example 1:**

**Input:** grades = \[10,6,12,7,3,5\]
**Output:** 3
**Explanation:** The following is a possible way to form 3 groups of students:
- 1st group has the students with grades = \[12\]. Sum of grades: 12. Student count: 1
- 2nd group has the students with grades = \[6,7\]. Sum of grades: 6 + 7 = 13. Student count: 2
- 3rd group has the students with grades = \[10,3,5\]. Sum of grades: 10 + 3 + 5 = 18. Student count: 3
It can be shown that it is not possible to form more than 3 groups.

**Example 2:**

**Input:** grades = \[8,8\]
**Output:** 1
**Explanation:** We can only form 1 group, since forming 2 groups would lead to an equal number of students in both groups.

**Constraints:**

*   `1 <= grades.length <= 105`
*   `1 <= grades[i] <= 105`
Find Closest Node to Given Two Nodes

You are given a **directed** graph of `n` nodes numbered from `0` to `n - 1`, where each node has **at most one** outgoing edge.

The graph is represented with a given **0-indexed** array `edges` of size `n`, indicating that there is a directed edge from node `i` to node `edges[i]`. If there is no outgoing edge from `i`, then `edges[i] == -1`.

You are also given two integers `node1` and `node2`.

Return _the **index** of the node that can be reached from both_ `node1` _and_ `node2`_, such that the **maximum** between the distance from_ `node1` _to that node, and from_ `node2` _to that node is **minimized**_. If there are multiple answers, return the node with the **smallest** index, and if no possible answer exists, return `-1`.

Note that `edges` may contain cycles.

**Example 1:**

**Input:** edges = \[2,2,3,-1\], node1 = 0, node2 = 1
**Output:** 2
**Explanation:** The distance from node 0 to node 2 is 1, and the distance from node 1 to node 2 is 1.
The maximum of those two distances is 1. It can be proven that we cannot get a node with a smaller maximum distance than 1, so we return node 2.

**Example 2:**

**Input:** edges = \[1,2,-1\], node1 = 0, node2 = 2
**Output:** 2
**Explanation:** The distance from node 0 to node 2 is 2, and the distance from node 2 to itself is 0.
The maximum of those two distances is 2. It can be proven that we cannot get a node with a smaller maximum distance than 2, so we return node 2.

**Constraints:**

*   `n == edges.length`
*   `2 <= n <= 105`
*   `-1 <= edges[i] < n`
*   `edges[i] != i`
*   `0 <= node1, node2 < n`
Longest Cycle in a Graph

You are given a **directed** graph of `n` nodes numbered from `0` to `n - 1`, where each node has **at most one** outgoing edge.

The graph is represented with a given **0-indexed** array `edges` of size `n`, indicating that there is a directed edge from node `i` to node `edges[i]`. If there is no outgoing edge from node `i`, then `edges[i] == -1`.

Return _the length of the **longest** cycle in the graph_. If no cycle exists, return `-1`.

A cycle is a path that starts and ends at the **same** node.

**Example 1:**

**Input:** edges = \[3,3,4,2,3\]
**Output:** 3
**Explanation:** The longest cycle in the graph is the cycle: 2 -> 4 -> 3 -> 2.
The length of this cycle is 3, so 3 is returned.

**Example 2:**

**Input:** edges = \[2,-1,3,1\]
**Output:** -1
**Explanation:** There are no cycles in this graph.

**Constraints:**

*   `n == edges.length`
*   `2 <= n <= 105`
*   `-1 <= edges[i] < n`
*   `edges[i] != i`
Minimum Costs Using the Train Line

A train line going through a city has two routes, the regular route and the express route. Both routes go through the **same** `n + 1` stops labeled from `0` to `n`. Initially, you start on the regular route at stop `0`.

You are given two **1-indexed** integer arrays `regular` and `express`, both of length `n`. `regular[i]` describes the cost it takes to go from stop `i - 1` to stop `i` using the regular route, and `express[i]` describes the cost it takes to go from stop `i - 1` to stop `i` using the express route.

You are also given an integer `expressCost` which represents the cost to transfer from the regular route to the express route.

Note that:

*   There is no cost to transfer from the express route back to the regular route.
*   You pay `expressCost` **every** time you transfer from the regular route to the express route.
*   There is no extra cost to stay on the express route.

Return _a **1-indexed** array_ `costs` _of length_ `n`_, where_ `costs[i]` _is the **minimum** cost to reach stop_ `i` _from stop_ `0`.

Note that a stop can be counted as **reached** from either route.

**Example 1:**

**Input:** regular = \[1,6,9,5\], express = \[5,2,3,10\], expressCost = 8
**Output:** \[1,7,14,19\]
**Explanation:** The diagram above shows how to reach stop 4 from stop 0 with minimum cost.
- Take the regular route from stop 0 to stop 1, costing 1.
- Take the express route from stop 1 to stop 2, costing 8 + 2 = 10.
- Take the express route from stop 2 to stop 3, costing 3.
- Take the regular route from stop 3 to stop 4, costing 5.
The total cost is 1 + 10 + 3 + 5 = 19.
Note that a different route could be taken to reach the other stops with minimum cost.

**Example 2:**

**Input:** regular = \[11,5,13\], express = \[7,10,6\], expressCost = 3
**Output:** \[10,15,24\]
**Explanation:** The diagram above shows how to reach stop 3 from stop 0 with minimum cost.
- Take the express route from stop 0 to stop 1, costing 3 + 7 = 10.
- Take the regular route from stop 1 to stop 2, costing 5.
- Take the express route from stop 2 to stop 3, costing 3 + 6 = 9.
The total cost is 10 + 5 + 9 = 24.
Note that the expressCost is paid again to transfer back to the express route.

**Constraints:**

*   `n == regular.length == express.length`
*   `1 <= n <= 105`
*   `1 <= regular[i], express[i], expressCost <= 105`
Merge Similar Items

You are given two 2D integer arrays, `items1` and `items2`, representing two sets of items. Each array `items` has the following properties:

*   `items[i] = [valuei, weighti]` where `valuei` represents the **value** and `weighti` represents the **weight** of the `ith` item.
*   The value of each item in `items` is **unique**.

Return _a 2D integer array_ `ret` _where_ `ret[i] = [valuei, weighti]`_,_ _with_ `weighti` _being the **sum of weights** of all items with value_ `valuei`.

**Note:** `ret` should be returned in **ascending** order by value.

**Example 1:**

**Input:** items1 = \[\[1,1\],\[4,5\],\[3,8\]\], items2 = \[\[3,1\],\[1,5\]\]
**Output:** \[\[1,6\],\[3,9\],\[4,5\]\]
**Explanation:** 
The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 5, total weight = 1 + 5 = 6.
The item with value = 3 occurs in items1 with weight = 8 and in items2 with weight = 1, total weight = 8 + 1 = 9.
The item with value = 4 occurs in items1 with weight = 5, total weight = 5.  
Therefore, we return \[\[1,6\],\[3,9\],\[4,5\]\].

**Example 2:**

**Input:** items1 = \[\[1,1\],\[3,2\],\[2,3\]\], items2 = \[\[2,1\],\[3,2\],\[1,3\]\]
**Output:** \[\[1,4\],\[2,4\],\[3,4\]\]
**Explanation:** 
The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 3, total weight = 1 + 3 = 4.
The item with value = 2 occurs in items1 with weight = 3 and in items2 with weight = 1, total weight = 3 + 1 = 4.
The item with value = 3 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4.
Therefore, we return \[\[1,4\],\[2,4\],\[3,4\]\].

**Example 3:**

**Input:** items1 = \[\[1,3\],\[2,2\]\], items2 = \[\[7,1\],\[2,2\],\[1,4\]\]
**Output:** \[\[1,7\],\[2,4\],\[7,1\]\]
**Explanation:**
The item with value = 1 occurs in items1 with weight = 3 and in items2 with weight = 4, total weight = 3 + 4 = 7. 
The item with value = 2 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4. 
The item with value = 7 occurs in items2 with weight = 1, total weight = 1.
Therefore, we return \[\[1,7\],\[2,4\],\[7,1\]\].

**Constraints:**

*   `1 <= items1.length, items2.length <= 1000`
*   `items1[i].length == items2[i].length == 2`
*   `1 <= valuei, weighti <= 1000`
*   Each `valuei` in `items1` is **unique**.
*   Each `valuei` in `items2` is **unique**.
Count Number of Bad Pairs

You are given a **0-indexed** integer array `nums`. A pair of indices `(i, j)` is a **bad pair** if `i < j` and `j - i != nums[j] - nums[i]`.

Return _the total number of **bad pairs** in_ `nums`.

**Example 1:**

**Input:** nums = \[4,1,3,3\]
**Output:** 5
**Explanation:** The pair (0, 1) is a bad pair since 1 - 0 != 1 - 4.
The pair (0, 2) is a bad pair since 2 - 0 != 3 - 4, 2 != -1.
The pair (0, 3) is a bad pair since 3 - 0 != 3 - 4, 3 != -1.
The pair (1, 2) is a bad pair since 2 - 1 != 3 - 1, 1 != 2.
The pair (2, 3) is a bad pair since 3 - 2 != 3 - 3, 1 != 0.
There are a total of 5 bad pairs, so we return 5.

**Example 2:**

**Input:** nums = \[1,2,3,4,5\]
**Output:** 0
**Explanation:** There are no bad pairs.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `1 <= nums[i] <= 109`
Task Scheduler II

You are given a **0-indexed** array of positive integers `tasks`, representing tasks that need to be completed **in order**, where `tasks[i]` represents the **type** of the `ith` task.

You are also given a positive integer `space`, which represents the **minimum** number of days that must pass **after** the completion of a task before another task of the **same** type can be performed.

Each day, until all tasks have been completed, you must either:

*   Complete the next task from `tasks`, or
*   Take a break.

Return _the **minimum** number of days needed to complete all tasks_.

**Example 1:**

**Input:** tasks = \[1,2,1,2,3,1\], space = 3
**Output:** 9
**Explanation:**
One way to complete all tasks in 9 days is as follows:
Day 1: Complete the 0th task.
Day 2: Complete the 1st task.
Day 3: Take a break.
Day 4: Take a break.
Day 5: Complete the 2nd task.
Day 6: Complete the 3rd task.
Day 7: Take a break.
Day 8: Complete the 4th task.
Day 9: Complete the 5th task.
It can be shown that the tasks cannot be completed in less than 9 days.

**Example 2:**

**Input:** tasks = \[5,8,8,5\], space = 2
**Output:** 6
**Explanation:**
One way to complete all tasks in 6 days is as follows:
Day 1: Complete the 0th task.
Day 2: Complete the 1st task.
Day 3: Take a break.
Day 4: Take a break.
Day 5: Complete the 2nd task.
Day 6: Complete the 3rd task.
It can be shown that the tasks cannot be completed in less than 6 days.

**Constraints:**

*   `1 <= tasks.length <= 105`
*   `1 <= tasks[i] <= 109`
*   `1 <= space <= tasks.length`
Minimum Replacements to Sort the Array

You are given a **0-indexed** integer array `nums`. In one operation you can replace any element of the array with **any two** elements that **sum** to it.

*   For example, consider `nums = [5,6,7]`. In one operation, we can replace `nums[1]` with `2` and `4` and convert `nums` to `[5,2,4,7]`.

Return _the minimum number of operations to make an array that is sorted in **non-decreasing** order_.

**Example 1:**

**Input:** nums = \[3,9,3\]
**Output:** 2
**Explanation:** Here are the steps to sort the array in non-decreasing order:
- From \[3,9,3\], replace the 9 with 3 and 6 so the array becomes \[3,3,6,3\]
- From \[3,3,6,3\], replace the 6 with 3 and 3 so the array becomes \[3,3,3,3,3\]
There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.

**Example 2:**

**Input:** nums = \[1,2,3,4,5\]
**Output:** 0
**Explanation:** The array is already in non-decreasing order. Therefore, we return 0. 

**Constraints:**

*   `1 <= nums.length <= 105`
*   `1 <= nums[i] <= 109`
Number of Arithmetic Triplets

You are given a **0-indexed**, **strictly increasing** integer array `nums` and a positive integer `diff`. A triplet `(i, j, k)` is an **arithmetic triplet** if the following conditions are met:

*   `i < j < k`,
*   `nums[j] - nums[i] == diff`, and
*   `nums[k] - nums[j] == diff`.

Return _the number of unique **arithmetic triplets**._

**Example 1:**

**Input:** nums = \[0,1,4,6,7,10\], diff = 3
**Output:** 2
**Explanation:**
(1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3.
(2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3. 

**Example 2:**

**Input:** nums = \[4,5,6,7,8,9\], diff = 2
**Output:** 2
**Explanation:**
(0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2.
(1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2.

**Constraints:**

*   `3 <= nums.length <= 200`
*   `0 <= nums[i] <= 200`
*   `1 <= diff <= 50`
*   `nums` is **strictly** increasing.
Reachable Nodes With Restrictions

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

You are 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 also given an integer array `restricted` which represents **restricted** nodes.

Return _the **maximum** number of nodes you can reach from node_ `0` _without visiting a restricted node._

Note that node `0` will **not** be a restricted node.

**Example 1:**

**Input:** n = 7, edges = \[\[0,1\],\[1,2\],\[3,1\],\[4,0\],\[0,5\],\[5,6\]\], restricted = \[4,5\]
**Output:** 4
**Explanation:** The diagram above shows the tree.
We have that \[0,1,2,3\] are the only nodes that can be reached from node 0 without visiting a restricted node.

**Example 2:**

**Input:** n = 7, edges = \[\[0,1\],\[0,2\],\[0,5\],\[0,4\],\[3,2\],\[6,5\]\], restricted = \[4,2,1\]
**Output:** 3
**Explanation:** The diagram above shows the tree.
We have that \[0,5,6\] are the only nodes that can be reached from node 0 without visiting a restricted node.

**Constraints:**

*   `2 <= n <= 105`
*   `edges.length == n - 1`
*   `edges[i].length == 2`
*   `0 <= ai, bi < n`
*   `ai != bi`
*   `edges` represents a valid tree.
*   `1 <= restricted.length < n`
*   `1 <= restricted[i] < n`
*   All the values of `restricted` are **unique**.