2023-10-08
1458. Max Dot Product of Two Subsequences
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
Given two arrays
Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length.
A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1458. Max Dot Product of Two Subsequences
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
Given two arrays
nums1 and nums2.Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length.
A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,
[2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).Example 1:
Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6]
Output: 18
Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2.
Their dot product is (2*3 + (-2)*(-6)) = 18.
Example 2:
Input: nums1 = [3,-2], nums2 = [2,-6,7]
Output: 21
Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2.
Their dot product is (3*7) = 21.
Example 3:
Input: nums1 = [-1,-1], nums2 = [1,1]
Output: -1
Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2.
Their dot product is -1.
Constraints:
•
1 <= nums1.length, nums2.length <= 500•
-1000 <= nums1[i], nums2[i] <= 10002023-10-09
34. Find First and Last Position of Element in Sorted Array
Topic: Array, Binary Search
Difficulty: Medium
Problem:
Given an array of integers
If
You must write an algorithm with
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
34. Find First and Last Position of Element in Sorted Array
Topic: Array, Binary Search
Difficulty: Medium
Problem:
Given an array of integers
nums sorted in non-decreasing order, find the starting and ending position of a given target value.If
target is not found in the array, return [-1, -1].You must write an algorithm with
O(log n) runtime complexity.Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
Example 3:
Input: nums = [], target = 0
Output: [-1,-1]
Constraints:
•
0 <= nums.length <= 10^5•
-10^9 <= nums[i] <= 10^9•
nums is a non-decreasing array.•
-10^9 <= target <= 10^92023-10-10
2009. Minimum Number of Operations to Make Array Continuous
Topic: Array, Binary Search
Difficulty: Hard
Problem:
You are given an integer array
• All elements in
• The difference between the maximum element and the minimum element in
For example,
Return the minimum number of operations to make
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2009. Minimum Number of Operations to Make Array Continuous
Topic: Array, Binary Search
Difficulty: Hard
Problem:
You are given an integer array
nums. In one operation, you can replace any element in nums with any integer.nums is considered continuous if both of the following conditions are fulfilled:• All elements in
nums are unique.• The difference between the maximum element and the minimum element in
nums equals nums.length - 1.For example,
nums = [4, 2, 5, 3] is continuous, but nums = [1, 2, 3, 5, 6] is not continuous.Return the minimum number of operations to make
nums continuous.Example 1:
Input: nums = [4,2,5,3]
Output: 0
Explanation: nums is already continuous.
Example 2:
Input: nums = [1,2,3,5,6]
Output: 1
Explanation: One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
Example 3:
Input: nums = [1,10,100,1000]
Output: 3
Explanation: One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^92023-10-11
2251. Number of Flowers in Full Bloom
Topic: Array, Hash Table, Binary Search, Sorting, Prefix Sum, Ordered Set
Difficulty: Hard
Problem:
You are given a 0-indexed 2D integer array
Return an integer array
Example 1:
Image: https://assets.leetcode.com/uploads/2022/03/02/ex1new.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2022/03/02/ex2new.jpg
Constraints:
•
•
•
•
•
2251. Number of Flowers in Full Bloom
Topic: Array, Hash Table, Binary Search, Sorting, Prefix Sum, Ordered Set
Difficulty: Hard
Problem:
You are given a 0-indexed 2D integer array
flowers, where flowers[i] = [start_i, end_i] means the i^th flower will be in full bloom from start_i to end_i (inclusive). You are also given a 0-indexed integer array people of size n, where people[i] is the time that the i^th person will arrive to see the flowers.Return an integer array
answer of size n, where answer[i] is the number of flowers that are in full bloom when the i^th person arrives.Example 1:
Image: https://assets.leetcode.com/uploads/2022/03/02/ex1new.jpg
Input: flowers = [[1,6],[3,7],[9,12],[4,13]], poeple = [2,3,7,11]
Output: [1,2,2,2]
Explanation: The figure above shows the times when the flowers are in full bloom and when the people arrive.
For each person, we return the number of flowers in full bloom during their arrival.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/03/02/ex2new.jpg
Input: flowers = [[1,10],[3,3]], poeple = [3,3,2]
Output: [2,2,1]
Explanation: The figure above shows the times when the flowers are in full bloom and when the people arrive.
For each person, we return the number of flowers in full bloom during their arrival.
Constraints:
•
1 <= flowers.length <= 5 * 10^4•
flowers[i].length == 2•
1 <= start_i <= end_i <= 10^9•
1 <= people.length <= 5 * 10^4•
1 <= people[i] <= 10^92023-10-12
1095. Find in Mountain Array
Topic: Array, Binary Search, Interactive
Difficulty: Hard
Problem:
(This problem is an interactive problem.)
You may recall that an array
•
• There exists some
•
•
Given a mountain array
You cannot access the mountain array directly. You may only access the array using a
•
•
Submissions making more than
Example 1:
Example 2:
Constraints:
•
•
•
1095. Find in Mountain Array
Topic: Array, Binary Search, Interactive
Difficulty: Hard
Problem:
(This problem is an interactive problem.)
You may recall that an array
arr is a mountain array if and only if:•
arr.length >= 3• There exists some
i with 0 < i < arr.length - 1 such that:•
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]•
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]Given a mountain array
mountainArr, return the minimum index such that mountainArr.get(index) == target. If such an index does not exist, return -1.You cannot access the mountain array directly. You may only access the array using a
MountainArray interface:•
MountainArray.get(k) returns the element of the array at index k (0-indexed).•
MountainArray.length() returns the length of the array.Submissions making more than
100 calls to MountainArray.get will be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification.Example 1:
Input: array = [1,2,3,4,5,3,1], target = 3
Output: 2
Explanation: 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.
Example 2:
Input: array = [0,1,2,4,2,1], target = 3
Output: -1
Explanation: 3 does not exist in the array, so we return -1.
Constraints:
•
3 <= mountain_arr.length() <= 10^4•
0 <= target <= 10^9•
0 <= mountain_arr.get(index) <= 10^92023-10-13
746. Min Cost Climbing Stairs
Topic: Array, Dynamic Programming
Difficulty: Easy
Problem:
You are given an integer array
You can either start from the step with index
Return the minimum cost to reach the top of the floor.
Example 1:
Example 2:
Constraints:
•
•
746. Min Cost Climbing Stairs
Topic: Array, Dynamic Programming
Difficulty: Easy
Problem:
You are given an integer array
cost where cost[i] is the cost of i^th step on a staircase. Once you pay the cost, you can either climb one or two steps.You can either start from the step with index
0, or the step with index 1.Return the minimum cost to reach the top of the floor.
Example 1:
Input: cost = [10,15,20]
Output: 15
Explanation: You will start at index 1.
- Pay 15 and climb two steps to reach the top.
The total cost is 15.
Example 2:
Input: cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6
Explanation: You will start at index 0.
- Pay 1 and climb two steps to reach index 2.
- Pay 1 and climb two steps to reach index 4.
- Pay 1 and climb two steps to reach index 6.
- Pay 1 and climb one step to reach index 7.
- Pay 1 and climb two steps to reach index 9.
- Pay 1 and climb one step to reach the top.
The total cost is 6.
Constraints:
•
2 <= cost.length <= 1000•
0 <= cost[i] <= 9992023-10-14
2742. Painting the Walls
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
You are given two 0-indexed integer arrays,
• A paid painter that paints the
• A free painter that paints any wall in
Return the minimum amount of money required to paint the
Example 1:
Example 2:
Constraints:
•
•
•
•
2742. Painting the Walls
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
You are given two 0-indexed integer arrays,
cost and time, of size n representing the costs and the time taken to paint n different walls respectively. There are two painters available:• A paid painter that paints the
i^th wall in time[i] units of time and takes cost[i] units of money.• A free painter that paints any wall in
1 unit of time at a cost of 0. But the free painter can only be used if the paid painter is already occupied.Return the minimum amount of money required to paint the
n walls.Example 1:
Input: cost = [1,2,3,2], time = [1,2,3,2]
Output: 3
Explanation: The walls at index 0 and 1 will be painted by the paid painter, and it will take 3 units of time; meanwhile, the free painter will paint the walls at index 2 and 3, free of cost in 2 units of time. Thus, the total cost is 1 + 2 = 3.
Example 2:
Input: cost = [2,3,4,2], time = [1,1,1,1]
Output: 4
Explanation: The walls at index 0 and 3 will be painted by the paid painter, and it will take 2 units of time; meanwhile, the free painter will paint the walls at index 1 and 2, free of cost in 2 units of time. Thus, the total cost is 2 + 2 = 4.
Constraints:
•
1 <= cost.length <= 500•
cost.length == time.length•
1 <= cost[i] <= 10^6•
1 <= time[i] <= 5002023-10-15
1269. Number of Ways to Stay in the Same Place After Some Steps
Topic: Dynamic Programming
Difficulty: Hard
Problem:
You have a pointer at index
Given two integers
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1269. Number of Ways to Stay in the Same Place After Some Steps
Topic: Dynamic Programming
Difficulty: Hard
Problem:
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.Example 1:
Input: steps = 3, arrLen = 2
Output: 4
Explanation: There are 4 differents 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 differents 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^62023-10-16
119. Pascal's Triangle II
Topic: Array, Dynamic Programming
Difficulty: Easy
Problem:
Given an integer
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
Image: https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif
Example 1:
Example 2:
Example 3:
Constraints:
•
Follow up: Could you optimize your algorithm to use only
119. Pascal's Triangle II
Topic: Array, Dynamic Programming
Difficulty: Easy
Problem:
Given an integer
rowIndex, return the rowIndex^th (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:
Image: https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif
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 <= 33Follow up: Could you optimize your algorithm to use only
O(rowIndex) extra space?2023-10-17
1361. Validate Binary Tree Nodes
Topic: Tree, Depth-First Search, Breadth-First Search, Union Find, Graph, Binary Tree
Difficulty: Medium
Problem:
You have
If node
Note that the nodes have no values and that we only use the node numbers in this problem.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/08/23/1503_ex1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2019/08/23/1503_ex2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2019/08/23/1503_ex3.png
Constraints:
•
•
•
1361. Validate Binary Tree Nodes
Topic: Tree, Depth-First Search, Breadth-First Search, Union Find, Graph, Binary Tree
Difficulty: Medium
Problem:
You have
n binary tree nodes numbered from 0 to n - 1 where 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 for the right child.Note that the nodes have no values and that we only use the node numbers in this problem.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/08/23/1503_ex1.png
Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
Output: true
Example 2:
Image: https://assets.leetcode.com/uploads/2019/08/23/1503_ex2.png
Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
Output: false
Example 3:
Image: https://assets.leetcode.com/uploads/2019/08/23/1503_ex3.png
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 - 12023-10-18
2050. Parallel Courses III
Topic: Array, Dynamic Programming, Graph, Topological Sort
Difficulty: Hard
Problem:
You are given an integer
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.
Note: The test cases are generated such that it is possible to complete every course (i.e., the graph is a directed acyclic graph).
Example 1:
Image: https://assets.leetcode.com/uploads/2021/10/07/ex1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/10/07/ex2.png
Constraints:
•
•
•
•
•
• All the pairs
•
•
• The given graph is a directed acyclic graph.
2050. Parallel Courses III
Topic: Array, Dynamic Programming, Graph, Topological Sort
Difficulty: Hard
Problem:
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] = [prevCourse_j, nextCourse_j] denotes that course prevCourse_j has to be completed before course nextCourse_j (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.
Note: The test cases are generated such that it is possible to complete every course (i.e., the graph is a directed acyclic graph).
Example 1:
Image: https://assets.leetcode.com/uploads/2021/10/07/ex1.png
Input: n = 3, relations = [[1,3],[2,3]], time = [3,2,5]
Output: 8
Explanation: The figure above represents the given graph and the time required to complete each course.
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:
Image: https://assets.leetcode.com/uploads/2021/10/07/ex2.png
Input: n = 5, relations = [[1,5],[2,5],[3,5],[3,4],[4,5]], time = [1,2,3,4,5]
Output: 12
Explanation: The figure above represents the given graph and the time required to complete each course.
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 <= prevCourse_j, nextCourse_j <= n•
prevCourse_j != nextCourse_j• All the pairs
[prevCourse_j, nextCourse_j] are unique.•
time.length == n•
1 <= time[i] <= 10^4• The given graph is a directed acyclic graph.
2023-10-19
844. Backspace String Compare
Topic: Two Pointers, String, Stack, Simulation
Difficulty: Easy
Problem:
Given two strings
Note that after backspacing an empty text, the text will continue empty.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
Follow up: Can you solve it in
844. Backspace String Compare
Topic: Two Pointers, String, Stack, Simulation
Difficulty: Easy
Problem:
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?2023-10-20
341. Flatten Nested List Iterator
Topic: Stack, Tree, Depth-First Search, Design, Queue, Iterator
Difficulty: Medium
Problem:
You are given a nested list of integers
Implement the
•
•
•
Your code will be tested with the following pseudocode:
If
Example 1:
Example 2:
Constraints:
•
• The values of the integers in the nested list is in the range
341. Flatten Nested List Iterator
Topic: Stack, Tree, Depth-First Search, Design, Queue, Iterator
Difficulty: Medium
Problem:
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 is in the range
[-10^6, 10^6].2023-10-21
1425. Constrained Subsequence Sum
Topic: Array, Dynamic Programming, Queue, Sliding Window, Heap (Priority Queue), Monotonic Queue
Difficulty: Hard
Problem:
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:
•
•
1425. Constrained Subsequence Sum
Topic: Array, Dynamic Programming, Queue, Sliding Window, Heap (Priority Queue), Monotonic Queue
Difficulty: Hard
Problem:
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•
-10^4 <= nums[i] <= 10^42023-10-22
1793. Maximum Score of a Good Subarray
Topic: Array, Two Pointers, Binary Search, Stack, Monotonic Stack
Difficulty: Hard
Problem:
You are given an array of integers
The score of a subarray
Return the maximum possible score of a good subarray.
Example 1:
Example 2:
Constraints:
•
•
•
1793. Maximum Score of a Good Subarray
Topic: Array, Two Pointers, Binary Search, Stack, Monotonic Stack
Difficulty: Hard
Problem:
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.length2023-10-23
342. Power of Four
Topic: Math, Bit Manipulation, Recursion
Difficulty: Easy
Problem:
Given an integer
An integer
Example 1:
Example 2:
Example 3:
Constraints:
•
Follow up: Could you solve it without loops/recursion?
342. Power of Four
Topic: Math, Bit Manipulation, Recursion
Difficulty: Easy
Problem:
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 == 4^x.Example 1:
Input: n = 16
Output: true
Example 2:
Input: n = 5
Output: false
Example 3:
Input: n = 1
Output: true
Constraints:
•
-2^31 <= n <= 2^31 - 1Follow up: Could you solve it without loops/recursion?
2023-10-24
515. Find Largest Value in Each Tree Row
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg
Example 2:
Constraints:
• The number of nodes in the tree will be in the range
•
515. Find Largest Value in Each Tree Row
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).Example 1:
Image: https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg
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, 10^4].•
-2^31 <= Node.val <= 2^31 - 12023-10-25
779. K-th Symbol in Grammar
Topic: Math, Bit Manipulation, Recursion
Difficulty: Medium
Problem:
We build a table of
• For example, for
Given two integer
Example 1:
Example 2:
Example 3:
Constraints:
•
•
779. K-th Symbol in Grammar
Topic: Math, Bit Manipulation, Recursion
Difficulty: Medium
Problem:
We build a table of
n rows (1-indexed). We start by writing 0 in the 1^st row. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.• For example, for
n = 3, the 1^st row is 0, the 2^nd row is 01, and the 3^rd row is 0110.Given two integer
n and k, return the k^th (1-indexed) symbol in the n^th row of a table of n rows.Example 1:
Input: n = 1, k = 1
Output: 0
Explanation: row 1: 0
Example 2:
Input: n = 2, k = 1
Output: 0
Explanation:
row 1: 0
row 2: 01
Example 3:
Input: n = 2, k = 2
Output: 1
Explanation:
row 1: 0
row 2: 01
Constraints:
•
1 <= n <= 30•
1 <= k <= 2^n - 12023-10-26
823. Binary Trees With Factors
Topic: Array, Hash Table, Dynamic Programming, Sorting
Difficulty: Medium
Problem:
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
Example 1:
Example 2:
Constraints:
•
•
• All the values of
823. Binary Trees With Factors
Topic: Array, Hash Table, Dynamic Programming, Sorting
Difficulty: Medium
Problem:
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.2023-10-27
5. Longest Palindromic Substring
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given a string
Example 1:
Example 2:
Constraints:
•
•
5. Longest Palindromic Substring
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
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 consist of only digits and English letters.