2024-02-08
279. Perfect Squares
Topic: Math, Dynamic Programming, Breadth-First Search
Difficulty: Medium
Problem:
Given an integer
A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example,
Example 1:
Example 2:
Constraints:
•
279. Perfect Squares
Topic: Math, Dynamic Programming, Breadth-First Search
Difficulty: Medium
Problem:
Given an integer
n, return the least number of perfect square numbers that sum to n.A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example,
1, 4, 9, and 16 are perfect squares while 3 and 11 are not.Example 1:
Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.
Example 2:
Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.
Constraints:
•
1 <= n <= 10^42024-02-09
368. Largest Divisible Subset
Topic: Array, Math, Dynamic Programming, Sorting
Difficulty: Medium
Problem:
Given a set of distinct positive integers
•
•
If there are multiple solutions, return any of them.
Example 1:
Example 2:
Constraints:
•
•
• All the integers in
368. Largest Divisible Subset
Topic: Array, Math, Dynamic Programming, Sorting
Difficulty: Medium
Problem:
Given a set of distinct positive integers
nums, return the largest subset answer such that every pair (answer[i], answer[j]) of elements in this subset satisfies:•
answer[i] % answer[j] == 0, or•
answer[j] % answer[i] == 0If there are multiple solutions, return any of them.
Example 1:
Input: nums = [1,2,3]
Output: [1,2]
Explanation: [1,3] is also accepted.
Example 2:
Input: nums = [1,2,4,8]
Output: [1,2,4,8]
Constraints:
•
1 <= nums.length <= 1000•
1 <= nums[i] <= 2 * 10^9• All the integers in
nums are unique.2024-02-10
647. Palindromic Substrings
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given a string
A string is a palindrome when it reads the same backward as forward.
A substring is a contiguous sequence of characters within the string.
Example 1:
Example 2:
Constraints:
•
•
647. Palindromic Substrings
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given a string
s, return the number of palindromic substrings in it.A string is a palindrome when it reads the same backward as forward.
A substring is a contiguous sequence of characters within the string.
Example 1:
Input: s = "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: s = "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Constraints:
•
1 <= s.length <= 1000•
s consists of lowercase English letters.2024-02-11
1463. Cherry Pickup II
Topic: Array, Dynamic Programming, Matrix
Difficulty: Hard
Problem:
You are given a
You have two robots that can collect cherries for you:
• Robot #1 is located at the top-left corner
• Robot #2 is located at the top-right corner
Return the maximum number of cherries collection using both robots by following the rules below:
• From a cell
• When any robot passes through a cell, It picks up all cherries, and the cell becomes an empty cell.
• When both robots stay in the same cell, only one takes the cherries.
• Both robots cannot move outside of the grid at any moment.
• Both robots should reach the bottom row in
Example 1:
Image: https://assets.leetcode.com/uploads/2020/04/29/sample_1_1802.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/04/23/sample_2_1802.png
Constraints:
•
•
•
•
1463. Cherry Pickup II
Topic: Array, Dynamic Programming, Matrix
Difficulty: Hard
Problem:
You are given a
rows x cols matrix grid representing a field of cherries where grid[i][j] represents the number of cherries that you can collect from the (i, j) cell.You have two robots that can collect cherries for you:
• Robot #1 is located at the top-left corner
(0, 0), and• Robot #2 is located at the top-right corner
(0, cols - 1).Return the maximum number of cherries collection using both robots by following the rules below:
• From a cell
(i, j), robots can move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1).• When any robot passes through a cell, It picks up all cherries, and the cell becomes an empty cell.
• When both robots stay in the same cell, only one takes the cherries.
• Both robots cannot move outside of the grid at any moment.
• Both robots should reach the bottom row in
grid.Example 1:
Image: https://assets.leetcode.com/uploads/2020/04/29/sample_1_1802.png
Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]
Output: 24
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12.
Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12.
Total of cherries: 12 + 12 = 24.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/04/23/sample_2_1802.png
Input: grid = [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]]
Output: 28
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17.
Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11.
Total of cherries: 17 + 11 = 28.
Constraints:
•
rows == grid.length•
cols == grid[i].length•
2 <= rows, cols <= 70•
0 <= grid[i][j] <= 1002024-02-12
169. Majority Element
Topic: Array, Hash Table, Divide and Conquer, Sorting, Counting
Difficulty: Easy
Problem:
Given an array
The majority element is the element that appears more than
Example 1:
Example 2:
Constraints:
•
•
•
Follow-up: Could you solve the problem in linear time and in
169. Majority Element
Topic: Array, Hash Table, Divide and Conquer, Sorting, Counting
Difficulty: Easy
Problem:
Given an array
nums of size n, return the majority element.The majority element is the element that appears more than
⌊n / 2⌋ times. You may assume that the majority element always exists in the array.Example 1:
Input: nums = [3,2,3]
Output: 3
Example 2:
Input: nums = [2,2,1,1,1,2,2]
Output: 2
Constraints:
•
n == nums.length•
1 <= n <= 5 * 10^4•
-10^9 <= nums[i] <= 10^9Follow-up: Could you solve the problem in linear time and in
O(1) space?2024-02-13
2108. Find First Palindromic String in the Array
Topic: Array, Two Pointers, String
Difficulty: Easy
Problem:
Given an array of strings
A string is palindromic if it reads the same forward and backward.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
2108. Find First Palindromic String in the Array
Topic: Array, Two Pointers, String
Difficulty: Easy
Problem:
Given an array of strings
words, return the first palindromic string in the array. If there is no such string, return an empty string "".A string is palindromic if it reads the same forward and backward.
Example 1:
Input: words = ["abc","car","ada","racecar","cool"]
Output: "ada"
Explanation: The first string that is palindromic is "ada".
Note that "racecar" is also palindromic, but it is not the first.
Example 2:
Input: words = ["notapalindrome","racecar"]
Output: "racecar"
Explanation: The first and only string that is palindromic is "racecar".
Example 3:
Input: words = ["def","ghi"]
Output: ""
Explanation: There are no palindromic strings, so the empty string is returned.
Constraints:
•
1 <= words.length <= 100•
1 <= words[i].length <= 100•
words[i] consists only of lowercase English letters.2024-02-14
2149. Rearrange Array Elements by Sign
Topic: Array, Two Pointers, Simulation
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
You should rearrange the elements of
1. Every consecutive pair of integers have opposite signs.
2. For all integers with the same sign, the order in which they were present in
3. The rearranged array begins with a positive integer.
Return the modified array after rearranging the elements to satisfy the aforementioned conditions.
Example 1:
Example 2:
Constraints:
•
•
•
•
2149. Rearrange Array Elements by Sign
Topic: Array, Two Pointers, Simulation
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
nums of even length consisting of an equal number of positive and negative integers.You should rearrange the elements of
nums such that the modified array follows the given conditions:1. Every consecutive pair of integers have opposite signs.
2. For all integers with the same sign, the order in which they were present in
nums is preserved.3. The rearranged array begins with a positive integer.
Return the modified array after rearranging the elements to satisfy the aforementioned conditions.
Example 1:
Input: nums = [3,1,-2,-5,2,-4]
Output: [3,-2,1,-5,2,-4]
Explanation:
The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4].
The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4].
Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions.
Example 2:
Input: nums = [-1,1]
Output: [1,-1]
Explanation:
1 is the only positive integer and -1 the only negative integer in nums.
So nums is rearranged to [1,-1].
Constraints:
•
2 <= nums.length <= 2 * 10^5•
nums.length is even•
1 <= |nums[i]| <= 10^5•
nums consists of equal number of positive and negative integers.2024-02-15
2971. Find Polygon With the Largest Perimeter
Topic: Array, Greedy, Sorting, Prefix Sum
Difficulty: Medium
Problem:
You are given an array of positive integers
A polygon is a closed plane figure that has at least
Conversely, if you have
The perimeter of a polygon is the sum of lengths of its sides.
Return the largest possible perimeter of a polygon whose sides can be formed from
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2971. Find Polygon With the Largest Perimeter
Topic: Array, Greedy, Sorting, Prefix Sum
Difficulty: Medium
Problem:
You are given an array of positive integers
nums of length n.A polygon is a closed plane figure that has at least
3 sides. The longest side of a polygon is smaller than the sum of its other sides.Conversely, if you have
k (k >= 3) positive real numbers a_1, a_2, a_3, ..., a_k where a_1 <= a_2 <= a_3 <= ... <= a_k and a_1 + a_2 + a_3 + ... + a_k-1 > a_k, then there always exists a polygon with k sides whose lengths are a_1, a_2, a_3, ..., a_k.The perimeter of a polygon is the sum of lengths of its sides.
Return the largest possible perimeter of a polygon whose sides can be formed from
nums, or -1 if it is not possible to create a polygon.Example 1:
Input: nums = [5,5,5]
Output: 15
Explanation: The only possible polygon that can be made from nums has 3 sides: 5, 5, and 5. The perimeter is 5 + 5 + 5 = 15.
Example 2:
Input: nums = [1,12,1,2,5,50,3]
Output: 12
Explanation: The polygon with the largest perimeter which can be made from nums has 5 sides: 1, 1, 2, 3, and 5. The perimeter is 1 + 1 + 2 + 3 + 5 = 12.
We cannot have a polygon with either 12 or 50 as the longest side because it is not possible to include 2 or more smaller sides that have a greater sum than either of them.
It can be shown that the largest possible perimeter is 12.
Example 3:
Input: nums = [5,5,50]
Output: -1
Explanation: There is no possible way to form a polygon from nums, as a polygon has at least 3 sides and 50 > 5 + 5.
Constraints:
•
3 <= n <= 10^5•
1 <= nums[i] <= 10^92024-02-16
1481. Least Number of Unique Integers after K Removals
Topic: Array, Hash Table, Greedy, Sorting, Counting
Difficulty: Medium
Problem:
Given an array of integers
Example 1:
Example 2:
Constraints:
•
•
•
1481. Least Number of Unique Integers after K Removals
Topic: Array, Hash Table, Greedy, Sorting, Counting
Difficulty: Medium
Problem:
Given an array of integers
arr and an integer k. Find the least number of unique integers after removing exactly k elements.Example 1:
Input: arr = [5,5,4], k = 1
Output: 1
Explanation: Remove the single 4, only 5 is left.
Example 2:
Input: arr = [4,3,1,1,3,3,2], k = 3
Output: 2
Explanation: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.
Constraints:
•
1 <= arr.length <= 10^5•
1 <= arr[i] <= 10^9•
0 <= k <= arr.length2024-02-17
1642. Furthest Building You Can Reach
Topic: Array, Greedy, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given an integer array
You start your journey from building
While moving from building
• If the current building's height is greater than or equal to the next building's height, you do not need a ladder or bricks.
• If the current building's height is less than the next building's height, you can either use one ladder or
Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/27/q4.gif
Example 2:
Example 3:
Constraints:
•
•
•
•
1642. Furthest Building You Can Reach
Topic: Array, Greedy, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given an integer array
heights representing the heights of buildings, some bricks, and some ladders.You start your journey from building
0 and move to the next building by possibly using bricks or ladders.While moving from building
i to building i+1 (0-indexed),• If the current building's height is greater than or equal to the next building's height, you do not need a ladder or bricks.
• If the current building's height is less than the next building's height, you can either use one ladder or
(h[i+1] - h[i]) bricks.Return the furthest building index (0-indexed) you can reach if you use the given ladders and bricks optimally.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/27/q4.gif
Input: heights = [4,2,7,6,9,14,12], bricks = 5, ladders = 1
Output: 4
Explanation: Starting at building 0, you can follow these steps:
- Go to building 1 without using ladders nor bricks since 4 >= 2.
- Go to building 2 using 5 bricks. You must use either bricks or ladders because 2 < 7.
- Go to building 3 without using ladders nor bricks since 7 >= 6.
- Go to building 4 using your only ladder. You must use either bricks or ladders because 6 < 9.
It is impossible to go beyond building 4 because you do not have any more bricks or ladders.
Example 2:
Input: heights = [4,12,2,7,3,18,20,3,19], bricks = 10, ladders = 2
Output: 7
Example 3:
Input: heights = [14,3,19,3], bricks = 17, ladders = 0
Output: 3
Constraints:
•
1 <= heights.length <= 10^5•
1 <= heights[i] <= 10^6•
0 <= bricks <= 10^9•
0 <= ladders <= heights.length2024-02-18
2402. Meeting Rooms III
Topic: Array, Hash Table, Sorting, Heap (Priority Queue), Simulation
Difficulty: Hard
Problem:
You are given an integer
You are given a 2D integer array
Meetings are allocated to rooms in the following manner:
1. Each meeting will take place in the unused room with the lowest number.
2. If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the same duration as the original meeting.
3. When a room becomes unused, meetings that have an earlier original start time should be given the room.
Return the number of the room that held the most meetings. If there are multiple rooms, return the room with the lowest number.
A half-closed interval
Example 1:
Example 2:
Constraints:
•
•
•
•
• All the values of
2402. Meeting Rooms III
Topic: Array, Hash Table, Sorting, Heap (Priority Queue), Simulation
Difficulty: Hard
Problem:
You are given an integer
n. There are n rooms numbered from 0 to n - 1.You are given a 2D integer array
meetings where meetings[i] = [start_i, end_i] means that a meeting will be held during the half-closed time interval [start_i, end_i). All the values of start_i are unique.Meetings are allocated to rooms in the following manner:
1. Each meeting will take place in the unused room with the lowest number.
2. If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the same duration as the original meeting.
3. When a room becomes unused, meetings that have an earlier original start time should be given the room.
Return the number of the room that held the most meetings. If there are multiple rooms, return the room with the lowest number.
A half-closed interval
[a, b) is the interval between a and b including a and not including b.Example 1:
Input: n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
Output: 0
Explanation:
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
Example 2:
Input: n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
Output: 1
Explanation:
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
Constraints:
•
1 <= n <= 100•
1 <= meetings.length <= 10^5•
meetings[i].length == 2•
0 <= start_i < end_i <= 5 * 10^5• All the values of
start_i are unique.2024-02-19
231. Power of Two
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?
231. Power of Two
Topic: Math, Bit Manipulation, Recursion
Difficulty: Easy
Problem:
Given an integer
n, return true if it is a power of two. Otherwise, return false.An integer
n is a power of two, if there exists an integer x such that n == 2^x.Example 1:
Input: n = 1
Output: true
Explanation: 2^0 = 1
Example 2:
Input: n = 16
Output: true
Explanation: 2^4 = 16
Example 3:
Input: n = 3
Output: false
Constraints:
•
-2^31 <= n <= 2^31 - 1Follow up: Could you solve it without loops/recursion?
2024-02-20
268. Missing Number
Topic: Array, Hash Table, Math, Binary Search, Bit Manipulation, Sorting
Difficulty: Easy
Problem:
Given an array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
• All the numbers of
Follow up: Could you implement a solution using only
268. Missing Number
Topic: Array, Hash Table, Math, Binary Search, Bit Manipulation, Sorting
Difficulty: Easy
Problem:
Given an array
nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.Example 1:
Input: nums = [3,0,1]
Output: 2
Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
Example 2:
Input: nums = [0,1]
Output: 2
Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
Example 3:
Input: nums = [9,6,4,2,3,5,7,0,1]
Output: 8
Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
Constraints:
•
n == nums.length•
1 <= n <= 10^4•
0 <= nums[i] <= n• All the numbers of
nums are unique.Follow up: Could you implement a solution using only
O(1) extra space complexity and O(n) runtime complexity?2024-02-21
201. Bitwise AND of Numbers Range
Topic: Bit Manipulation
Difficulty: Medium
Problem:
Given two integers
Example 1:
Example 2:
Example 3:
Constraints:
•
201. Bitwise AND of Numbers Range
Topic: Bit Manipulation
Difficulty: Medium
Problem:
Given two integers
left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.Example 1:
Input: left = 5, right = 7
Output: 4
Example 2:
Input: left = 0, right = 0
Output: 0
Example 3:
Input: left = 1, right = 2147483647
Output: 0
Constraints:
•
0 <= left <= right <= 2^31 - 12024-02-22
997. Find the Town Judge
Topic: Array, Hash Table, Graph
Difficulty: Easy
Problem:
In a town, there are
If the town judge exists, then:
1. The town judge trusts nobody.
2. Everybody (except for the town judge) trusts the town judge.
3. There is exactly one person that satisfies properties 1 and 2.
You are given an array
Return the label of the town judge if the town judge exists and can be identified, or return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
• All the pairs of
•
•
997. Find the Town Judge
Topic: Array, Hash Table, Graph
Difficulty: Easy
Problem:
In a town, there are
n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge.If the town judge exists, then:
1. The town judge trusts nobody.
2. Everybody (except for the town judge) trusts the town judge.
3. There is exactly one person that satisfies properties 1 and 2.
You are given an array
trust where trust[i] = [a_i, b_i] representing that the person labeled a_i trusts the person labeled b_i. If a trust relationship does not exist in trust array, then such a trust relationship does not exist.Return the label of the town judge if the town judge exists and can be identified, or return
-1 otherwise.Example 1:
Input: n = 2, trust = [[1,2]]
Output: 2
Example 2:
Input: n = 3, trust = [[1,3],[2,3]]
Output: 3
Example 3:
Input: n = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1
Constraints:
•
1 <= n <= 1000•
0 <= trust.length <= 10^4•
trust[i].length == 2• All the pairs of
trust are unique.•
a_i != b_i•
1 <= a_i, b_i <= n2024-02-23
787. Cheapest Flights Within K Stops
Topic: Dynamic Programming, Depth-First Search, Breadth-First Search, Graph, Heap (Priority Queue), Shortest Path
Difficulty: Medium
Problem:
There are
You are also given three integers
Example 1:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-3drawio.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-1drawio.png
Example 3:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-2drawio.png
Constraints:
•
•
•
•
•
•
• There will not be any multiple flights between two cities.
•
•
787. Cheapest Flights Within K Stops
Topic: Dynamic Programming, Depth-First Search, Breadth-First Search, Graph, Heap (Priority Queue), Shortest Path
Difficulty: Medium
Problem:
There are
n cities connected by some number of flights. You are given an array flights where flights[i] = [from_i, to_i, price_i] indicates that there is a flight from city from_i to city to_i with cost price_i.You are also given three integers
src, dst, and k, return the cheapest price from src to dst with at most k stops. If there is no such route, return -1.Example 1:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-3drawio.png
Input: n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
Output: 700
Explanation:
The graph is shown above.
The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost 100 + 600 = 700.
Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2 stops.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-1drawio.png
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
Output: 200
Explanation:
The graph is shown above.
The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost 100 + 100 = 200.
Example 3:
Image: https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-2drawio.png
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
Output: 500
Explanation:
The graph is shown above.
The optimal path with no stops from city 0 to 2 is marked in red and has cost 500.
Constraints:
•
1 <= n <= 100•
0 <= flights.length <= (n * (n - 1) / 2)•
flights[i].length == 3•
0 <= from_i, to_i < n•
from_i != to_i•
1 <= price_i <= 10^4• There will not be any multiple flights between two cities.
•
0 <= src, dst, k < n•
src != dst2024-02-24
2092. Find All People With Secret
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph, Sorting
Difficulty: Hard
Problem:
You are given an integer
Person
The secrets are shared instantaneously. That is, a person may receive the secret and share it with people in other meetings within the same time frame.
Return a list of all the people that have the secret after all the meetings have taken place. You may return the answer in any order.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
•
•
2092. Find All People With Secret
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph, Sorting
Difficulty: Hard
Problem:
You are given an integer
n indicating there are n people numbered from 0 to n - 1. You are also given a 0-indexed 2D integer array meetings where meetings[i] = [x_i, y_i, time_i] indicates that person x_i and person y_i have a meeting at time_i. A person may attend multiple meetings at the same time. Finally, you are given an integer firstPerson.Person
0 has a secret and initially shares the secret with a person firstPerson at time 0. This secret is then shared every time a meeting takes place with a person that has the secret. More formally, for every meeting, if a person x_i has the secret at time_i, then they will share the secret with person y_i, and vice versa.The secrets are shared instantaneously. That is, a person may receive the secret and share it with people in other meetings within the same time frame.
Return a list of all the people that have the secret after all the meetings have taken place. You may return the answer in any order.
Example 1:
Input: n = 6, meetings = [[1,2,5],[2,3,8],[1,5,10]], firstPerson = 1
Output: [0,1,2,3,5]
Explanation:
At time 0, person 0 shares the secret with person 1.
At time 5, person 1 shares the secret with person 2.
At time 8, person 2 shares the secret with person 3.
At time 10, person 1 shares the secret with person 5.
Thus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.
Example 2:
Input: n = 4, meetings = [[3,1,3],[1,2,2],[0,3,3]], firstPerson = 3
Output: [0,1,3]
Explanation:
At time 0, person 0 shares the secret with person 3.
At time 2, neither person 1 nor person 2 know the secret.
At time 3, person 3 shares the secret with person 0 and person 1.
Thus, people 0, 1, and 3 know the secret after all the meetings.
Example 3:
Input: n = 5, meetings = [[3,4,2],[1,2,1],[2,3,1]], firstPerson = 1
Output: [0,1,2,3,4]
Explanation:
At time 0, person 0 shares the secret with person 1.
At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with person 3.
Note that person 2 can share the secret at the same time as receiving it.
At time 2, person 3 shares the secret with person 4.
Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.
Constraints:
•
2 <= n <= 10^5•
1 <= meetings.length <= 10^5•
meetings[i].length == 3•
0 <= x_i, y_i <= n - 1•
x_i != y_i•
1 <= time_i <= 10^5•
1 <= firstPerson <= n - 12024-02-25
2709. Greatest Common Divisor Traversal
Topic: Array, Math, Union Find, Number Theory
Difficulty: Hard
Problem:
You are given a 0-indexed integer array
Your task is to determine if for every pair of indices
Return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2709. Greatest Common Divisor Traversal
Topic: Array, Math, Union Find, Number Theory
Difficulty: Hard
Problem:
You are given a 0-indexed integer array
nums, and you are allowed to traverse between its indices. You can traverse between index i and index j, i != j, if and only if gcd(nums[i], nums[j]) > 1, where gcd is the greatest common divisor.Your task is to determine if for every pair of indices
i and j in nums, where i < j, there exists a sequence of traversals that can take us from i to j.Return
true if it is possible to traverse between all such pairs of indices, or false otherwise.Example 1:
Input: nums = [2,3,6]
Output: true
Explanation: In this example, there are 3 possible pairs of indices: (0, 1), (0, 2), and (1, 2).
To go from index 0 to index 1, we can use the sequence of traversals 0 -> 2 -> 1, where we move from index 0 to index 2 because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1, and then move from index 2 to index 1 because gcd(nums[2], nums[1]) = gcd(6, 3) = 3 > 1.
To go from index 0 to index 2, we can just go directly because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1. Likewise, to go from index 1 to index 2, we can just go directly because gcd(nums[1], nums[2]) = gcd(3, 6) = 3 > 1.
Example 2:
Input: nums = [3,9,5]
Output: false
Explanation: No sequence of traversals can take us from index 0 to index 2 in this example. So, we return false.
Example 3:
Input: nums = [4,3,12,8]
Output: true
Explanation: There are 6 possible pairs of indices to traverse between: (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), and (2, 3). A valid sequence of traversals exists for each pair, so we return true.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^52024-02-26
100. Same Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given the roots of two binary trees
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/20/ex1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/20/ex2.jpg
Example 3:
Image: https://assets.leetcode.com/uploads/2020/12/20/ex3.jpg
Constraints:
• The number of nodes in both trees is in the range
•
100. Same Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given the roots of two binary trees
p and q, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/20/ex1.jpg
Input: p = [1,2,3], q = [1,2,3]
Output: true
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/20/ex2.jpg
Input: p = [1,2], q = [1,null,2]
Output: false
Example 3:
Image: https://assets.leetcode.com/uploads/2020/12/20/ex3.jpg
Input: p = [1,2,1], q = [1,1,2]
Output: false
Constraints:
• The number of nodes in both trees is in the range
[0, 100].•
-10^4 <= Node.val <= 10^42024-02-27
543. Diameter of Binary Tree
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given the
The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the
The length of a path between two nodes is represented by the number of edges between them.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg
Example 2:
Constraints:
• The number of nodes in the tree is in the range
•
543. Diameter of Binary Tree
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given the
root of a binary tree, return the length of the diameter of the tree.The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the
root.The length of a path between two nodes is represented by the number of edges between them.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg
Input: root = [1,2,3,4,5]
Output: 3
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
Example 2:
Input: root = [1,2]
Output: 1
Constraints:
• The number of nodes in the tree is in the range
[1, 10^4].•
-100 <= Node.val <= 1002024-02-28
513. Find Bottom Left Tree Value
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg
Constraints:
• The number of nodes in the tree is in the range
•
513. Find Bottom Left Tree Value
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, return the leftmost value in the last row of the tree.Example 1:
Image: https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg
Input: root = [2,1,3]
Output: 1
Example 2:
Image: https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg
Input: root = [1,2,3,4,null,5,6,null,null,7]
Output: 7
Constraints:
• The number of nodes in the tree is in the range
[1, 10^4].•
-2^31 <= Node.val <= 2^31 - 1