bowang0911/leetcode huggingface dataset:
Remove All Ones With Row and Column Flips II

You are given a **0-indexed** `m x n` **binary** matrix `grid`.

In one operation, you can choose any `i` and `j` that meet the following conditions:

*   `0 <= i < m`
*   `0 <= j < n`
*   `grid[i][j] == 1`

and change the values of **all** cells in row `i` and column `j` to zero.

Return _the **minimum** number of operations needed to remove all_ `1`_'s from_ `grid`_._

**Example 1:**

**Input:** grid = \[\[1,1,1\],\[1,1,1\],\[0,1,0\]\]
**Output:** 2
**Explanation:**
In the first operation, change all cell values of row 1 and column 1 to zero.
In the second operation, change all cell values of row 0 and column 0 to zero.

**Example 2:**

**Input:** grid = \[\[0,1,0\],\[1,0,1\],\[0,1,0\]\]
**Output:** 2
**Explanation:**
In the first operation, change all cell values of row 1 and column 0 to zero.
In the second operation, change all cell values of row 2 and column 1 to zero.
Note that we cannot perform an operation using row 1 and column 1 because grid\[1\]\[1\] != 1.

**Example 3:**

**Input:** grid = \[\[0,0\],\[0,0\]\]
**Output:** 0
**Explanation:**
There are no 1's to remove so return 0.

**Constraints:**

*   `m == grid.length`
*   `n == grid[i].length`
*   `1 <= m, n <= 15`
*   `1 <= m * n <= 15`
*   `grid[i][j]` is either `0` or `1`.
Count Equal and Divisible Pairs in an Array

Given a **0-indexed** integer array `nums` of length `n` and an integer `k`, return _the **number of pairs**_ `(i, j)` _where_ `0 <= i < j < n`, _such that_ `nums[i] == nums[j]` _and_ `(i * j)` _is divisible by_ `k`.

**Example 1:**

**Input:** nums = \[3,1,2,2,2,1,3\], k = 2
**Output:** 4
**Explanation:**
There are 4 pairs that meet all the requirements:
- nums\[0\] == nums\[6\], and 0 \* 6 == 0, which is divisible by 2.
- nums\[2\] == nums\[3\], and 2 \* 3 == 6, which is divisible by 2.
- nums\[2\] == nums\[4\], and 2 \* 4 == 8, which is divisible by 2.
- nums\[3\] == nums\[4\], and 3 \* 4 == 12, which is divisible by 2.

**Example 2:**

**Input:** nums = \[1,2,3,4\], k = 1
**Output:** 0
**Explanation:** Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements.

**Constraints:**

*   `1 <= nums.length <= 100`
*   `1 <= nums[i], k <= 100`
Find Three Consecutive Integers That Sum to a Given Number

Given an integer `num`, return _three consecutive integers (as a sorted array)_ _that **sum** to_ `num`. If `num` cannot be expressed as the sum of three consecutive integers, return _an **empty** array._

**Example 1:**

**Input:** num = 33
**Output:** \[10,11,12\]
**Explanation:** 33 can be expressed as 10 + 11 + 12 = 33.
10, 11, 12 are 3 consecutive integers, so we return \[10, 11, 12\].

**Example 2:**

**Input:** num = 4
**Output:** \[\]
**Explanation:** There is no way to express 4 as the sum of 3 consecutive integers.

**Constraints:**

*   `0 <= num <= 1015`
Maximum Split of Positive Even Integers

You are given an integer `finalSum`. Split it into a sum of a **maximum** number of **unique** positive even integers.

*   For example, given `finalSum = 12`, the following splits are **valid** (unique positive even integers summing up to `finalSum`): `(12)`, `(2 + 10)`, `(2 + 4 + 6)`, and `(4 + 8)`. Among them, `(2 + 4 + 6)` contains the maximum number of integers. Note that `finalSum` cannot be split into `(2 + 2 + 4 + 4)` as all the numbers should be unique.

Return _a list of integers that represent a valid split containing a **maximum** number of integers_. If no valid split exists for `finalSum`, return _an **empty** list_. You may return the integers in **any** order.

**Example 1:**

**Input:** finalSum = 12
**Output:** \[2,4,6\]
**Explanation:** The following are valid splits: `(12)`, `(2 + 10)`, `(2 + 4 + 6)`, and `(4 + 8)`.
(2 + 4 + 6) has the maximum number of integers, which is 3. Thus, we return \[2,4,6\].
Note that \[2,6,4\], \[6,2,4\], etc. are also accepted.

**Example 2:**

**Input:** finalSum = 7
**Output:** \[\]
**Explanation:** There are no valid splits for the given finalSum.
Thus, we return an empty array.

**Example 3:**

**Input:** finalSum = 28
**Output:** \[6,8,2,12\]
**Explanation:** The following are valid splits: `(2 + 26)`, `(6 + 8 + 2 + 12)`, and `(4 + 24)`. 
`(6 + 8 + 2 + 12)` has the maximum number of integers, which is 4. Thus, we return \[6,8,2,12\].
Note that \[10,2,4,12\], \[6,2,4,16\], etc. are also accepted.

**Constraints:**

*   `1 <= finalSum <= 1010`
Count Good Triplets in an Array

You are given two **0-indexed** arrays `nums1` and `nums2` of length `n`, both of which are **permutations** of `[0, 1, ..., n - 1]`.

A **good triplet** is a set of `3` **distinct** values which are present in **increasing order** by position both in `nums1` and `nums2`. In other words, if we consider `pos1v` as the index of the value `v` in `nums1` and `pos2v` as the index of the value `v` in `nums2`, then a good triplet will be a set `(x, y, z)` where `0 <= x, y, z <= n - 1`, such that `pos1x < pos1y < pos1z` and `pos2x < pos2y < pos2z`.

Return _the **total number** of good triplets_.

**Example 1:**

**Input:** nums1 = \[2,0,1,3\], nums2 = \[0,1,2,3\]
**Output:** 1
**Explanation:** 
There are 4 triplets (x,y,z) such that pos1x < pos1y < pos1z. They are (2,0,1), (2,0,3), (2,1,3), and (0,1,3). 
Out of those triplets, only the triplet (0,1,3) satisfies pos2x < pos2y < pos2z. Hence, there is only 1 good triplet.

**Example 2:**

**Input:** nums1 = \[4,0,1,3,2\], nums2 = \[4,1,0,2,3\]
**Output:** 4
**Explanation:** The 4 good triplets are (4,0,3), (4,0,2), (4,1,3), and (4,1,2).

**Constraints:**

*   `n == nums1.length == nums2.length`
*   `3 <= n <= 105`
*   `0 <= nums1[i], nums2[i] <= n - 1`
*   `nums1` and `nums2` are permutations of `[0, 1, ..., n - 1]`.
Count Integers With Even Digit Sum

Given a positive integer `num`, return _the number of positive integers **less than or equal to**_ `num` _whose digit sums are **even**_.

The **digit sum** of a positive integer is the sum of all its digits.

**Example 1:**

**Input:** num = 4
**Output:** 2
**Explanation:**
The only integers less than or equal to 4 whose digit sums are even are 2 and 4.    

**Example 2:**

**Input:** num = 30
**Output:** 14
**Explanation:**
The 14 integers less than or equal to 30 whose digit sums are even are
2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, and 28.

**Constraints:**

*   `1 <= num <= 1000`
Merge Nodes in Between Zeros

You are given the `head` of a linked list, which contains a series of integers **separated** by `0`'s. The **beginning** and **end** of the linked list will have `Node.val == 0`.

For **every** two consecutive `0`'s, **merge** all the nodes lying in between them into a single node whose value is the **sum** of all the merged nodes. The modified list should not contain any `0`'s.

Return _the_ `head` _of the modified linked list_.

**Example 1:**

**Input:** head = \[0,3,1,0,4,5,2,0\]
**Output:** \[4,11\]
**Explanation:** 
The above figure represents the given linked list. The modified list contains
- The sum of the nodes marked in green: 3 + 1 = 4.
- The sum of the nodes marked in red: 4 + 5 + 2 = 11.

**Example 2:**

**Input:** head = \[0,1,0,3,0,2,2,0\]
**Output:** \[1,3,4\]
**Explanation:** 
The above figure represents the given linked list. The modified list contains
- The sum of the nodes marked in green: 1 = 1.
- The sum of the nodes marked in red: 3 = 3.
- The sum of the nodes marked in yellow: 2 + 2 = 4.

**Constraints:**

*   The number of nodes in the list is in the range `[3, 2 * 105]`.
*   `0 <= Node.val <= 1000`
*   There are **no** two consecutive nodes with `Node.val == 0`.
*   The **beginning** and **end** of the linked list have `Node.val == 0`.
Construct String With Repeat Limit

You are given a string `s` and an integer `repeatLimit`. Construct a new string `repeatLimitedString` using the characters of `s` such that no letter appears **more than** `repeatLimit` times **in a row**. You do **not** have to use all characters from `s`.

Return _the **lexicographically largest**_ `repeatLimitedString` _possible_.

A string `a` is **lexicographically larger** than a string `b` if in the first position where `a` and `b` differ, string `a` has a letter that appears later in the alphabet than the corresponding letter in `b`. If the first `min(a.length, b.length)` characters do not differ, then the longer string is the lexicographically larger one.

**Example 1:**

**Input:** s =  "cczazcc ", repeatLimit = 3
**Output:**  "zzcccac "
**Explanation:** We use all of the characters from s to construct the repeatLimitedString  "zzcccac ".
The letter 'a' appears at most 1 time in a row.
The letter 'c' appears at most 3 times in a row.
The letter 'z' appears at most 2 times in a row.
Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
The string is the lexicographically largest repeatLimitedString possible so we return  "zzcccac ".
Note that the string  "zzcccca " is lexicographically larger but the letter 'c' appears more than 3 times in a row, so it is not a valid repeatLimitedString.

**Example 2:**

**Input:** s =  "aababab ", repeatLimit = 2
**Output:**  "bbabaa "
**Explanation:** We use only some of the characters from s to construct the repeatLimitedString  "bbabaa ". 
The letter 'a' appears at most 2 times in a row.
The letter 'b' appears at most 2 times in a row.
Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
The string is the lexicographically largest repeatLimitedString possible so we return  "bbabaa ".
Note that the string  "bbabaaa " is lexicographically larger but the letter 'a' appears more than 2 times in a row, so it is not a valid repeatLimitedString.

**Constraints:**

*   `1 <= repeatLimit <= s.length <= 105`
*   `s` consists of lowercase English letters.
Count Array Pairs Divisible by K

Given a **0-indexed** integer array `nums` of length `n` and an integer `k`, return _the **number of pairs**_ `(i, j)` _such that:_

*   `0 <= i < j <= n - 1` _and_
*   `nums[i] * nums[j]` _is divisible by_ `k`.

**Example 1:**

**Input:** nums = \[1,2,3,4,5\], k = 2
**Output:** 7
**Explanation:** 
The 7 pairs of indices whose corresponding products are divisible by 2 are
(0, 1), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), and (3, 4).
Their products are 2, 4, 6, 8, 10, 12, and 20 respectively.
Other pairs such as (0, 2) and (2, 4) have products 3 and 15 respectively, which are not divisible by 2.    

**Example 2:**

**Input:** nums = \[1,2,3,4\], k = 5
**Output:** 0
**Explanation:** There does not exist any pair of indices whose corresponding product is divisible by 5.

**Constraints:**

*   `1 <= nums.length <= 105`
*   `1 <= nums[i], k <= 105`
Number of Ways to Build Sturdy Brick Wall

You are given integers `height` and `width` which specify the dimensions of a brick wall you are building. You are also given a **0-indexed** array of **unique** integers `bricks`, where the `ith` brick has a height of `1` and a width of `bricks[i]`. You have an **infinite** supply of each type of brick and bricks may **not** be rotated.

Each row in the wall must be exactly `width` units long. For the wall to be **sturdy**, adjacent rows in the wall should **not** join bricks at the same location, except at the ends of the wall.

Return _the number of ways to build a **sturdy** wall._ Since the answer may be very large, return it **modulo** `109 + 7`.

**Example 1:**

**Input:** height = 2, width = 3, bricks = \[1,2\]
**Output:** 2
**Explanation:**
The first two walls in the diagram show the only two ways to build a sturdy brick wall.
Note that the third wall in the diagram is not sturdy because adjacent rows join bricks 2 units from the left.

**Example 2:**

**Input:** height = 1, width = 1, bricks = \[5\]
**Output:** 0
**Explanation:**
There are no ways to build a sturdy wall because the only type of brick we have is longer than the width of the wall.

**Constraints:**

*   `1 <= height <= 100`
*   `1 <= width <= 10`
*   `1 <= bricks.length <= 10`
*   `1 <= bricks[i] <= 10`
*   All the values of `bricks` are **unique**.
Counting Words With a Given Prefix

You are given an array of strings `words` and a string `pref`.

Return _the number of strings in_ `words` _that contain_ `pref` _as a **prefix**_.

A **prefix** of a string `s` is any leading contiguous substring of `s`.

**Example 1:**

**Input:** words = \[ "pay ", "**at**tention ", "practice ", "**at**tend "\], `pref` \=  "at "
**Output:** 2
**Explanation:** The 2 strings that contain  "at " as a prefix are:  "**at**tention " and  "**at**tend ".

**Example 2:**

**Input:** words = \[ "leetcode ", "win ", "loops ", "success "\], `pref` \=  "code "
**Output:** 0
**Explanation:** There are no strings that contain  "code " as a prefix.

**Constraints:**

*   `1 <= words.length <= 100`
*   `1 <= words[i].length, pref.length <= 100`
*   `words[i]` and `pref` consist of lowercase English letters.
Minimum Number of Steps to Make Two Strings Anagram II

You are given two strings `s` and `t`. In one step, you can append **any character** to either `s` or `t`.

Return _the minimum number of steps to make_ `s` _and_ `t` _**anagrams** of each other._

An **anagram** of a string is a string that contains the same characters with a different (or the same) ordering.

**Example 1:**

**Input:** s =  "**lee**tco**de** ", t =  "co**a**t**s** "
**Output:** 7
**Explanation:** 
- In 2 steps, we can append the letters in  "as " onto s =  "leetcode ", forming s =  "leetcode**as** ".
- In 5 steps, we can append the letters in  "leede " onto t =  "coats ", forming t =  "coats**leede** ".
 "leetcodeas " and  "coatsleede " are now anagrams of each other.
We used a total of 2 + 5 = 7 steps.
It can be shown that there is no way to make them anagrams of each other with less than 7 steps.

**Example 2:**

**Input:** s =  "night ", t =  "thing "
**Output:** 0
**Explanation:** The given strings are already anagrams of each other. Thus, we do not need any further steps.

**Constraints:**

*   `1 <= s.length, t.length <= 2 * 105`
*   `s` and `t` consist of lowercase English letters.
Minimum Time to Complete Trips

You are given an array `time` where `time[i]` denotes the time taken by the `ith` bus to complete **one trip**.

Each bus can make multiple trips **successively**; that is, the next trip can start **immediately after** completing the current trip. Also, each bus operates **independently**; that is, the trips of one bus do not influence the trips of any other bus.

You are also given an integer `totalTrips`, which denotes the number of trips all buses should make **in total**. Return _the **minimum time** required for all buses to complete **at least**_ `totalTrips` _trips_.

**Example 1:**

**Input:** time = \[1,2,3\], totalTrips = 5
**Output:** 3
**Explanation:**
- At time t = 1, the number of trips completed by each bus are \[1,0,0\]. 
  The total number of trips completed is 1 + 0 + 0 = 1.
- At time t = 2, the number of trips completed by each bus are \[2,1,0\]. 
  The total number of trips completed is 2 + 1 + 0 = 3.
- At time t = 3, the number of trips completed by each bus are \[3,1,1\]. 
  The total number of trips completed is 3 + 1 + 1 = 5.
So the minimum time needed for all buses to complete at least 5 trips is 3.

**Example 2:**

**Input:** time = \[2\], totalTrips = 1
**Output:** 2
**Explanation:**
There is only one bus, and it will complete its first trip at t = 2.
So the minimum time needed to complete 1 trip is 2.

**Constraints:**

*   `1 <= time.length <= 105`
*   `1 <= time[i], totalTrips <= 107`
Minimum Time to Finish the Race

You are given a **0-indexed** 2D integer array `tires` where `tires[i] = [fi, ri]` indicates that the `ith` tire can finish its `xth` successive lap in `fi * ri(x-1)` seconds.

*   For example, if `fi = 3` and `ri = 2`, then the tire would finish its `1st` lap in `3` seconds, its `2nd` lap in `3 * 2 = 6` seconds, its `3rd` lap in `3 * 22 = 12` seconds, etc.

You are also given an integer `changeTime` and an integer `numLaps`.

The race consists of `numLaps` laps and you may start the race with **any** tire. You have an **unlimited** supply of each tire and after every lap, you may **change** to any given tire (including the current tire type) if you wait `changeTime` seconds.

Return _the **minimum** time to finish the race._

**Example 1:**

**Input:** tires = \[\[2,3\],\[3,4\]\], changeTime = 5, numLaps = 4
**Output:** 21
**Explanation:** 
Lap 1: Start with tire 0 and finish the lap in 2 seconds.
Lap 2: Continue with tire 0 and finish the lap in 2 \* 3 = 6 seconds.
Lap 3: Change tires to a new tire 0 for 5 seconds and then finish the lap in another 2 seconds.
Lap 4: Continue with tire 0 and finish the lap in 2 \* 3 = 6 seconds.
Total time = 2 + 6 + 5 + 2 + 6 = 21 seconds.
The minimum time to complete the race is 21 seconds.

**Example 2:**

**Input:** tires = \[\[1,10\],\[2,2\],\[3,4\]\], changeTime = 6, numLaps = 5
**Output:** 25
**Explanation:** 
Lap 1: Start with tire 1 and finish the lap in 2 seconds.
Lap 2: Continue with tire 1 and finish the lap in 2 \* 2 = 4 seconds.
Lap 3: Change tires to a new tire 1 for 6 seconds and then finish the lap in another 2 seconds.
Lap 4: Continue with tire 1 and finish the lap in 2 \* 2 = 4 seconds.
Lap 5: Change tires to tire 0 for 6 seconds then finish the lap in another 1 second.
Total time = 2 + 4 + 6 + 2 + 4 + 6 + 1 = 25 seconds.
The minimum time to complete the race is 25 seconds. 

**Constraints:**

*   `1 <= tires.length <= 105`
*   `tires[i].length == 2`
*   `1 <= fi, changeTime <= 105`
*   `2 <= ri <= 105`
*   `1 <= numLaps <= 1000`
Number of Ways to Build House of Cards

You are given an integer `n` representing the number of playing cards you have. A **house of cards** meets the following conditions:

*   A **house of cards** consists of one or more rows of **triangles** and horizontal cards.
*   **Triangles** are created by leaning two cards against each other.
*   One card must be placed horizontally between **all adjacent** triangles in a row.
*   Any triangle on a row higher than the first must be placed on a horizontal card from the previous row.
*   Each triangle is placed in the **leftmost** available spot in the row.

Return _the number of **distinct** **house of cards** you can build using **all**_ `n` _cards._ Two houses of cards are considered distinct if there exists a row where the two houses contain a different number of cards.

**Example 1:**

**Input:** n = 16
**Output:** 2
**Explanation:** The two valid houses of cards are shown.
The third house of cards in the diagram is not valid because the rightmost triangle on the top row is not placed on top of a horizontal card.

**Example 2:**

**Input:** n = 2
**Output:** 1
**Explanation:** The one valid house of cards is shown.

**Example 3:**

**Input:** n = 4
**Output:** 0
**Explanation:** The three houses of cards in the diagram are not valid.
The first house of cards needs a horizontal card placed between the two triangles.
The second house of cards uses 5 cards.
The third house of cards uses 2 cards.

**Constraints:**

*   `1 <= n <= 500`
Most Frequent Number Following Key In an Array

You are given a **0-indexed** integer array `nums`. You are also given an integer `key`, which is present in `nums`.

For every unique integer `target` in `nums`, **count** the number of times `target` immediately follows an occurrence of `key` in `nums`. In other words, count the number of indices `i` such that:

*   `0 <= i <= nums.length - 2`,
*   `nums[i] == key` and,
*   `nums[i + 1] == target`.

Return _the_ `target` _with the **maximum** count_. The test cases will be generated such that the `target` with maximum count is unique.

**Example 1:**

**Input:** nums = \[1,100,200,1,100\], key = 1
**Output:** 100
**Explanation:** For target = 100, there are 2 occurrences at indices 1 and 4 which follow an occurrence of key.
No other integers follow an occurrence of key, so we return 100.

**Example 2:**

**Input:** nums = \[2,2,2,2,3\], key = 2
**Output:** 2
**Explanation:** For target = 2, there are 3 occurrences at indices 1, 2, and 3 which follow an occurrence of key.
For target = 3, there is only one occurrence at index 4 which follows an occurrence of key.
target = 2 has the maximum number of occurrences following an occurrence of key, so we return 2.

**Constraints:**

*   `2 <= nums.length <= 1000`
*   `1 <= nums[i] <= 1000`
*   The test cases will be generated such that the answer is unique.
Sort the Jumbled Numbers

You are given a **0-indexed** integer array `mapping` which represents the mapping rule of a shuffled decimal system. `mapping[i] = j` means digit `i` should be mapped to digit `j` in this system.

The **mapped value** of an integer is the new integer obtained by replacing each occurrence of digit `i` in the integer with `mapping[i]` for all `0 <= i <= 9`.

You are also given another integer array `nums`. Return _the array_ `nums` _sorted in **non-decreasing** order based on the **mapped values** of its elements._

**Notes:**

*   Elements with the same mapped values should appear in the **same relative order** as in the input.
*   The elements of `nums` should only be sorted based on their mapped values and **not be replaced** by them.

**Example 1:**

**Input:** mapping = \[8,9,4,0,2,1,3,5,7,6\], nums = \[991,338,38\]
**Output:** \[338,38,991\]
**Explanation:** 
Map the number 991 as follows:
1. mapping\[9\] = 6, so all occurrences of the digit 9 will become 6.
2. mapping\[1\] = 9, so all occurrences of the digit 1 will become 9.
Therefore, the mapped value of 991 is 669.
338 maps to 007, or 7 after removing the leading zeros.
38 maps to 07, which is also 7 after removing leading zeros.
Since 338 and 38 share the same mapped value, they should remain in the same relative order, so 338 comes before 38.
Thus, the sorted array is \[338,38,991\].

**Example 2:**

**Input:** mapping = \[0,1,2,3,4,5,6,7,8,9\], nums = \[789,456,123\]
**Output:** \[123,456,789\]
**Explanation:** 789 maps to 789, 456 maps to 456, and 123 maps to 123. Thus, the sorted array is \[123,456,789\].

**Constraints:**

*   `mapping.length == 10`
*   `0 <= mapping[i] <= 9`
*   All the values of `mapping[i]` are **unique**.
*   `1 <= nums.length <= 3 * 104`
*   `0 <= nums[i] < 109`
All Ancestors of a Node in a Directed Acyclic Graph

You are given a positive integer `n` representing the number of nodes of a **Directed Acyclic Graph** (DAG). The nodes are numbered from `0` to `n - 1` (**inclusive**).

You are also given a 2D integer array `edges`, where `edges[i] = [fromi, toi]` denotes that there is a **unidirectional** edge from `fromi` to `toi` in the graph.

Return _a list_ `answer`_, where_ `answer[i]` _is the **list of ancestors** of the_ `ith` _node, sorted in **ascending order**_.

A node `u` is an **ancestor** of another node `v` if `u` can reach `v` via a set of edges.

**Example 1:**

**Input:** n = 8, edgeList = \[\[0,3\],\[0,4\],\[1,3\],\[2,4\],\[2,7\],\[3,5\],\[3,6\],\[3,7\],\[4,6\]\]
**Output:** \[\[\],\[\],\[\],\[0,1\],\[0,2\],\[0,1,3\],\[0,1,2,3,4\],\[0,1,2,3\]\]
**Explanation:**
The above diagram represents the input graph.
- Nodes 0, 1, and 2 do not have any ancestors.
- Node 3 has two ancestors 0 and 1.
- Node 4 has two ancestors 0 and 2.
- Node 5 has three ancestors 0, 1, and 3.
- Node 6 has five ancestors 0, 1, 2, 3, and 4.
- Node 7 has four ancestors 0, 1, 2, and 3.

**Example 2:**

**Input:** n = 5, edgeList = \[\[0,1\],\[0,2\],\[0,3\],\[0,4\],\[1,2\],\[1,3\],\[1,4\],\[2,3\],\[2,4\],\[3,4\]\]
**Output:** \[\[\],\[0\],\[0,1\],\[0,1,2\],\[0,1,2,3\]\]
**Explanation:**
The above diagram represents the input graph.
- Node 0 does not have any ancestor.
- Node 1 has one ancestor 0.
- Node 2 has two ancestors 0 and 1.
- Node 3 has three ancestors 0, 1, and 2.
- Node 4 has four ancestors 0, 1, 2, and 3.

**Constraints:**

*   `1 <= n <= 1000`
*   `0 <= edges.length <= min(2000, n * (n - 1) / 2)`
*   `edges[i].length == 2`
*   `0 <= fromi, toi <= n - 1`
*   `fromi != toi`
*   There are no duplicate edges.
*   The graph is **directed** and **acyclic**.