2023-05-19
785. Is Graph Bipartite?
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph
Difficulty: Medium
Problem:
There is an undirected graph with
• There are no self-edges (
• There are no parallel edges (
• If
• The graph may not be connected, meaning there may be two nodes
A graph is bipartite if the nodes can be partitioned into two independent sets
Return
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/21/bi2.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/10/21/bi1.jpg
Constraints:
•
•
•
•
•
• All the values of
• If
785. Is Graph Bipartite?
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph
Difficulty: Medium
Problem:
There is an undirected graph with
n nodes, where each node is numbered between 0 and n - 1. You are given a 2D array graph, where graph[u] is an array of nodes that node u is adjacent to. More formally, for each v in graph[u], there is an undirected edge between node u and node v. The graph has the following properties:• There are no self-edges (
graph[u] does not contain u).• There are no parallel edges (
graph[u] does not contain duplicate values).• If
v is in graph[u], then u is in graph[v] (the graph is undirected).• The graph may not be connected, meaning there may be two nodes
u and v such that there is no path between them.A graph is bipartite if the nodes can be partitioned into two independent sets
A and B such that every edge in the graph connects a node in set A and a node in set B.Return
true if and only if it is bipartite.Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/21/bi2.jpg
Input: graph = [[1,2,3],[0,2],[0,1,3],[0,2]]
Output: false
Explanation: There is no way to partition the nodes into two independent sets such that every edge connects a node in one and a node in the other.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/10/21/bi1.jpg
Input: graph = [[1,3],[0,2],[1,3],[0,2]]
Output: true
Explanation: We can partition the nodes into two sets: {0, 2} and {1, 3}.
Constraints:
•
graph.length == n•
1 <= n <= 100•
0 <= graph[u].length < n•
0 <= graph[u][i] <= n - 1•
graph[u] does not contain u.• All the values of
graph[u] are unique.• If
graph[u] contains v, then graph[v] contains u.2023-05-20
399. Evaluate Division
Topic: Array, Depth-First Search, Breadth-First Search, Union Find, Graph, Shortest Path
Difficulty: Medium
Problem:
You are given an array of variable pairs
You are also given some
Return the answers to all queries. If a single answer cannot be determined, return
Note: The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
•
•
•
•
399. Evaluate Division
Topic: Array, Depth-First Search, Breadth-First Search, Union Find, Graph, Shortest Path
Difficulty: Medium
Problem:
You are given an array of variable pairs
equations and an array of real numbers values, where equations[i] = [A_i, B_i] and values[i] represent the equation A_i / B_i = values[i]. Each A_i or B_i is a string that represents a single variable.You are also given some
queries, where queries[j] = [C_j, D_j] represents the j^th query where you must find the answer for C_j / D_j = ?.Return the answers to all queries. If a single answer cannot be determined, return
-1.0.Note: The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.
Example 1:
Input: equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
Output: [6.00000,0.50000,-1.00000,1.00000,-1.00000]
Explanation:
Given: a / b = 2.0, b / c = 3.0
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
Example 2:
Input: equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
Output: [3.75000,0.40000,5.00000,0.20000]
Example 3:
Input: equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
Output: [0.50000,2.00000,-1.00000,-1.00000]
Constraints:
•
1 <= equations.length <= 20•
equations[i].length == 2•
1 <= A_i.length, B_i.length <= 5•
values.length == equations.length•
0.0 < values[i] <= 20.0•
1 <= queries.length <= 20•
queries[i].length == 2•
1 <= C_j.length, D_j.length <= 5•
A_i, B_i, C_j, D_j consist of lower case English letters and digits.2023-05-21
934. Shortest Bridge
Topic: Array, Depth-First Search, Breadth-First Search, Matrix
Difficulty: Medium
Problem:
You are given an
An island is a 4-directionally connected group of
You may change
Return the smallest number of
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
• There are exactly two islands in
934. Shortest Bridge
Topic: Array, Depth-First Search, Breadth-First Search, Matrix
Difficulty: Medium
Problem:
You are given an
n x n binary matrix grid where 1 represents land and 0 represents water.An island is a 4-directionally connected group of
1's not connected to any other 1's. There are exactly two islands in grid.You may change
0's to 1's to connect the two islands to form one island.Return the smallest number of
0's you must flip to connect the two islands.Example 1:
Input: grid = [[0,1],[1,0]]
Output: 1
Example 2:
Input: grid = [[0,1,0],[0,0,0],[0,0,1]]
Output: 2
Example 3:
Input: grid = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
Output: 1
Constraints:
•
n == grid.length == grid[i].length•
2 <= n <= 100•
grid[i][j] is either 0 or 1.• There are exactly two islands in
grid.2023-05-22
347. Top K Frequent Elements
Topic: Array, Hash Table, Divide and Conquer, Sorting, Heap (Priority Queue), Bucket Sort, Counting, Quickselect
Difficulty: Medium
Problem:
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
•
• It is guaranteed that the answer is unique.
Follow up: Your algorithm's time complexity must be better than
347. Top K Frequent Elements
Topic: Array, Hash Table, Divide and Conquer, Sorting, Heap (Priority Queue), Bucket Sort, Counting, Quickselect
Difficulty: Medium
Problem:
Given an integer array
nums and an integer k, return the k most frequent elements. You may return the answer in any order.Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Example 2:
Input: nums = [1], k = 1
Output: [1]
Constraints:
•
1 <= nums.length <= 10^5•
-10^4 <= nums[i] <= 10^4•
k is in the range [1, the number of unique elements in the array].• It is guaranteed that the answer is unique.
Follow up: Your algorithm's time complexity must be better than
O(n log n), where n is the array's size.2023-05-23
703. Kth Largest Element in a Stream
Topic: Tree, Design, Binary Search Tree, Heap (Priority Queue), Binary Tree, Data Stream
Difficulty: Easy
Problem:
Design a class to find the
Implement
•
•
Example 1:
Constraints:
•
•
•
•
• At most
• It is guaranteed that there will be at least
703. Kth Largest Element in a Stream
Topic: Tree, Design, Binary Search Tree, Heap (Priority Queue), Binary Tree, Data Stream
Difficulty: Easy
Problem:
Design a class to find the
k^th largest element in a stream. Note that it is the k^th largest element in the sorted order, not the k^th distinct element.Implement
KthLargest class:•
KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums.•
int add(int val) Appends the integer val to the stream and returns the element representing the k^th largest element in the stream.Example 1:
Input
["KthLargest", "add", "add", "add", "add", "add"]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
Output
[null, 4, 5, 5, 8, 8]
Explanation
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // return 4
kthLargest.add(5); // return 5
kthLargest.add(10); // return 5
kthLargest.add(9); // return 8
kthLargest.add(4); // return 8
Constraints:
•
1 <= k <= 10^4•
0 <= nums.length <= 10^4•
-10^4 <= nums[i] <= 10^4•
-10^4 <= val <= 10^4• At most
10^4 calls will be made to add.• It is guaranteed that there will be at least
k elements in the array when you search for the k^th element.2023-05-24
2542. Maximum Subsequence Score
Topic: Array, Greedy, Sorting, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given two 0-indexed integer arrays
For chosen indices
• The sum of the selected elements from
• It can defined simply as:
Return the maximum possible score.
A subsequence of indices of an array is a set that can be derived from the set
Example 1:
Example 2:
Constraints:
•
•
•
•
2542. Maximum Subsequence Score
Topic: Array, Greedy, Sorting, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given two 0-indexed integer arrays
nums1 and nums2 of equal length n and a positive integer k. You must choose a subsequence of indices from nums1 of length k.For chosen indices
i_0, i_1, ..., i_k - 1, your score is defined as:• The sum of the selected elements from
nums1 multiplied with the minimum of the selected elements from nums2.• It can defined simply as:
(nums1[i_0] + nums1[i_1] +...+ nums1[i_k - 1]) * min(nums2[i_0] , nums2[i_1], ... ,nums2[i_k - 1]).Return the maximum possible score.
A subsequence of indices of an array is a set that can be derived from the set
{0, 1, ..., n-1} by deleting some or no elements.Example 1:
Input: nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3
Output: 12
Explanation:
The four possible subsequence scores are:
- We choose the indices 0, 1, and 2 with score = (1+3+3) * min(2,1,3) = 7.
- We choose the indices 0, 1, and 3 with score = (1+3+2) * min(2,1,4) = 6.
- We choose the indices 0, 2, and 3 with score = (1+3+2) * min(2,3,4) = 12.
- We choose the indices 1, 2, and 3 with score = (3+3+2) * min(1,3,4) = 8.
Therefore, we return the max score, which is 12.
Example 2:
Input: nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1
Output: 30
Explanation:
Choosing index 2 is optimal: nums1[2] * nums2[2] = 3 * 10 = 30 is the maximum possible score.
Constraints:
•
n == nums1.length == nums2.length•
1 <= n <= 10^5•
0 <= nums1[i], nums2[j] <= 10^5•
1 <= k <= n2023-05-25
837. New 21 Game
Topic: Math, Dynamic Programming, Sliding Window, Probability and Statistics
Difficulty: Medium
Problem:
Alice plays the following game, loosely based on the card game "21".
Alice starts with
Alice stops drawing numbers when she gets
Return the probability that Alice has
Answers within
Example 1:
Example 2:
Example 3:
Constraints:
•
•
837. New 21 Game
Topic: Math, Dynamic Programming, Sliding Window, Probability and Statistics
Difficulty: Medium
Problem:
Alice plays the following game, loosely based on the card game "21".
Alice starts with
0 points and draws numbers while she has less than k points. During each draw, she gains an integer number of points randomly from the range [1, maxPts], where maxPts is an integer. Each draw is independent and the outcomes have equal probabilities.Alice stops drawing numbers when she gets
k or more points.Return the probability that Alice has
n or fewer points.Answers within
10^-5 of the actual answer are considered accepted.Example 1:
Input: n = 10, k = 1, maxPts = 10
Output: 1.00000
Explanation: Alice gets a single card, then stops.
Example 2:
Input: n = 6, k = 1, maxPts = 10
Output: 0.60000
Explanation: Alice gets a single card, then stops.
In 6 out of 10 possibilities, she is at or below 6 points.
Example 3:
Input: n = 21, k = 17, maxPts = 10
Output: 0.73278
Constraints:
•
0 <= k <= n <= 10^4•
1 <= maxPts <= 10^42023-05-26
1140. Stone Game II
Topic: Array, Math, Dynamic Programming, Game Theory
Difficulty: Medium
Problem:
Alice and Bob continue their games with piles of stones. There are a number of piles arranged in a row, and each pile has a positive integer number of stones
Alice and Bob take turns, with Alice starting first. Initially,
On each player's turn, that player can take all the stones in the first
The game continues until all the stones have been taken.
Assuming Alice and Bob play optimally, return the maximum number of stones Alice can get.
Example 1:
Example 2:
Constraints:
•
•
1140. Stone Game II
Topic: Array, Math, Dynamic Programming, Game Theory
Difficulty: Medium
Problem:
Alice and Bob continue their games with piles of stones. There are a number of piles arranged in a row, and each pile has a positive integer number of stones
piles[i]. The objective of the game is to end with the most stones. Alice and Bob take turns, with Alice starting first. Initially,
M = 1.On each player's turn, that player can take all the stones in the first
X remaining piles, where 1 <= X <= 2M. Then, we set M = max(M, X).The game continues until all the stones have been taken.
Assuming Alice and Bob play optimally, return the maximum number of stones Alice can get.
Example 1:
Input: piles = [2,7,9,4,4]
Output: 10
Explanation: If Alice takes one pile at the beginning, Bob takes two piles, then Alice takes 2 piles again. Alice can get 2 + 4 + 4 = 10 piles in total. If Alice takes two piles at the beginning, then Bob can take all three piles left. In this case, Alice get 2 + 7 = 9 piles in total. So we return 10 since it's larger.
Example 2:
Input: piles = [1,2,3,4,5,100]
Output: 104
Constraints:
•
1 <= piles.length <= 100•
1 <= piles[i] <= 10^42023-05-27
1406. Stone Game III
Topic: Array, Math, Dynamic Programming, Game Theory
Difficulty: Hard
Problem:
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take
The score of each player is the sum of the values of the stones taken. The score of each player is
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1406. Stone Game III
Topic: Array, Math, Dynamic Programming, Game Theory
Difficulty: Hard
Problem:
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array
stoneValue.Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take
1, 2, or 3 stones from the first remaining stones in the row.The score of each player is the sum of the values of the stones taken. The score of each player is
0 initially.The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return
"Alice" if Alice will win, "Bob" if Bob will win, or "Tie" if they will end the game with the same score.Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. In the next move, Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. In the next move, Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Constraints:
•
1 <= stoneValue.length <= 5 * 10^4•
-1000 <= stoneValue[i] <= 10002023-05-28
1547. Minimum Cost to Cut a Stick
Topic: Array, Dynamic Programming, Sorting
Difficulty: Hard
Problem:
Given a wooden stick of length
Image: https://assets.leetcode.com/uploads/2020/07/21/statement.jpg
Given an integer array
You should perform the cuts in order, you can change the order of the cuts as you wish.
The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation.
Return the minimum total cost of the cuts.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/07/23/e1.jpg
Example 2:
Constraints:
•
•
•
• All the integers in
1547. Minimum Cost to Cut a Stick
Topic: Array, Dynamic Programming, Sorting
Difficulty: Hard
Problem:
Given a wooden stick of length
n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows:Image: https://assets.leetcode.com/uploads/2020/07/21/statement.jpg
Given an integer array
cuts where cuts[i] denotes a position you should perform a cut at.You should perform the cuts in order, you can change the order of the cuts as you wish.
The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation.
Return the minimum total cost of the cuts.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/07/23/e1.jpg
Input: n = 7, cuts = [1,3,4,5]
Output: 16
Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario:
Image: [https://assets.leetcode.com/uploads/2020/07/21/e11.jpg](https://assets.leetcode.com/uploads/2020/07/21/e11.jpg)
The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20.
Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16).
Example 2:
Input: n = 9, cuts = [5,6,1,4,2]
Output: 22
Explanation: If you try the given cuts ordering the cost will be 25.
There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.
Constraints:
•
2 <= n <= 10^6•
1 <= cuts.length <= min(n - 1, 100)•
1 <= cuts[i] <= n - 1• All the integers in
cuts array are distinct.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?