1.57K subscribers
577 photos
1 file
949 links
Don't miss a day to solve the problem
My leetcode graph - https://leetcode.com/SamoylenkoDmitry/
Download Telegram
[Problem link](https://leetcode.com/problems/power-of-four/?envType=daily-question&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.
Problem Statement

We have a wooden plank of length *n* units. Ants are walking on the plank, each moving at a speed of 1 unit per second. Some ants move to the left, while others move to the right. When two ants moving in opposite directions meet, they change directions and continue moving. Changing directions does not take any additional time. When an ant reaches one end of the plank, it falls off immediately.

Given the integer *n* and two integer arrays *left* and *right*, which represent the positions of ants moving to the left and right respectively, we need to determine the moment when the last ant(s) fall off the plank.

Example 1:

Input:
- *n* = 4
- *left* = [4,3]
- *right* = [0,1]

Output: 4

Explanation:
- Ant A at index 0 is moving to the right.
- Ant B at index 1 is moving to the right.
- Ant C at index 3 is moving to the left.
- Ant D at index 4 is moving to the left.

The last moment when an ant was on the plank is at time t = 4 seconds. After that, it falls off immediately.

Example 2:

Input:
- *n* = 7
- *left* = []
- *right* = [0,1,2,3,4,5,6,7]

Output: 7

Explanation:
All ants are moving to the right. The ant at index 0 needs 7 seconds to fall off.

Example 3:

Input:
- *n* = 7
- *left* = [0,1,2,3,4,5,6,7]
- *right* = []

Output: 7

Explanation:
All ants are moving to the left. The ant at index 7 needs 7 seconds to fall off.

Constraints:
- 1 <= *n* <= 10^4
- 0 <= *left.length* <= *n* + 1
- 0 <= *left[i]* <= *n*
- 0 <= *right.length* <= *n* + 1
- 0 <= *right[i]* <= *n*
- 1 <= *left.length* + *right.length* <= *n* + 1
- All values of *left* and *right* are unique, and each value can appear only in one of the two arrays.
Yesterday's problem, I accidentally solved another
Problem

[Problem URL](https://leetcode.com/problems/find-the-winner-of-an-array-game/?envType=daily-question&amp;envId=2023-11-05)

Given an integer array arr of distinct integers and an integer k. A game will be played between the first two elements of the array (arr[0] and arr[1]). In each round of the game, we compare arr[0] with arr[1], the larger integer wins and remains at position 0, and the smaller integer moves to the end of the array. The game ends when an integer wins k consecutive rounds. Return the integer which will win the game. It is guaranteed that there will be a winner of the game.

Example 1

Input: arr = [2,1,3,5,4,6,7], k = 2

Output: 5

Explanation: Let's see the rounds of the game:
- Round 1: [2,1,3,5,4,6,7], winner: 2, win count: 1
- Round 2: [2,3,5,4,6,7,1], winner: 3, win count: 1
- Round 3: [3,5,4,6,7,1,2], winner: 5, win count: 1
- Round 4: [5,4,6,7,1,2,3], winner: 5, win count: 2

So we can see that 4 rounds will be played and 5 is the winner because it wins 2 consecutive games.

Example 2

Input: arr = [3,2,1], k = 10

Output: 3

Explanation: 3 will win the first 10 rounds consecutively.

Constraints

- 2 <= arr.length <= 10^5
- 1 <= arr[i] <= 10^6
- arr contains distinct integers.
- `1 <= k <= 10^9
Problem URL: [https://leetcode.com/problems/seat-reservation-manager/?envType=daily-question&amp;envId=2023-11-06](https://leetcode.com/problems/seat-reservation-manager/?envType=daily-question&amp;envId=2023-11-06)

Text:
Design a system that manages the reservation state of n seats that are numbered from 1 to n.

Implement the SeatManager class:
- SeatManager(int n) Initializes a SeatManager object that will manage n seats numbered from 1 to n. All seats are initially available.
- int reserve() Fetches the smallest-numbered unreserved seat, reserves it, and returns its number.
- void unreserve(int seatNumber) Unreserves the seat with the given seatNumber.

Example:

Input:
["SeatManager", "reserve", "reserve", "unreserve", "reserve", "reserve", "reserve", "reserve", "unreserve"]
[[5], [], [], [2], [], [], [], [], [5]]


Output:
[null, 1, 2, null, 2, 3, 4, 5, null]


Explanation:
SeatManager seatManager = new SeatManager(5); // Initializes a SeatManager with 5 seats.
seatManager.reserve(); // All seats are available, so return the lowest numbered seat, which is 1.
seatManager.reserve(); // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
seatManager.unreserve(2); // Unreserve seat 2, so now the available seats are [2,3,4,5].
seatManager.reserve(); // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
seatManager.reserve(); // The available seats are [3,4,5], so return the lowest of them, which is 3.
seatManager.reserve(); // The available seats are [4,5], so return the lowest of them, which is 4.
seatManager.reserve(); // The only available seat is seat 5, so return 5.
seatManager.unreserve(5); // Unreserve seat 5, so now the available seats are [5].


Constraints:
- 1 <= n <= 10^5
- 1 <= seatNumber <= n
- For each call to reserve, it is guaranteed that there will be at least one unreserved seat.
- For each call to unreserve, it is guaranteed that seatNumber will be reserved.
- At most 10^5 calls in total will be made to reserve and unreserve.
## Problem

You are playing a video game where you are defending your city from a group of n monsters. You are given a 0-indexed integer array dist of size n, where dist[i] is the initial distance in kilometers of the ith monster from the city.

The monsters walk toward the city at a constant speed. The speed of each monster is given to you in an integer array speed of size n, where speed[i] is the speed of the ith monster in kilometers per minute.

You have a weapon that, once fully charged, can eliminate a single monster. However, the weapon takes one minute to charge. The weapon is fully charged at the very start.

You lose when any monster reaches your city. If a monster reaches the city at the exact moment the weapon is fully charged, it counts as a loss, and the game ends before you can use your weapon.

Return the maximum number of monsters that you can eliminate before you lose, or n if you can eliminate all the monsters before they reach the city.

## Example 1
Input: dist = [1,3,4], speed = [1,1,1]
Output: 3
Explanation:
In the beginning, the distances of the monsters are [1,3,4]. You eliminate the first monster.
After a minute, the distances of the monsters are [X,2,3]. You eliminate the second monster.
After a minute, the distances of the monsters are [X,X,2]. You eliminate the thrid monster.
All 3 monsters can be eliminated.


## Example 2
Input: dist = [1,1,2,3], speed = [1,1,1,1]
Output: 1
Explanation:
In the beginning, the distances of the monsters are [1,1,2,3]. You eliminate the first monster.
After a minute, the distances of the monsters are [X,0,1,2], so you lose.
You can only eliminate 1 monster.


## Example 3
Input: dist = [3,2,4], speed = [5,3,2]
Output: 1
Explanation:
In the beginning, the distances of the monsters are [3,2,4]. You eliminate the first monster.
After a minute, the distances of the monsters are [X,0,2], so you lose.
You can only eliminate 1 monster.


## Constraints
- n == dist.length == speed.length
- 1 <= n <= 10^5
- 1 <= dist[i], speed[i] <= 10^5
Problem: Determine if a cell is reachable at a given time

URL: [https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/?envType=daily-question&amp;envId=2023-11-08](https://leetcode.com/problems/determine-if-a-cell-is-reachable-at-a-given-time/?envType=daily-question&amp;envId=2023-11-08)

Text:
You are given four integers sx, sy, fx, fy, and a non-negative integer t.
In an infinite 2D grid, you start at the cell (sx, sy). Each second, you must move to any of its adjacent cells.
Return true if you can reach cell (fx, fy) after exactly t seconds, or false otherwise.
A cell's adjacent cells are the 8 cells around it that share at least one corner with it. You can visit the same cell several times.

Example 1:
Input: sx = 2, sy = 4, fx = 7, fy = 7, t = 6
Output: true
Explanation: Starting at cell (2, 4), we can reach cell (7, 7) in exactly 6 seconds by going through the cells depicted in the picture above.

Example 2:
Input: sx = 3, sy = 1, fx = 7, fy = 3, t = 3
Output: false
Explanation: Starting at cell (3, 1), it takes at least 4 seconds to reach cell (7, 3) by going through the cells depicted in the picture above. Hence, we cannot reach cell (7, 3) at the third second.

Constraints:
- 1 <= sx, sy, fx, fy <= 10^9
- `0 <= t <= 10^9
Problem URL: [https://leetcode.com/problems/count-number-of-homogenous-substrings/?envType=daily-question&amp;envId=2023-11-09](https://leetcode.com/problems/count-number-of-homogenous-substrings/?envType=daily-question&amp;envId=2023-11-09)

Given a string s, return the number of homogenous substrings of s. Modulo 109 + 7 is used for returning the answer, as it may be too large.

A string is homogenous if all its characters are the same. A substring is a contiguous sequence of characters within a string.

Example 1:
Input: s = "abbcccaa"
Output: 13
Explanation: The homogenous substrings are as follows:
- "a" appears 3 times.
- "aa" appears 1 time.
- "b" appears 2 times.
- "bb" appears 1 time.
- "c" appears 3 times.
- "cc" appears 2 times.
- "ccc" appears 1 time.
Total count: 3 + 1 + 2 + 1 + 3 + 2 + 1 = 13.

Example 2:
Input: s = "xy"
Output: 2
Explanation: The homogenous substrings are "x" and "y".

Example 3:
Input: s = "zzzzz"
Output: 15

Constraints:
- 1 <= s.length <= 105
- s consists of lowercase letters.
Problem URL: [https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/?envType=daily-question&amp;envId=2023-11-10](https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/?envType=daily-question&amp;envId=2023-11-10)

Text: There is an integer array nums that consists of n unique elements, but you have forgotten it. However, you do remember every pair of adjacent elements in nums.

You are given a 2D integer array adjacentPairs of size n - 1 where each adjacentPairs[i] = [ui, vi] indicates that the elements ui and vi are adjacent in nums.

It is guaranteed that every adjacent pair of elements nums[i] and nums[i+1] will exist in adjacentPairs, either as [nums[i], nums[i+1]] or [nums[i+1], nums[i]]. The pairs can appear in any order.

Return the original array nums. If there are multiple solutions, return any of them.

Example 1:
Input: adjacentPairs = [[2,1],[3,4],[3,2]]
Output: [1,2,3,4]
Explanation: This array has all its adjacent pairs in adjacentPairs.
Notice that adjacentPairs[i] may not be in left-to-right order.


Example 2:
Input: adjacentPairs = [[4,-2],[1,4],[-3,1]]
Output: [-2,4,1,-3]
Explanation: There can be negative numbers.
Another solution is [-3,1,4,-2], which would also be accepted.


Example 3:
Input: adjacentPairs = [[100000,-100000]]
Output: [100000,-100000]


Constraints:
- nums.length == n
- adjacentPairs.length == n - 1
- adjacentPairs[i].length == 2
- 2 <= n <= 10^5
- -10^5 <= nums[i], ui, vi <= 10^5
- There exists some nums that has adjacentPairs as its pairs.