2023-05-29
1603. Design Parking System
Topic: Design, Simulation, Counting
Difficulty: Easy
Problem:
Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.
Implement the
•
•
Example 1:
Constraints:
•
•
• At most
1603. Design Parking System
Topic: Design, Simulation, Counting
Difficulty: Easy
Problem:
Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.
Implement the
ParkingSystem class:•
ParkingSystem(int big, int medium, int small) Initializes object of the ParkingSystem class. The number of slots for each parking space are given as part of the constructor.•
bool addCar(int carType) Checks whether there is a parking space of carType for the car that wants to get into the parking lot. carType can be of three kinds: big, medium, or small, which are represented by 1, 2, and 3 respectively. A car can only park in a parking space of its carType. If there is no space available, return false, else park the car in that size space and return true.Example 1:
Input
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
[[1, 1, 0], [1], [2], [3], [1]]
Output
[null, true, true, false, false]
Explanation
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
parkingSystem.addCar(1); // return true because there is 1 available slot for a big car
parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car
parkingSystem.addCar(3); // return false because there is no available slot for a small car
parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied.
Constraints:
•
0 <= big, medium, small <= 1000•
carType is 1, 2, or 3• At most
1000 calls will be made to addCar2023-05-30
705. Design HashSet
Topic: Array, Hash Table, Linked List, Design, Hash Function
Difficulty: Easy
Problem:
Design a HashSet without using any built-in hash table libraries.
Implement
•
•
•
Example 1:
Constraints:
•
• At most
705. Design HashSet
Topic: Array, Hash Table, Linked List, Design, Hash Function
Difficulty: Easy
Problem:
Design a HashSet without using any built-in hash table libraries.
Implement
MyHashSet class:•
void add(key) Inserts the value key into the HashSet.•
bool contains(key) Returns whether the value key exists in the HashSet or not.•
void remove(key) Removes the value key in the HashSet. If key does not exist in the HashSet, do nothing.Example 1:
Input
["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"]
[[], [1], [2], [1], [3], [2], [2], [2], [2]]
Output
[null, null, null, true, false, null, true, null, false]
Explanation
MyHashSet myHashSet = new MyHashSet();
myHashSet.add(1); // set = [1]
myHashSet.add(2); // set = [1, 2]
myHashSet.contains(1); // return True
myHashSet.contains(3); // return False, (not found)
myHashSet.add(2); // set = [1, 2]
myHashSet.contains(2); // return True
myHashSet.remove(2); // set = [1]
myHashSet.contains(2); // return False, (already removed)
Constraints:
•
0 <= key <= 10^6• At most
10^4 calls will be made to add, remove, and contains.2023-06-01
1091. Shortest Path in Binary Matrix
Topic: Array, Breadth-First Search, Matrix
Difficulty: Medium
Problem:
Given an
A clear path in a binary matrix is a path from the top-left cell (i.e.,
• All the visited cells of the path are
• All the adjacent cells of the path are 8-directionally connected (i.e., they are different and they share an edge or a corner).
The length of a clear path is the number of visited cells of this path.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/02/18/example1_1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/02/18/example2_1.png
Example 3:
Constraints:
•
•
•
•
1091. Shortest Path in Binary Matrix
Topic: Array, Breadth-First Search, Matrix
Difficulty: Medium
Problem:
Given an
n x n binary matrix grid, return the length of the shortest clear path in the matrix. If there is no clear path, return -1.A clear path in a binary matrix is a path from the top-left cell (i.e.,
(0, 0)) to the bottom-right cell (i.e., (n - 1, n - 1)) such that:• All the visited cells of the path are
0.• All the adjacent cells of the path are 8-directionally connected (i.e., they are different and they share an edge or a corner).
The length of a clear path is the number of visited cells of this path.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/02/18/example1_1.png
Input: grid = [[0,1],[1,0]]
Output: 2
Example 2:
Image: https://assets.leetcode.com/uploads/2021/02/18/example2_1.png
Input: grid = [[0,0,0],[1,1,0],[1,1,0]]
Output: 4
Example 3:
Input: grid = [[1,0,0],[1,1,0],[1,1,0]]
Output: -1
Constraints:
•
n == grid.length•
n == grid[i].length•
1 <= n <= 100•
grid[i][j] is 0 or 12023-06-02
2101. Detonate the Maximum Bombs
Topic: Array, Math, Depth-First Search, Breadth-First Search, Graph, Geometry
Difficulty: Medium
Problem:
You are given a list of bombs. The range of a bomb is defined as the area where its effect can be felt. This area is in the shape of a circle with the center as the location of the bomb.
The bombs are represented by a 0-indexed 2D integer array
You may choose to detonate a single bomb. When a bomb is detonated, it will detonate all bombs that lie in its range. These bombs will further detonate the bombs that lie in their ranges.
Given the list of
Example 1:
Image: https://assets.leetcode.com/uploads/2021/11/06/desmos-eg-3.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/11/06/desmos-eg-2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2021/11/07/desmos-eg1.png
Constraints:
•
•
•
2101. Detonate the Maximum Bombs
Topic: Array, Math, Depth-First Search, Breadth-First Search, Graph, Geometry
Difficulty: Medium
Problem:
You are given a list of bombs. The range of a bomb is defined as the area where its effect can be felt. This area is in the shape of a circle with the center as the location of the bomb.
The bombs are represented by a 0-indexed 2D integer array
bombs where bombs[i] = [x_i, y_i, r_i]. x_i and y_i denote the X-coordinate and Y-coordinate of the location of the i^th bomb, whereas r_i denotes the radius of its range.You may choose to detonate a single bomb. When a bomb is detonated, it will detonate all bombs that lie in its range. These bombs will further detonate the bombs that lie in their ranges.
Given the list of
bombs, return the maximum number of bombs that can be detonated if you are allowed to detonate only one bomb.Example 1:
Image: https://assets.leetcode.com/uploads/2021/11/06/desmos-eg-3.png
Input: bombs = [[2,1,3],[6,1,4]]
Output: 2
Explanation:
The above figure shows the positions and ranges of the 2 bombs.
If we detonate the left bomb, the right bomb will not be affected.
But if we detonate the right bomb, both bombs will be detonated.
So the maximum bombs that can be detonated is max(1, 2) = 2.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/11/06/desmos-eg-2.png
Input: bombs = [[1,1,5],[10,10,5]]
Output: 1
Explanation:
Detonating either bomb will not detonate the other bomb, so the maximum number of bombs that can be detonated is 1.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/11/07/desmos-eg1.png
Input: bombs = [[1,2,3],[2,3,1],[3,4,2],[4,5,3],[5,6,4]]
Output: 5
Explanation:
The best bomb to detonate is bomb 0 because:
- Bomb 0 detonates bombs 1 and 2. The red circle denotes the range of bomb 0.
- Bomb 2 detonates bomb 3. The blue circle denotes the range of bomb 2.
- Bomb 3 detonates bomb 4. The green circle denotes the range of bomb 3.
Thus all 5 bombs are detonated.
Constraints:
•
1 <= bombs.length <= 100•
bombs[i].length == 3•
1 <= x_i, y_i, r_i <= 10^52023-06-03
1376. Time Needed to Inform All Employees
Topic: Tree, Depth-First Search, Breadth-First Search
Difficulty: Medium
Problem:
A company has
Each employee has one direct manager given in the
The head of the company wants to inform all the company employees of an urgent piece of news. He will inform his direct subordinates, and they will inform their subordinates, and so on until all employees know about the urgent news.
The
Return the number of minutes needed to inform all the employees about the urgent news.
Example 1:
Example 2:
Image: https://assets.leetcode.com/uploads/2020/02/27/graph.png
Constraints:
•
•
•
•
•
•
•
•
• It is guaranteed that all the employees can be informed.
1376. Time Needed to Inform All Employees
Topic: Tree, Depth-First Search, Breadth-First Search
Difficulty: Medium
Problem:
A company has
n employees with a unique ID for each employee from 0 to n - 1. The head of the company is the one with headID.Each employee has one direct manager given in the
manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also, it is guaranteed that the subordination relationships have a tree structure.The head of the company wants to inform all the company employees of an urgent piece of news. He will inform his direct subordinates, and they will inform their subordinates, and so on until all employees know about the urgent news.
The
i-th employee needs informTime[i] minutes to inform all of his direct subordinates (i.e., After informTimei minutes, all his direct subordinates can start spreading the news).Return the number of minutes needed to inform all the employees about the urgent news.
Example 1:
Input: n = 1, headID = 0, manager = [-1], informTime = [0]
Output: 0
Explanation: The head of the company is the only employee in the company.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/02/27/graph.png
Input: n = 6, headID = 2, manager = [2,2,-1,2,2,2], informTime = [0,0,1,0,0,0]
Output: 1
Explanation: The head of the company with id = 2 is the direct manager of all the employees in the company and needs 1 minute to inform them all.
The tree structure of the employees in the company is shown.
Constraints:
•
1 <= n <= 10^5•
0 <= headID < n•
manager.length == n•
0 <= manager[i] < n•
manager[headID] == -1•
informTime.length == n•
0 <= informTime[i] <= 1000•
informTime[i] == 0 if employee i has no subordinates.• It is guaranteed that all the employees can be informed.
2023-06-04
547. Number of Provinces
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph
Difficulty: Medium
Problem:
There are
A province is a group of directly or indirectly connected cities and no other cities outside of the group.
You are given an
Return the total number of provinces.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/24/graph1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/24/graph2.jpg
Constraints:
•
•
•
•
•
•
547. Number of Provinces
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph
Difficulty: Medium
Problem:
There are
n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c.A province is a group of directly or indirectly connected cities and no other cities outside of the group.
You are given an
n x n matrix isConnected where isConnected[i][j] = 1 if the i^th city and the j^th city are directly connected, and isConnected[i][j] = 0 otherwise.Return the total number of provinces.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/24/graph1.jpg
Input: isConnected = [[1,1,0],[1,1,0],[0,0,1]]
Output: 2
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/24/graph2.jpg
Input: isConnected = [[1,0,0],[0,1,0],[0,0,1]]
Output: 3
Constraints:
•
1 <= n <= 200•
n == isConnected.length•
n == isConnected[i].length•
isConnected[i][j] is 1 or 0.•
isConnected[i][i] == 1•
isConnected[i][j] == isConnected[j][i]2023-06-05
1232. Check If It Is a Straight Line
Topic: Array, Math, Geometry
Difficulty: Easy
Problem:
You are given an array
Example 1:
Image: https://assets.leetcode.com/uploads/2019/10/15/untitled-diagram-2.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2019/10/09/untitled-diagram-1.jpg
Constraints:
•
•
•
•
1232. Check If It Is a Straight Line
Topic: Array, Math, Geometry
Difficulty: Easy
Problem:
You are given an array
coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.Example 1:
Image: https://assets.leetcode.com/uploads/2019/10/15/untitled-diagram-2.jpg
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
Output: true
Example 2:
Image: https://assets.leetcode.com/uploads/2019/10/09/untitled-diagram-1.jpg
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
Output: false
Constraints:
•
2 <= coordinates.length <= 1000•
coordinates[i].length == 2•
-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4•
coordinates contains no duplicate point.2023-06-06
1502. Can Make Arithmetic Progression From Sequence
Topic: Array, Sorting
Difficulty: Easy
Problem:
A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.
Given an array of numbers
Example 1:
Example 2:
Constraints:
•
•
1502. Can Make Arithmetic Progression From Sequence
Topic: Array, Sorting
Difficulty: Easy
Problem:
A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.
Given an array of numbers
arr, return true if the array can be rearranged to form an arithmetic progression. Otherwise, return false.Example 1:
Input: arr = [3,5,1]
Output: true
Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements.
Example 2:
Input: arr = [1,2,4]
Output: false
Explanation: There is no way to reorder the elements to obtain an arithmetic progression.
Constraints:
•
2 <= arr.length <= 1000•
-10^6 <= arr[i] <= 10^62023-06-07
1318. Minimum Flips to Make a OR b Equal to c
Topic: Bit Manipulation
Difficulty: Medium
Problem:
Given 3 positives numbers
Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/06/sample_3_1676.png
Example 2:
Example 3:
Constraints:
•
•
•
1318. Minimum Flips to Make a OR b Equal to c
Topic: Bit Manipulation
Difficulty: Medium
Problem:
Given 3 positives numbers
a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation).Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/06/sample_3_1676.png
Input: a = 2, b = 6, c = 5
Output: 3
Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c)
Example 2:
Input: a = 4, b = 2, c = 7
Output: 1
Example 3:
Input: a = 1, b = 2, c = 3
Output: 0
Constraints:
•
1 <= a <= 10^9•
1 <= b <= 10^9•
1 <= c <= 10^92023-06-08
1351. Count Negative Numbers in a Sorted Matrix
Topic: Array, Binary Search, Matrix
Difficulty: Easy
Problem:
Given a
Example 1:
Example 2:
Constraints:
•
•
•
•
Follow up: Could you find an
1351. Count Negative Numbers in a Sorted Matrix
Topic: Array, Binary Search, Matrix
Difficulty: Easy
Problem:
Given a
m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid.Example 1:
Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
Output: 8
Explanation: There are 8 negatives number in the matrix.
Example 2:
Input: grid = [[3,2],[1,0]]
Output: 0
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m, n <= 100•
-100 <= grid[i][j] <= 100Follow up: Could you find an
O(n + m) solution?2023-06-09
744. Find Smallest Letter Greater Than Target
Topic: Array, Binary Search
Difficulty: Easy
Problem:
You are given an array of characters
Return the smallest character in
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
744. Find Smallest Letter Greater Than Target
Topic: Array, Binary Search
Difficulty: Easy
Problem:
You are given an array of characters
letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters.Return the smallest character in
letters that is lexicographically greater than target. If such a character does not exist, return the first character in letters.Example 1:
Input: letters = ["c","f","j"], target = "a"
Output: "c"
Explanation: The smallest character that is lexicographically greater than 'a' in letters is 'c'.
Example 2:
Input: letters = ["c","f","j"], target = "c"
Output: "f"
Explanation: The smallest character that is lexicographically greater than 'c' in letters is 'f'.
Example 3:
Input: letters = ["x","x","y","y"], target = "z"
Output: "x"
Explanation: There are no characters in letters that is lexicographically greater than 'z' so we return letters[0].
Constraints:
•
2 <= letters.length <= 10^4•
letters[i] is a lowercase English letter.•
letters is sorted in non-decreasing order.•
letters contains at least two different characters.•
target is a lowercase English letter.2023-06-10
1802. Maximum Value at a Given Index in a Bounded Array
Topic: Binary Search, Greedy
Difficulty: Medium
Problem:
You are given three positive integers:
•
•
•
• The sum of all the elements of
•
Return
Note that
Example 1:
Example 2:
Constraints:
•
•
1802. Maximum Value at a Given Index in a Bounded Array
Topic: Binary Search, Greedy
Difficulty: Medium
Problem:
You are given three positive integers:
n, index, and maxSum. You want to construct an array nums (0-indexed) that satisfies the following conditions:•
nums.length == n•
nums[i] is a positive integer where 0 <= i < n.•
abs(nums[i] - nums[i+1]) <= 1 where 0 <= i < n-1.• The sum of all the elements of
nums does not exceed maxSum.•
nums[index] is maximized.Return
nums[index] of the constructed array.Note that
abs(x) equals x if x >= 0, and -x otherwise.Example 1:
Input: n = 4, index = 2, maxSum = 6
Output: 2
Explanation: nums = [1,2,2,1] is one array that satisfies all the conditions.
There are no arrays that satisfy all the conditions and have nums[2] == 3, so 2 is the maximum nums[2].
Example 2:
Input: n = 6, index = 1, maxSum = 10
Output: 3
Constraints:
•
1 <= n <= maxSum <= 10^9•
0 <= index < n2023-06-11
1146. Snapshot Array
Topic: Array, Hash Table, Binary Search, Design
Difficulty: Medium
Problem:
Implement a SnapshotArray that supports the following interface:
•
•
•
•
Example 1:
Constraints:
•
•
•
•
• At most
1146. Snapshot Array
Topic: Array, Hash Table, Binary Search, Design
Difficulty: Medium
Problem:
Implement a SnapshotArray that supports the following interface:
•
SnapshotArray(int length) initializes an array-like data structure with the given length. Initially, each element equals 0.•
void set(index, val) sets the element at the given index to be equal to val.•
int snap() takes a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.•
int get(index, snap_id) returns the value at the given index, at the time we took the snapshot with the given snap_idExample 1:
Input: ["SnapshotArray","set","snap","set","get"]
[[3],[0,5],[],[0,6],[0,0]]
Output: [null,null,0,null,5]
Explanation:
SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3
snapshotArr.set(0,5); // Set array[0] = 5
snapshotArr.snap(); // Take a snapshot, return snap_id = 0
snapshotArr.set(0,6);
snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5
Constraints:
•
1 <= length <= 5 * 10^4•
0 <= index < length•
0 <= val <= 10^9•
0 <= snap_id < (the total number of times we call snap())• At most
5 * 10^4 calls will be made to set, snap, and get.2023-06-12
228. Summary Ranges
Topic: Array
Difficulty: Easy
Problem:
You are given a sorted unique integer array
A range
Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of
Each range
•
•
Example 1:
Example 2:
Constraints:
•
•
• All the values of
•
228. Summary Ranges
Topic: Array
Difficulty: Easy
Problem:
You are given a sorted unique integer array
nums.A range
[a,b] is the set of all integers from a to b (inclusive).Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of
nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.Each range
[a,b] in the list should be output as:•
"a->b" if a != b•
"a" if a == bExample 1:
Input: nums = [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
Example 2:
Input: nums = [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
Constraints:
•
0 <= nums.length <= 20•
-2^31 <= nums[i] <= 2^31 - 1• All the values of
nums are unique.•
nums is sorted in ascending order.2023-06-13
2352. Equal Row and Column Pairs
Topic: Array, Hash Table, Matrix, Simulation
Difficulty: Medium
Problem:
Given a 0-indexed
A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array).
Example 1:
Image: https://assets.leetcode.com/uploads/2022/06/01/ex1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2022/06/01/ex2.jpg
Constraints:
•
•
•
2352. Equal Row and Column Pairs
Topic: Array, Hash Table, Matrix, Simulation
Difficulty: Medium
Problem:
Given a 0-indexed
n x n integer matrix grid, return the number of pairs (r_i, c_j) such that row r_i and column c_j are equal.A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array).
Example 1:
Image: https://assets.leetcode.com/uploads/2022/06/01/ex1.jpg
Input: grid = [[3,2,1],[1,7,6],[2,7,7]]
Output: 1
Explanation: There is 1 equal row and column pair:
- (Row 2, Column 1): [2,7,7]
Example 2:
Image: https://assets.leetcode.com/uploads/2022/06/01/ex2.jpg
Input: grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
Output: 3
Explanation: There are 3 equal row and column pairs:
- (Row 0, Column 0): [3,1,2,2]
- (Row 2, Column 2): [2,4,2,2]
- (Row 3, Column 2): [2,4,2,2]
Constraints:
•
n == grid.length == grid[i].length•
1 <= n <= 200•
1 <= grid[i][j] <= 10^52023-06-14
530. Minimum Absolute Difference in BST
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Search Tree, Binary Tree
Difficulty: Easy
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg
Constraints:
• The number of nodes in the tree is in the range
•
Note: This question is the same as 783: <https://leetcode.com/problems/minimum-distance-between-bst-nodes/>
530. Minimum Absolute Difference in BST
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Search Tree, Binary Tree
Difficulty: Easy
Problem:
Given the
root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.Example 1:
Image: https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg
Input: root = [4,2,6,1,3]
Output: 1
Example 2:
Image: https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg
Input: root = [1,0,48,null,null,12,49]
Output: 1
Constraints:
• The number of nodes in the tree is in the range
[2, 10^4].•
0 <= Node.val <= 10^5Note: This question is the same as 783: <https://leetcode.com/problems/minimum-distance-between-bst-nodes/>
2023-06-15
1161. Maximum Level Sum of a Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
Return the smallest level
Example 1:
Image: https://assets.leetcode.com/uploads/2019/05/03/capture.JPG
Example 2:
Constraints:
• The number of nodes in the tree is in the range
•
1161. Maximum Level Sum of a Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.Return the smallest level
x such that the sum of all the values of nodes at level x is maximal.Example 1:
Image: https://assets.leetcode.com/uploads/2019/05/03/capture.JPG
Input: root = [1,7,0,7,-8,null,null]
Output: 2
Explanation:
Level 1 sum = 1.
Level 2 sum = 7 + 0 = 7.
Level 3 sum = 7 + -8 = -1.
So we return the level with the maximum sum which is level 2.
Example 2:
Input: root = [989,null,10250,98693,-89388,null,null,null,-32127]
Output: 2
Constraints:
• The number of nodes in the tree is in the range
[1, 10^4].•
-10^5 <= Node.val <= 10^52023-06-16
1569. Number of Ways to Reorder Array to Get Same BST
Topic: Array, Math, Divide and Conquer, Dynamic Programming, Tree, Union Find, Binary Search Tree, Memoization, Combinatorics, Binary Tree
Difficulty: Hard
Problem:
Given an array
• For example, given
Return the number of ways to reorder
Since the answer may be very large, return it modulo
Example 1:
Image: https://assets.leetcode.com/uploads/2020/08/12/bb.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/08/12/ex1.png
Example 3:
Image: https://assets.leetcode.com/uploads/2020/08/12/ex4.png
Constraints:
•
•
• All integers in
1569. Number of Ways to Reorder Array to Get Same BST
Topic: Array, Math, Divide and Conquer, Dynamic Programming, Tree, Union Find, Binary Search Tree, Memoization, Combinatorics, Binary Tree
Difficulty: Hard
Problem:
Given an array
nums that represents a permutation of integers from 1 to n. We are going to construct a binary search tree (BST) by inserting the elements of nums in order into an initially empty BST. Find the number of different ways to reorder nums so that the constructed BST is identical to that formed from the original array nums.• For example, given
nums = [2,1,3], we will have 2 as the root, 1 as a left child, and 3 as a right child. The array [2,3,1] also yields the same BST but [3,2,1] yields a different BST.Return the number of ways to reorder
nums such that the BST formed is identical to the original BST formed from nums.Since the answer may be very large, return it modulo
10^9 + 7.Example 1:
Image: https://assets.leetcode.com/uploads/2020/08/12/bb.png
Input: nums = [2,1,3]
Output: 1
Explanation: We can reorder nums to be [2,3,1] which will yield the same BST. There are no other ways to reorder nums which will yield the same BST.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/08/12/ex1.png
Input: nums = [3,4,5,1,2]
Output: 5
Explanation: The following 5 arrays will yield the same BST:
[3,1,2,4,5]
[3,1,4,2,5]
[3,1,4,5,2]
[3,4,1,2,5]
[3,4,1,5,2]
Example 3:
Image: https://assets.leetcode.com/uploads/2020/08/12/ex4.png
Input: nums = [1,2,3]
Output: 0
Explanation: There are no other orderings of nums that will yield the same BST.
Constraints:
•
1 <= nums.length <= 1000•
1 <= nums[i] <= nums.length• All integers in
nums are distinct.2023-06-17
1187. Make Array Strictly Increasing
Topic: Array, Binary Search, Dynamic Programming, Sorting
Difficulty: Hard
Problem:
Given two integer arrays
In one operation, you can choose two indices
If there is no way to make
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1187. Make Array Strictly Increasing
Topic: Array, Binary Search, Dynamic Programming, Sorting
Difficulty: Hard
Problem:
Given two integer arrays
arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing.In one operation, you can choose two indices
0 <= i < arr1.length and 0 <= j < arr2.length and do the assignment arr1[i] = arr2[j].If there is no way to make
arr1 strictly increasing, return -1.Example 1:
Input: arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
Output: 1
Explanation: Replace 5 with 2, then arr1 = [1, 2, 3, 6, 7].
Example 2:
Input: arr1 = [1,5,3,6,7], arr2 = [4,3,1]
Output: 2
Explanation: Replace 5 with 3 and then replace 3 with 4. arr1 = [1, 3, 4, 6, 7].
Example 3:
Input: arr1 = [1,5,3,6,7], arr2 = [1,6,3,3]
Output: -1
Explanation: You can't make arr1 strictly increasing.
Constraints:
•
1 <= arr1.length, arr2.length <= 2000•
0 <= arr1[i], arr2[i] <= 10^92023-06-18
2328. Number of Increasing Paths in a Grid
Topic: Array, Dynamic Programming, Depth-First Search, Breadth-First Search, Graph, Topological Sort, Memoization, Matrix
Difficulty: Hard
Problem:
You are given an
Return the number of strictly increasing paths in the grid such that you can start from any cell and end at any cell. Since the answer may be very large, return it modulo
Two paths are considered different if they do not have exactly the same sequence of visited cells.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/05/10/griddrawio-4.png
Example 2:
Constraints:
•
•
•
•
•
2328. Number of Increasing Paths in a Grid
Topic: Array, Dynamic Programming, Depth-First Search, Breadth-First Search, Graph, Topological Sort, Memoization, Matrix
Difficulty: Hard
Problem:
You are given an
m x n integer matrix grid, where you can move from a cell to any adjacent cell in all 4 directions.Return the number of strictly increasing paths in the grid such that you can start from any cell and end at any cell. Since the answer may be very large, return it modulo
10^9 + 7.Two paths are considered different if they do not have exactly the same sequence of visited cells.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/05/10/griddrawio-4.png
Input: grid = [[1,1],[3,4]]
Output: 8
Explanation: The strictly increasing paths are:
- Paths with length 1: [1], [1], [3], [4].
- Paths with length 2: [1 -> 3], [1 -> 4], [3 -> 4].
- Paths with length 3: [1 -> 3 -> 4].
The total number of paths is 4 + 3 + 1 = 8.
Example 2:
Input: grid = [[1],[2]]
Output: 3
Explanation: The strictly increasing paths are:
- Paths with length 1: [1], [2].
- Paths with length 2: [1 -> 2].
The total number of paths is 2 + 1 = 3.
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m, n <= 1000•
1 <= m * n <= 10^5•
1 <= grid[i][j] <= 10^52023-06-19
1732. Find the Highest Altitude
Topic: Array, Prefix Sum
Difficulty: Easy
Problem:
There is a biker going on a road trip. The road trip consists of
You are given an integer array
Example 1:
Example 2:
Constraints:
•
•
•
1732. Find the Highest Altitude
Topic: Array, Prefix Sum
Difficulty: Easy
Problem:
There is a biker going on a road trip. The road trip consists of
n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0.You are given an integer array
gain of length n where gain[i] is the net gain in altitude between points i and i + 1 for all (0 <= i < n). Return the highest altitude of a point.Example 1:
Input: gain = [-5,1,5,0,-7]
Output: 1
Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
Example 2:
Input: gain = [-4,-3,-2,-1,4,3,2]
Output: 0
Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
Constraints:
•
n == gain.length•
1 <= n <= 100•
-100 <= gain[i] <= 100