bowang0911/leetcode huggingface dataset:
Minimized Maximum of Products Distributed to Any Store

You are given an integer `n` indicating there are `n` specialty retail stores. There are `m` product types of varying amounts, which are given as a **0-indexed** integer array `quantities`, where `quantities[i]` represents the number of products of the `ith` product type.

You need to distribute **all products** to the retail stores following these rules:

*   A store can only be given **at most one product type** but can be given **any** amount of it.
*   After distribution, each store will have been given some number of products (possibly `0`). Let `x` represent the maximum number of products given to any store. You want `x` to be as small as possible, i.e., you want to **minimize** the **maximum** number of products that are given to any store.

Return _the minimum possible_ `x`.

**Example 1:**

**Input:** n = 6, quantities = \[11,6\]
**Output:** 3
**Explanation:** One optimal way is:
- The 11 products of type 0 are distributed to the first four stores in these amounts: 2, 3, 3, 3
- The 6 products of type 1 are distributed to the other two stores in these amounts: 3, 3
The maximum number of products given to any store is max(2, 3, 3, 3, 3, 3) = 3.

**Example 2:**

**Input:** n = 7, quantities = \[15,10,10\]
**Output:** 5
**Explanation:** One optimal way is:
- The 15 products of type 0 are distributed to the first three stores in these amounts: 5, 5, 5
- The 10 products of type 1 are distributed to the next two stores in these amounts: 5, 5
- The 10 products of type 2 are distributed to the last two stores in these amounts: 5, 5
The maximum number of products given to any store is max(5, 5, 5, 5, 5, 5, 5) = 5.

**Example 3:**

**Input:** n = 1, quantities = \[100000\]
**Output:** 100000
**Explanation:** The only optimal way is:
- The 100000 products of type 0 are distributed to the only store.
The maximum number of products given to any store is max(100000) = 100000.

**Constraints:**

*   `m == quantities.length`
*   `1 <= m <= n <= 105`
*   `1 <= quantities[i] <= 105`
Maximum Path Quality of a Graph

There is an **undirected** graph with `n` nodes numbered from `0` to `n - 1` (**inclusive**). You are given a **0-indexed** integer array `values` where `values[i]` is the **value** of the `ith` node. You are also given a **0-indexed** 2D integer array `edges`, where each `edges[j] = [uj, vj, timej]` indicates that there is an undirected edge between the nodes `uj` and `vj`, and it takes `timej` seconds to travel between the two nodes. Finally, you are given an integer `maxTime`.

A **valid** **path** in the graph is any path that starts at node `0`, ends at node `0`, and takes **at most** `maxTime` seconds to complete. You may visit the same node multiple times. The **quality** of a valid path is the **sum** of the values of the **unique nodes** visited in the path (each node's value is added **at most once** to the sum).

Return _the **maximum** quality of a valid path_.

**Note:** There are **at most four** edges connected to each node.

**Example 1:**

**Input:** values = \[0,32,10,43\], edges = \[\[0,1,10\],\[1,2,15\],\[0,3,10\]\], maxTime = 49
**Output:** 75
**Explanation:**
One possible path is 0 -> 1 -> 0 -> 3 -> 0. The total time taken is 10 + 10 + 10 + 10 = 40 <= 49.
The nodes visited are 0, 1, and 3, giving a maximal path quality of 0 + 32 + 43 = 75.

**Example 2:**

**Input:** values = \[5,10,15,20\], edges = \[\[0,1,10\],\[1,2,10\],\[0,3,10\]\], maxTime = 30
**Output:** 25
**Explanation:**
One possible path is 0 -> 3 -> 0. The total time taken is 10 + 10 = 20 <= 30.
The nodes visited are 0 and 3, giving a maximal path quality of 5 + 20 = 25.

**Example 3:**

**Input:** values = \[1,2,3,4\], edges = \[\[0,1,10\],\[1,2,11\],\[2,3,12\],\[1,3,13\]\], maxTime = 50
**Output:** 7
**Explanation:**
One possible path is 0 -> 1 -> 3 -> 1 -> 0. The total time taken is 10 + 13 + 13 + 10 = 46 <= 50.
The nodes visited are 0, 1, and 3, giving a maximal path quality of 1 + 2 + 4 = 7.

**Constraints:**

*   `n == values.length`
*   `1 <= n <= 1000`
*   `0 <= values[i] <= 108`
*   `0 <= edges.length <= 2000`
*   `edges[j].length == 3`
*   `0 <= uj < vj <= n - 1`
*   `10 <= timej, maxTime <= 100`
*   All the pairs `[uj, vj]` are **unique**.
*   There are **at most four** edges connected to each node.
*   The graph may not be connected.
Number of Equal Count Substrings

You are given a **0-indexed** string `s` consisting of only lowercase English letters, and an integer `count`. A **substring** of `s` is said to be an **equal count substring** if, for each **unique** letter in the substring, it appears exactly `count` times in the substring.

Return _the number of **equal count substrings** in_ `s`.

A **substring** is a contiguous non-empty sequence of characters within a string.

**Example 1:**

**Input:** s =  "aaabcbbcc ", count = 3
**Output:** 3
**Explanation:**
The substring that starts at index 0 and ends at index 2 is  "aaa ".
The letter 'a' in the substring appears exactly 3 times.
The substring that starts at index 3 and ends at index 8 is  "bcbbcc ".
The letters 'b' and 'c' in the substring appear exactly 3 times.
The substring that starts at index 0 and ends at index 8 is  "aaabcbbcc ".
The letters 'a', 'b', and 'c' in the substring appear exactly 3 times.

**Example 2:**

**Input:** s =  "abcd ", count = 2
**Output:** 0
**Explanation:**
The number of times each letter appears in s is less than count.
Therefore, no substrings in s are equal count substrings, so return 0.

**Example 3:**

**Input:** s =  "a ", count = 5
**Output:** 0
**Explanation:**
The number of times each letter appears in s is less than count.
Therefore, no substrings in s are equal count substrings, so return 0

**Constraints:**

*   `1 <= s.length <= 3 * 104`
*   `1 <= count <= 3 * 104`
*   `s` consists only of lowercase English letters.
Check Whether Two Strings are Almost Equivalent

Two strings `word1` and `word2` are considered **almost equivalent** if the differences between the frequencies of each letter from `'a'` to `'z'` between `word1` and `word2` is **at most** `3`.

Given two strings `word1` and `word2`, each of length `n`, return `true` _if_ `word1` _and_ `word2` _are **almost equivalent**, or_ `false` _otherwise_.

The **frequency** of a letter `x` is the number of times it occurs in the string.

**Example 1:**

**Input:** word1 =  "aaaa ", word2 =  "bccb "
**Output:** false
**Explanation:** There are 4 'a's in  "aaaa " but 0 'a's in  "bccb ".
The difference is 4, which is more than the allowed 3.

**Example 2:**

**Input:** word1 =  "abcdeef ", word2 =  "abaaacc "
**Output:** true
**Explanation:** The differences between the frequencies of each letter in word1 and word2 are at most 3:
- 'a' appears 1 time in word1 and 4 times in word2. The difference is 3.
- 'b' appears 1 time in word1 and 1 time in word2. The difference is 0.
- 'c' appears 1 time in word1 and 2 times in word2. The difference is 1.
- 'd' appears 1 time in word1 and 0 times in word2. The difference is 1.
- 'e' appears 2 times in word1 and 0 times in word2. The difference is 2.
- 'f' appears 1 time in word1 and 0 times in word2. The difference is 1.

**Example 3:**

**Input:** word1 =  "cccddabba ", word2 =  "babababab "
**Output:** true
**Explanation:** The differences between the frequencies of each letter in word1 and word2 are at most 3:
- 'a' appears 2 times in word1 and 4 times in word2. The difference is 2.
- 'b' appears 2 times in word1 and 5 times in word2. The difference is 3.
- 'c' appears 3 times in word1 and 0 times in word2. The difference is 3.
- 'd' appears 2 times in word1 and 0 times in word2. The difference is 2.

**Constraints:**

*   `n == word1.length == word2.length`
*   `1 <= n <= 100`
*   `word1` and `word2` consist only of lowercase English letters.
Walking Robot Simulation II

A `width x height` grid is on an XY-plane with the **bottom-left** cell at `(0, 0)` and the **top-right** cell at `(width - 1, height - 1)`. The grid is aligned with the four cardinal directions ( `"North "`, `"East "`, `"South "`, and `"West "`). A robot is **initially** at cell `(0, 0)` facing direction `"East "`.

The robot can be instructed to move for a specific number of **steps**. For each step, it does the following.

1.  Attempts to move **forward one** cell in the direction it is facing.
2.  If the cell the robot is **moving to** is **out of bounds**, the robot instead **turns** 90 degrees **counterclockwise** and retries the step.

After the robot finishes moving the number of steps required, it stops and awaits the next instruction.

Implement the `Robot` class:

*   `Robot(int width, int height)` Initializes the `width x height` grid with the robot at `(0, 0)` facing `"East "`.
*   `void step(int num)` Instructs the robot to move forward `num` steps.
*   `int[] getPos()` Returns the current cell the robot is at, as an array of length 2, `[x, y]`.
*   `String getDir()` Returns the current direction of the robot, `"North "`, `"East "`, `"South "`, or `"West "`.

**Example 1:**

**Input**
\[ "Robot ",  "step ",  "step ",  "getPos ",  "getDir ",  "step ",  "step ",  "step ",  "getPos ",  "getDir "\]
\[\[6, 3\], \[2\], \[2\], \[\], \[\], \[2\], \[1\], \[4\], \[\], \[\]\]
**Output**
\[null, null, null, \[4, 0\],  "East ", null, null, null, \[1, 2\],  "West "\]

**Explanation**
Robot robot = new Robot(6, 3); // Initialize the grid and the robot at (0, 0) facing East.
robot.step(2);  // It moves two steps East to (2, 0), and faces East.
robot.step(2);  // It moves two steps East to (4, 0), and faces East.
robot.getPos(); // return \[4, 0\]
robot.getDir(); // return  "East "
robot.step(2);  // It moves one step East to (5, 0), and faces East.
                // Moving the next step East would be out of bounds, so it turns and faces North.
                // Then, it moves one step North to (5, 1), and faces North.
robot.step(1);  // It moves one step North to (5, 2), and faces **North** (not West).
robot.step(4);  // Moving the next step North would be out of bounds, so it turns and faces West.
                // Then, it moves four steps West to (1, 2), and faces West.
robot.getPos(); // return \[1, 2\]
robot.getDir(); // return  "West "

**Constraints:**

*   `2 <= width, height <= 100`
*   `1 <= num <= 105`
*   At most `104` calls **in total** will be made to `step`, `getPos`, and `getDir`.
Most Beautiful Item for Each Query

You are given a 2D integer array `items` where `items[i] = [pricei, beautyi]` denotes the **price** and **beauty** of an item respectively.

You are also given a **0-indexed** integer array `queries`. For each `queries[j]`, you want to determine the **maximum beauty** of an item whose **price** is **less than or equal** to `queries[j]`. If no such item exists, then the answer to this query is `0`.

Return _an array_ `answer` _of the same length as_ `queries` _where_ `answer[j]` _is the answer to the_ `jth` _query_.

**Example 1:**

**Input:** items = \[\[1,2\],\[3,2\],\[2,4\],\[5,6\],\[3,5\]\], queries = \[1,2,3,4,5,6\]
**Output:** \[2,4,5,5,6,6\]
**Explanation:**
- For queries\[0\]=1, \[1,2\] is the only item which has price <= 1. Hence, the answer for this query is 2.
- For queries\[1\]=2, the items which can be considered are \[1,2\] and \[2,4\]. 
  The maximum beauty among them is 4.
- For queries\[2\]=3 and queries\[3\]=4, the items which can be considered are \[1,2\], \[3,2\], \[2,4\], and \[3,5\].
  The maximum beauty among them is 5.
- For queries\[4\]=5 and queries\[5\]=6, all items can be considered.
  Hence, the answer for them is the maximum beauty of all items, i.e., 6.

**Example 2:**

**Input:** items = \[\[1,2\],\[1,2\],\[1,3\],\[1,4\]\], queries = \[1\]
**Output:** \[4\]
**Explanation:** 
The price of every item is equal to 1, so we choose the item with the maximum beauty 4. 
Note that multiple items can have the same price and/or beauty.  

**Example 3:**

**Input:** items = \[\[10,1000\]\], queries = \[5\]
**Output:** \[0\]
**Explanation:**
No item has a price less than or equal to 5, so no item can be chosen.
Hence, the answer to the query is 0.

**Constraints:**

*   `1 <= items.length, queries.length <= 105`
*   `items[i].length == 2`
*   `1 <= pricei, beautyi, queries[j] <= 109`
Maximum Number of Tasks You Can Assign

You have `n` tasks and `m` workers. Each task has a strength requirement stored in a **0-indexed** integer array `tasks`, with the `ith` task requiring `tasks[i]` strength to complete. The strength of each worker is stored in a **0-indexed** integer array `workers`, with the `jth` worker having `workers[j]` strength. Each worker can only be assigned to a **single** task and must have a strength **greater than or equal** to the task's strength requirement (i.e., `workers[j] >= tasks[i]`).

Additionally, you have `pills` magical pills that will **increase a worker's strength** by `strength`. You can decide which workers receive the magical pills, however, you may only give each worker **at most one** magical pill.

Given the **0-indexed** integer arrays `tasks` and `workers` and the integers `pills` and `strength`, return _the **maximum** number of tasks that can be completed._

**Example 1:**

**Input:** tasks = \[**3**,**2**,**1**\], workers = \[**0**,**3**,**3**\], pills = 1, strength = 1
**Output:** 3
**Explanation:**
We can assign the magical pill and tasks as follows:
- Give the magical pill to worker 0.
- Assign worker 0 to task 2 (0 + 1 >= 1)
- Assign worker 1 to task 1 (3 >= 2)
- Assign worker 2 to task 0 (3 >= 3)

**Example 2:**

**Input:** tasks = \[**5**,4\], workers = \[**0**,0,0\], pills = 1, strength = 5
**Output:** 1
**Explanation:**
We can assign the magical pill and tasks as follows:
- Give the magical pill to worker 0.
- Assign worker 0 to task 0 (0 + 5 >= 5)

**Example 3:**

**Input:** tasks = \[**10**,**15**,30\], workers = \[**0**,**10**,10,10,10\], pills = 3, strength = 10
**Output:** 2
**Explanation:**
We can assign the magical pills and tasks as follows:
- Give the magical pill to worker 0 and worker 1.
- Assign worker 0 to task 0 (0 + 10 >= 10)
- Assign worker 1 to task 1 (10 + 10 >= 15)
The last pill is not given because it will not make any worker strong enough for the last task.

**Constraints:**

*   `n == tasks.length`
*   `m == workers.length`
*   `1 <= n, m <= 5 * 104`
*   `0 <= pills <= m`
*   `0 <= tasks[i], workers[j], strength <= 109`
Time Needed to Buy Tickets

There are `n` people in a line queuing to buy tickets, where the `0th` person is at the **front** of the line and the `(n - 1)th` person is at the **back** of the line.

You are given a **0-indexed** integer array `tickets` of length `n` where the number of tickets that the `ith` person would like to buy is `tickets[i]`.

Each person takes **exactly 1 second** to buy a ticket. A person can only buy **1 ticket at a time** and has to go back to **the end** of the line (which happens **instantaneously**) in order to buy more tickets. If a person does not have any tickets left to buy, the person will **leave** the line.

Return _the **time taken** for the person at position_ `k` **_(0-indexed)_** _to finish buying tickets_.

**Example 1:**

**Input:** tickets = \[2,3,2\], k = 2
**Output:** 6
**Explanation:** 
- In the first pass, everyone in the line buys a ticket and the line becomes \[1, 2, 1\].
- In the second pass, everyone in the line buys a ticket and the line becomes \[0, 1, 0\].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.

**Example 2:**

**Input:** tickets = \[5,1,1,1\], k = 0
**Output:** 8
**Explanation:**
- In the first pass, everyone in the line buys a ticket and the line becomes \[4, 0, 0, 0\].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.

**Constraints:**

*   `n == tickets.length`
*   `1 <= n <= 100`
*   `1 <= tickets[i] <= 100`
*   `0 <= k < n`
Reverse Nodes in Even Length Groups

You are given the `head` of a linked list.

The nodes in the linked list are **sequentially** assigned to **non-empty** groups whose lengths form the sequence of the natural numbers (`1, 2, 3, 4, ...`). The **length** of a group is the number of nodes assigned to it. In other words,

*   The `1st` node is assigned to the first group.
*   The `2nd` and the `3rd` nodes are assigned to the second group.
*   The `4th`, `5th`, and `6th` nodes are assigned to the third group, and so on.

Note that the length of the last group may be less than or equal to `1 + the length of the second to last group`.

**Reverse** the nodes in each group with an **even** length, and return _the_ `head` _of the modified linked list_.

**Example 1:**

**Input:** head = \[5,2,6,3,9,1,7,3,8,4\]
**Output:** \[5,6,2,3,9,1,4,8,3,7\]
**Explanation:**
- The length of the first group is 1, which is odd, hence no reversal occurs.
- The length of the second group is 2, which is even, hence the nodes are reversed.
- The length of the third group is 3, which is odd, hence no reversal occurs.
- The length of the last group is 4, which is even, hence the nodes are reversed.

**Example 2:**

**Input:** head = \[1,1,0,6\]
**Output:** \[1,0,1,6\]
**Explanation:**
- The length of the first group is 1. No reversal occurs.
- The length of the second group is 2. The nodes are reversed.
- The length of the last group is 1. No reversal occurs.

**Example 3:**

**Input:** head = \[1,1,0,6,5\]
**Output:** \[1,0,1,5,6\]
**Explanation:**
- The length of the first group is 1. No reversal occurs.
- The length of the second group is 2. The nodes are reversed.
- The length of the last group is 2. The nodes are reversed.

**Constraints:**

*   The number of nodes in the list is in the range `[1, 105]`.
*   `0 <= Node.val <= 105`
Decode the Slanted Ciphertext

A string `originalText` is encoded using a **slanted transposition cipher** to a string `encodedText` with the help of a matrix having a **fixed number of rows** `rows`.

`originalText` is placed first in a top-left to bottom-right manner.

The blue cells are filled first, followed by the red cells, then the yellow cells, and so on, until we reach the end of `originalText`. The arrow indicates the order in which the cells are filled. All empty cells are filled with `' '`. The number of columns is chosen such that the rightmost column will **not be empty** after filling in `originalText`.

`encodedText` is then formed by appending all characters of the matrix in a row-wise fashion.

The characters in the blue cells are appended first to `encodedText`, then the red cells, and so on, and finally the yellow cells. The arrow indicates the order in which the cells are accessed.

For example, if `originalText = "cipher "` and `rows = 3`, then we encode it in the following manner:

The blue arrows depict how `originalText` is placed in the matrix, and the red arrows denote the order in which `encodedText` is formed. In the above example, `encodedText = "ch ie pr "`.

Given the encoded string `encodedText` and number of rows `rows`, return _the original string_ `originalText`.

**Note:** `originalText` **does not** have any trailing spaces `' '`. The test cases are generated such that there is only one possible `originalText`.

**Example 1:**

**Input:** encodedText =  "ch   ie   pr ", rows = 3
**Output:**  "cipher "
**Explanation:** This is the same example described in the problem description.

**Example 2:**

**Input:** encodedText =  "iveo    eed   l te   olc ", rows = 4
**Output:**  "i love leetcode "
**Explanation:** The figure above denotes the matrix that was used to encode originalText. 
The blue arrows show how we can find originalText from encodedText.

**Example 3:**

**Input:** encodedText =  "coding ", rows = 1
**Output:**  "coding "
**Explanation:** Since there is only 1 row, both originalText and encodedText are the same.

**Constraints:**

*   `0 <= encodedText.length <= 106`
*   `encodedText` consists of lowercase English letters and `' '` only.
*   `encodedText` is a valid encoding of some `originalText` that **does not** have trailing spaces.
*   `1 <= rows <= 1000`
*   The testcases are generated such that there is **only one** possible `originalText`.
Process Restricted Friend Requests

You are given an integer `n` indicating the number of people in a network. Each person is labeled from `0` to `n - 1`.

You are also given a **0-indexed** 2D integer array `restrictions`, where `restrictions[i] = [xi, yi]` means that person `xi` and person `yi` **cannot** become **friends**, either **directly** or **indirectly** through other people.

Initially, no one is friends with each other. You are given a list of friend requests as a **0-indexed** 2D integer array `requests`, where `requests[j] = [uj, vj]` is a friend request between person `uj` and person `vj`.

A friend request is **successful** if `uj` and `vj` can be **friends**. Each friend request is processed in the given order (i.e., `requests[j]` occurs before `requests[j + 1]`), and upon a successful request, `uj` and `vj` **become direct friends** for all future friend requests.

Return _a **boolean array**_ `result`, _where each_ `result[j]` _is_ `true` _if the_ `jth` _friend request is **successful** or_ `false` _if it is not_.

**Note:** If `uj` and `vj` are already direct friends, the request is still **successful**.

**Example 1:**

**Input:** n = 3, restrictions = \[\[0,1\]\], requests = \[\[0,2\],\[2,1\]\]
**Output:** \[true,false\]
**Explanation:**
Request 0: Person 0 and person 2 can be friends, so they become direct friends. 
Request 1: Person 2 and person 1 cannot be friends since person 0 and person 1 would be indirect friends (1--2--0).

**Example 2:**

**Input:** n = 3, restrictions = \[\[0,1\]\], requests = \[\[1,2\],\[0,2\]\]
**Output:** \[true,false\]
**Explanation:**
Request 0: Person 1 and person 2 can be friends, so they become direct friends.
Request 1: Person 0 and person 2 cannot be friends since person 0 and person 1 would be indirect friends (0--2--1).

**Example 3:**

**Input:** n = 5, restrictions = \[\[0,1\],\[1,2\],\[2,3\]\], requests = \[\[0,4\],\[1,2\],\[3,1\],\[3,4\]\]
**Output:** \[true,false,true,false\]
**Explanation:**
Request 0: Person 0 and person 4 can be friends, so they become direct friends.
Request 1: Person 1 and person 2 cannot be friends since they are directly restricted.
Request 2: Person 3 and person 1 can be friends, so they become direct friends.
Request 3: Person 3 and person 4 cannot be friends since person 0 and person 1 would be indirect friends (0--4--3--1).

**Constraints:**

*   `2 <= n <= 1000`
*   `0 <= restrictions.length <= 1000`
*   `restrictions[i].length == 2`
*   `0 <= xi, yi <= n - 1`
*   `xi != yi`
*   `1 <= requests.length <= 1000`
*   `requests[j].length == 2`
*   `0 <= uj, vj <= n - 1`
*   `uj != vj`
Paths in Maze That Lead to Same Room

A maze consists of `n` rooms numbered from `1` to `n`, and some rooms are connected by corridors. You are given a 2D integer array `corridors` where `corridors[i] = [room1i, room2i]` indicates that there is a corridor connecting `room1i` and `room2i`, allowing a person in the maze to go from `room1i` to `room2i` **and vice versa**.

The designer of the maze wants to know how confusing the maze is. The **confusion** **score** of the maze is the number of different cycles of **length 3**.

*   For example, `1 -> 2 -> 3 -> 1` is a cycle of length 3, but `1 -> 2 -> 3 -> 4` and `1 -> 2 -> 3 -> 2 -> 1` are not.

Two cycles are considered to be **different** if one or more of the rooms visited in the first cycle is **not** in the second cycle.

Return _the_ _**confusion** **score** of the maze._

**Example 1:**

**Input:** n = 5, corridors = \[\[1,2\],\[5,2\],\[4,1\],\[2,4\],\[3,1\],\[3,4\]\]
**Output:** 2
**Explanation:**
One cycle of length 3 is 4 -> 1 -> 3 -> 4, denoted in red.
Note that this is the same cycle as 3 -> 4 -> 1 -> 3 or 1 -> 3 -> 4 -> 1 because the rooms are the same.
Another cycle of length 3 is 1 -> 2 -> 4 -> 1, denoted in blue.
Thus, there are two different cycles of length 3.

**Example 2:**

**Input:** n = 4, corridors = \[\[1,2\],\[3,4\]\]
**Output:** 0
**Explanation:**
There are no cycles of length 3.

**Constraints:**

*   `2 <= n <= 1000`
*   `1 <= corridors.length <= 5 * 104`
*   `corridors[i].length == 2`
*   `1 <= room1i, room2i <= n`
*   `room1i != room2i`
*   There are no duplicate corridors.