1.57K subscribers
578 photos
1 file
950 links
Don't miss a day to solve the problem
My leetcode graph - https://leetcode.com/SamoylenkoDmitry/
Download Telegram
Problem URL: [Number of Ways to Stay in the Same Place After Some Steps](https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/?envType=daily-question&envId=2023-10-15)

Problem Description:
You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).

Given two integers steps and arrLen, return the number of ways such that your pointer is still at index 0 after exactly steps steps. Since the answer may be too large, return it modulo 10^9 + 7.

Examples:
- Example 1:
- Input: steps = 3, arrLen = 2
- Output: 4
- Explanation: There are 4 different ways to stay at index 0 after 3 steps.
- Right, Left, Stay
- Stay, Right, Left
- Right, Stay, Left
- Stay, Stay, Stay

- Example 2:
- Input: steps = 2, arrLen = 4
- Output: 2
- Explanation: There are 2 different ways to stay at index 0 after 2 steps.
- Right, Left
- Stay, Stay

- Example 3:
- Input: steps = 4, arrLen = 2
- Output: 8

Constraints:
- 1 <= steps <= 500
- 1 <= arrLen <= 10^6
Problem url: [https://leetcode.com/problems/pascals-triangle-ii/?envType=daily-question&amp;envId=2023-10-16](https://leetcode.com/problems/pascals-triangle-ii/?envType=daily-question&amp;envId=2023-10-16)

Text:
Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

Example 1:
Input: rowIndex = 3
Output: [1,3,3,1]

Example 2:
Input: rowIndex = 0
Output: [1]

Example 3:
Input: rowIndex = 1
Output: [1,1]

Constraints:
0 <= rowIndex <= 33

Follow up:
Could you optimize your algorithm to use only O(rowIndex) extra space?
Problem url: [https://leetcode.com/problems/validate-binary-tree-nodes/?envType=daily-question&amp;envId=2023-10-17](https://leetcode.com/problems/validate-binary-tree-nodes/?envType=daily-question&amp;envId=2023-10-17)

You have n binary tree nodes numbered from 0 to n - 1. Each node i has two children leftChild[i] and rightChild[i].

Return true if and only if all the given nodes form exactly one valid binary tree.

If node i has no left child, then leftChild[i] will equal -1. Similarly, if node i has no right child, then rightChild[i] will equal -1.

Note that the nodes have no values and we only use the node numbers in this problem.

Example 1:

Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
Output: true

Example 2:

Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
Output: false

Example 3:

Input: n = 2, leftChild = [1,0], rightChild = [-1,-1]
Output: false

Constraints:

- n == leftChild.length == rightChild.length
- 1 <= n <= 10^4
- -1 <= leftChild[i], rightChild[i] <= n - 1
Problem Description

You are given an integer n, which indicates that there are n courses labeled from 1 to n. You are also given a 2D integer array relations where relations[j] = [prevCoursej, nextCoursej] denotes that course prevCoursej has to be completed before course nextCoursej (prerequisite relationship). Furthermore, you are given a 0-indexed integer array time where time[i] denotes how many months it takes to complete the `(i+1)`th course.

You must find the minimum number of months needed to complete all the courses following these rules:
- You may start taking a course at any time if the prerequisites are met.
- Any number of courses can be taken at the same time.

Return the minimum number of months needed to complete all the courses.

Example 1

Input:
python
n = 3,
relations = [[1,3],[2,3]],
time = [3,2,5]


Output:
python
8


Explanation:
The given graph and the time required to complete each course:
     1      2       3
______ ______ ______
| | | |
v v v v
3m 2m 5m

We start course 1 and course 2 simultaneously at month 0.
Course 1 takes 3 months and course 2 takes 2 months to complete respectively.
Thus, the earliest time we can start course 3 is at month 3, and the total time required is 3 + 5 = 8 months.

Example 2

Input:
python
n = 5,
relations = [[1,5],[2,5],[3,5],[3,4],[4,5]],
time = [1,2,3,4,5]


Output:
python
12

Explanation:
The given graph and the time required to complete each course:
    /     1      2      3      4      
/______________________________
| | | | |
v v v v v
1m 2m 3m 4m 5m

You can start courses 1, 2, and 3 at month 0.
You can complete them after 1, 2, and 3 months respectively.
Course 4 can be taken only after course 3 is completed, i.e., after 3 months. It is completed after 3 + 4 = 7 months.
Course 5 can be taken only after courses 1, 2, 3, and 4 have been completed, i.e., after max(1,2,3,7) = 7 months.
Thus, the minimum time needed to complete all the courses is 7 + 5 = 12 months.

Constraints
- 1 <= n <= 5 * 10^4
- 0 <= relations.length <= min(n * (n - 1) / 2, 5 * 10^4)
- relations[j].length == 2
- 1 <= prevCoursej, nextCoursej <= n
- prevCoursej != nextCoursej
- All the pairs [prevCoursej, nextCoursej] are unique.
- time.length == n
- 1 <= time[i] <= 10^4
- The given graph is a directed acyclic graph.

Problem URL: [https://leetcode.com/problems/parallel-courses-iii/?envType=daily-question&amp;envId=2023-10-18](https://leetcode.com/problems/parallel-courses-iii/?envType=daily-question&amp;envId=2023-10-18)
Problem URL:
[https://leetcode.com/problems/backspace-string-compare/?envType=daily-question&amp;envId=2023-10-19](https://leetcode.com/problems/backspace-string-compare/?envType=daily-question&amp;envId=2023-10-19)

Problem Description:
Given two strings s and t, return true if they are equal when both are typed into empty text editors. # means a backspace character.
Note that after backspacing an empty text, the text will continue empty.

Example 1:
Input: s = "ab#c", t = "ad#c"
Output: true
Explanation: Both s and t become "ac".

Example 2:
Input: s = "ab##", t = "c#d#"
Output: true
Explanation: Both s and t become "".

Example 3:
Input: s = "a#c", t = "b"
Output: false
Explanation: s becomes "c" while t becomes "b".

Constraints:
- 1 <= s.length, t.length <= 200
- s and t only contain lowercase letters and '#' characters.

Follow up:
Can you solve it in O(n) time and O(1) space?
Problem Description

You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.

Implement the NestedIterator class:

- NestedIterator(List<NestedInteger> nestedList): Initializes the iterator with the nested list nestedList.
- int next(): Returns the next integer in the nested list.
- boolean hasNext(): Returns true if there are still some integers in the nested list and false otherwise.

Your code will be tested with the following pseudocode:

initialize iterator with nestedList
res = []
while iterator.hasNext()
append iterator.next() to the end of res
return res


If res matches the expected flattened list, then your code will be judged as correct.

Example 1:

Input: nestedList = [[1,1],2,[1,1]]
Output: [1,1,2,1,1]
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].

Example 2:

Input: nestedList = [1,[4,[6]]]
Output: [1,4,6]
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].

Constraints:

- 1 <= nestedList.length <= 500
- The values of the integers in the nested list are in the range [-10^6, 10^6].

Problem URL: [Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/?envType=daily-question&amp;envId=2023-10-20)
Problem URL: [Problem Link](https://leetcode.com/problems/constrained-subsequence-sum/?envType=daily-question&amp;envId=2023-10-21)

Problem Statement:
Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied.

A subsequence of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.

Example 1:
Input: nums = [10,2,-10,5,20], k = 2
Output: 37
Explanation: The subsequence is [10, 2, 5, 20].


Example 2:
Input: nums = [-1,-2,-3], k = 1
Output: -1
Explanation: The subsequence must be non-empty, so we choose the largest number.


Example 3:
Input: nums = [10,-2,-10,-5,20], k = 2
Output: 23
Explanation: The subsequence is [10, -2, -5, 20].


Constraints:
- 1 <= k <= nums.length <= 10^5
- -104 <= nums[i] <= 10^4
[Problem url](https://leetcode.com/problems/maximum-score-of-a-good-subarray/?envType=daily-question&amp;envId=2023-10-22)

You are given an array of integers nums (0-indexed) and an integer k.

The score of a subarray (i, j) is defined as min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1).

A good subarray is a subarray where i <= k <= j.

Return the maximum possible score of a good subarray.

Example 1:
Input: nums = [1,4,3,7,4,5], k = 3
Output: 15
Explanation: The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.


Example 2:
Input: nums = [5,5,4,5,4,1,1,1], k = 0
Output: 20
Explanation: The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.


Constraints:
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 2 * 10^4
- 0 <= k < nums.length
[Problem link](https://leetcode.com/problems/power-of-four/?envType=daily-question&amp;envId=2023-10-23)

Given an integer n, return true if it is a power of four. Otherwise, return false.

An integer n is a power of four, if there exists an integer x such that n == 4x.

Example 1:
Input: n = 16
Output: true

Example 2:
Input: n = 5
Output: false

Example 3:
Input: n = 1
Output: true

Constraints:
-231 <= n <= 231 - 1

Follow up: Could you solve it without loops/recursion?
[Problem Statement](https://leetcode.com/problems/find-largest-value-in-each-tree-row/?envType=daily-question&amp;envId=2023-10-24)

Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).

Example 1:

- Input: root = [1,3,2,5,3,null,9]
- Output: [1,3,9]

Example 2:

- Input: root = [1,2,3]
- Output: [1,3]

Constraints:

- The number of nodes in the tree will be in the range [0, 104].
- -2^31 <= Node.val <= 2^31 - 1
Problem: Find the kth symbol in the nth row of a table constructed based on the given rules.

Input:
- Number of rows, n
- Index of the symbol to find, k

Output: The kth symbol in the nth row

Example 1:
- Input: n = 1, k = 1
- Output: 0
- Explanation: In the first row, there is only one symbol which is 0.

Example 2:
- Input: n = 2, k = 1
- Output: 0
- Explanation: In the second row, the first symbol is 0.

Example 3:
- Input: n = 2, k = 2
- Output: 1
- Explanation: In the second row, the second symbol is 1.

Constraints:
- The number of rows, n, is between 1 and 30.
- The index of the symbol, k, is between 1 and 2n - 1.
Problem Statement:

Given an array of unique integers, arr, where each integer arr[i] is strictly greater than 1.

We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.

Return the number of binary trees we can make. The answer may be too large so return the answer modulo 10^9 + 7.

Example 1:

Input: arr = [2,4]

Output: 3

Explanation: We can make these trees: [2], [4], [4, 2, 2]

Example 2:

Input: arr = [2,4,5,10]

Output: 7

Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].

Constraints:

- 1 <= arr.length <= 1000
- 2 <= arr[i] <= 10^9
- All the values of arr are unique.

Problem url: [https://leetcode.com/problems/binary-trees-with-factors/?envType=daily-question&amp;envId=2023-10-26](https://leetcode.com/problems/binary-trees-with-factors/?envType=daily-question&amp;envId=2023-10-26)
[Problem link](https://leetcode.com/problems/longest-palindromic-substring/?envType=daily-question&amp;envId=2023-10-27)

Given a string s, return the longest palindromic substring in s.

Example 1:
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.

Example 2:
Input: s = "cbbd"
Output: "bb"

Constraints:
- 1 <= s.length <= 1000
- s consists of only digits and English letters.
Problem URL: [Count Vowels Permutation](https://leetcode.com/problems/count-vowels-permutation/?envType=daily-question&amp;envId=2023-10-28)

Given "n", count the number of valid strings of length "n" that can be formed based on the following rules:

- Each character is a lowercase vowel ('a', 'e', 'i', 'o', 'u').
- The vowel 'a' can only be followed by an 'e'.
- The vowel 'e' can only be followed by an 'a' or an 'i'.
- The vowel 'i' cannot be followed by another 'i'.
- The vowel 'o' can only be followed by an 'i' or a 'u'.
- The vowel 'u' can only be followed by an 'a'.

The final answer should be returned modulo 10^9 + 7.

Example 1:

Input: n = 1

Output: 5

Explanation: All possible strings are: "a", "e", "i", "o", and "u".

Example 2:

Input: n = 2

Output: 10

Explanation: All possible strings are: "ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou", and "ua".

Example 3:

Input: n = 5

Output: 68

Constraints:

1 <= n <= 2 * 10^4
Problem Description:

You have buckets of liquid, and one of them is poisonous. You need to determine which bucket is poisonous within a certain amount of time.

You have some number of pigs to help you. Here are the steps you can follow:

1. Choose some live pigs to feed.
2. For each pig, choose which buckets to feed it simultaneously. Each pig can feed from any number of buckets, and each bucket can be fed from by any number of pigs.
3. Wait for a certain amount of time, called "minutesToDie". During this time, you cannot feed any other pigs.
4. After "minutesToDie" minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.
5. Repeat this process until you run out of time.

You need to determine the minimum number of pigs needed to figure out which bucket is poisonous within the allotted time.

Example:
Input: buckets = 4, minutesToDie = 15, minutesToTest = 15
Output: 2
Explanation: You can determine the poisonous bucket as follows:
- Feed the first pig buckets 1 and 2, and feed the second pig buckets 2 and 3. After 15 minutes, there are 4 possible outcomes:
- If only the first pig dies, then bucket 1 must be poisonous.
- If only the second pig dies, then bucket 3 must be poisonous.
- If both pigs die, then bucket 2 must be poisonous.
- If neither pig dies, then bucket 4 must be poisonous.

Constraints:
- 1 <= buckets <= 1000
- 1 <= minutesToDie <= minutesToTest <= 100
Problem url: [leetcode.com/problems/sort-integers-by-the-number-of-1-bits](https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/?envType=daily-question&amp;envId=2023-10-30)

You are given an integer array arr. Sort the integers in the array in ascending order by the number of 1's in their binary representation. In case of two or more integers having the same number of 1's, sort them in ascending order.

Return the sorted array.

Example 1:
Input: arr = [0,1,2,3,4,5,6,7,8]
Output: [0,1,2,4,8,3,5,6,7]
Explanation:
[0] is the only integer with 0 bits.
[1,2,4,8] all have 1 bit.
[3,5,6] have 2 bits.
[7] has 3 bits.
The sorted array by bits is [0,1,2,4,8,3,5,6,7]


Example 2:
Input: arr = [1024,512,256,128,64,32,16,8,4,2,1]
Output: [1,2,4,8,16,32,64,128,256,512,1024]
Explanation: All integers have 1 bit in the binary representation, so you should just sort them in ascending order.


Constraints:
- 1 <= arr.length <= 500
- 0 <= arr[i] <= 10^4
[Problem URL](https://leetcode.com/problems/find-the-original-array-of-prefix-xor/?envType=daily-question&amp;envId=2023-10-31)

You are given an integer array pref of size n. Find and return the array arr of size n that satisfies the following equation:

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 <= 10^5
- 0 <= pref[i] <= 10^6
Problem: Find the mode(s) (most frequently occurred element) in a binary search tree with duplicates.

- Input: The root of a binary search tree (BST)
- Output: Array containing the mode(s) of the BST

Assumptions:
- BST has duplicates
- If there are multiple modes, return them in any order

Constraints:
- Number of nodes in the tree: [1, 10^4]
- Node values: -10^5 <= Node.val <= 10^5

Follow-up question: Can you solve this without using any extra space? (Recursion stack does not count)
Problem: The task is to count the number of nodes in a binary tree where the node's value is equal to the average of the values in its subtree.

Example 1:

Input: root = [4,8,5,0,1,null,6]

Output: 5

Explanation:
- For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4.
- For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5.
- For the node with value 0: The average of its subtree is 0 / 1 = 0.
- For the node with value 1: The average of its subtree is 1 / 1 = 1.
- For the node with value 6: The average of its subtree is 6 / 1 = 6.

Example 2:

Input: root = [1]

Output: 1

Explanation: For the node with value 1: The average of its subtree is 1 / 1 = 1.

Constraints:

- The number of nodes in the tree is in the range [1, 1000].
- 0 <= Node.val <= 1000
Problem url: [Build an Array with Stack Operations](https://leetcode.com/problems/build-an-array-with-stack-operations/?envType=daily-question&amp;envId=2023-11-03)

You are given an integer array target and an integer n.
You have an empty stack with the two following operations:
- "Push": pushes an integer to the top of the stack.
- "Pop": removes the integer on the top of the stack.
You also have a stream of the integers in the range [1, n].
Use the two stack operations to make the numbers in the stack (from the bottom to the top) equal to target. You should follow the following rules:
- If the stream of the integers is not empty, pick the next integer from the stream and push it to the top of the stack.
- If the stack is not empty, pop the integer at the top of the stack.
- If, at any moment, the elements in the stack (from the bottom to the top) are equal to target, do not read new integers from the stream and do not do more operations on the stack.
Return the stack operations needed to build target following the mentioned rules. If there are multiple valid answers, return any of them.

Example 1:
Input: target = [1,3], n = 3
Output: ["Push","Push","Pop","Push"]
Explanation: Initially the stack s is empty. The last element is the top of the stack.
- Read 1 from the stream and push it to the stack. s = [1].
- Read 2 from the stream and push it to the stack. s = [1,2].
- Pop the integer on the top of the stack. s = [1].
- Read 3 from the stream and push it to the stack. s = [1,3].

Example 2:
Input: target = [1,2,3], n = 3
Output: ["Push","Push","Push"]
Explanation: Initially the stack s is empty. The last element is the top of the stack.
- Read 1 from the stream and push it to the stack. s = [1].
- Read 2 from the stream and push it to the stack. s = [1,2].
- Read 3 from the stream and push it to the stack. s = [1,2,3].

Example 3:
Input: target = [1,2], n = 4
Output: ["Push","Push"]
Explanation: Initially the stack s is empty. The last element is the top of the stack.
- Read 1 from the stream and push it to the stack. s = [1].
- Read 2 from the stream and push it to the stack. s = [1,2].
- Since the stack (from the bottom to the top) is equal to target, we stop the stack operations.
The answers that read integer 3 from the stream are not accepted.

Constraints:
- 1 <= target.length <= 100
- 1 <= n <= 100
- 1 <= target[i] <= n
- target is strictly increasing.