Problem URL:
[https://leetcode.com/problems/backspace-string-compare/?envType=daily-question&envId=2023-10-19](https://leetcode.com/problems/backspace-string-compare/?envType=daily-question&envId=2023-10-19)
Problem Description:
Given two strings
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?
[https://leetcode.com/problems/backspace-string-compare/?envType=daily-question&envId=2023-10-19](https://leetcode.com/problems/backspace-string-compare/?envType=daily-question&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
Implement the
-
-
-
Your code will be tested with the following pseudocode:
If
Example 1:
Input:
Output:
Explanation: By calling
Example 2:
Input:
Output:
Explanation: By calling
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&envId=2023-10-20)
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&envId=2023-10-20)
Problem URL: [Problem Link](https://leetcode.com/problems/constrained-subsequence-sum/?envType=daily-question&envId=2023-10-21)
Problem Statement:
Given an integer array
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:
Example 2:
Example 3:
Constraints:
- 1 <= k <= nums.length <= 10^5
- -104 <= nums[i] <= 10^4
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&envId=2023-10-22)
You are given an array of integers
The score of a subarray
A good subarray is a subarray where
Return the maximum possible score of a good subarray.
Example 1:
Example 2:
Constraints:
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 2 * 10^4
- 0 <= k < nums.length
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&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?
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&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
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.
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,
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:
Output:
Explanation: We can make these trees:
Example 2:
Input:
Output:
Explanation: We can make these trees:
Constraints:
- 1 <=
- 2 <=
- All the values of
Problem url: [https://leetcode.com/problems/binary-trees-with-factors/?envType=daily-question&envId=2023-10-26](https://leetcode.com/problems/binary-trees-with-factors/?envType=daily-question&envId=2023-10-26)
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&envId=2023-10-26](https://leetcode.com/problems/binary-trees-with-factors/?envType=daily-question&envId=2023-10-26)
[Problem link](https://leetcode.com/problems/longest-palindromic-substring/?envType=daily-question&envId=2023-10-27)
Given a string
Example 1:
Input:
Output:
Explanation:
Example 2:
Input:
Output:
Constraints:
- 1 <= s.length <= 1000
-
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&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
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
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&envId=2023-10-30)
You are given an integer array
Return the sorted array.
Example 1:
Example 2:
Constraints:
- 1 <= arr.length <= 500
- 0 <= arr[i] <= 10^4
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&envId=2023-10-31)
You are given an integer array
Note that
Example 1:
Example 2:
Constraints:
-
-
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)
- 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:
Output:
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:
Output:
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
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&envId=2023-11-03)
You are given an integer array
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
- 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
Return the stack operations needed to build
Example 1:
Input:
Output:
Explanation: Initially the stack
- Read 1 from the stream and push it to the stack.
- Read 2 from the stream and push it to the stack.
- Pop the integer on the top of the stack.
- Read 3 from the stream and push it to the stack.
Example 2:
Input:
Output:
Explanation: Initially the stack
- Read 1 from the stream and push it to the stack.
- Read 2 from the stream and push it to the stack.
- Read 3 from the stream and push it to the stack.
Example 3:
Input:
Output:
Explanation: Initially the stack
- Read 1 from the stream and push it to the stack.
- Read 2 from the stream and push it to the stack.
- Since the stack (from the bottom to the top) is equal to
The answers that read integer 3 from the stream are not accepted.
Constraints:
- 1 <=
- 1 <=
- 1 <=
-
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.
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.
Problem
[Problem URL](https://leetcode.com/problems/find-the-winner-of-an-array-game/?envType=daily-question&envId=2023-11-05)
Given an integer array
Example 1
Input:
Output:
Explanation: Let's see the rounds of the game:
- Round 1:
- Round 2:
- Round 3:
- Round 4:
So we can see that 4 rounds will be played and
Example 2
Input:
Output:
Explanation:
Constraints
-
-
-
- `1 <= k <= 10^9
[Problem URL](https://leetcode.com/problems/find-the-winner-of-an-array-game/?envType=daily-question&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&envId=2023-11-06](https://leetcode.com/problems/seat-reservation-manager/?envType=daily-question&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:
-
-
-
Example:
Input:
Output:
Explanation:
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.
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.