2023-07-04
137. Single Number II
Topic: Array, Bit Manipulation
Difficulty: Medium
Problem:
Given an integer array
You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:
Example 2:
Constraints:
•
•
• Each element in
137. Single Number II
Topic: Array, Bit Manipulation
Difficulty: Medium
Problem:
Given an integer array
nums where every element appears three times except for one, which appears exactly once. Find the single element and return it.You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:
Input: nums = [2,2,3,2]
Output: 3
Example 2:
Input: nums = [0,1,0,1,0,1,99]
Output: 99
Constraints:
•
1 <= nums.length <= 3 * 10^4•
-2^31 <= nums[i] <= 2^31 - 1• Each element in
nums appears exactly three times except for one element which appears once.2023-07-05
1493. Longest Subarray of 1's After Deleting One Element
Topic: Array, Dynamic Programming, Sliding Window
Difficulty: Medium
Problem:
Given a binary array
Return the size of the longest non-empty subarray containing only
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1493. Longest Subarray of 1's After Deleting One Element
Topic: Array, Dynamic Programming, Sliding Window
Difficulty: Medium
Problem:
Given a binary array
nums, you should delete one element from it.Return the size of the longest non-empty subarray containing only
1's in the resulting array. Return 0 if there is no such subarray.Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Constraints:
•
1 <= nums.length <= 10^5•
nums[i] is either 0 or 1.2023-07-06
209. Minimum Size Subarray Sum
Topic: Array, Binary Search, Sliding Window, Prefix Sum
Difficulty: Medium
Problem:
Given an array of positive integers
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
Follow up: If you have figured out the
209. Minimum Size Subarray Sum
Topic: Array, Binary Search, Sliding Window, Prefix Sum
Difficulty: Medium
Problem:
Given an array of positive integers
nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.Example 1:
Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.
Example 2:
Input: target = 4, nums = [1,4,4]
Output: 1
Example 3:
Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0
Constraints:
•
1 <= target <= 10^9•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^4Follow up: If you have figured out the
O(n) solution, try coding another solution of which the time complexity is O(n log(n)).2023-07-07
2024. Maximize the Confusion of an Exam
Topic: String, Binary Search, Sliding Window, Prefix Sum
Difficulty: Medium
Problem:
A teacher is writing a test with
You are given a string
• Change the answer key for any question to
Return the maximum number of consecutive
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
2024. Maximize the Confusion of an Exam
Topic: String, Binary Search, Sliding Window, Prefix Sum
Difficulty: Medium
Problem:
A teacher is writing a test with
n true/false questions, with 'T' denoting true and 'F' denoting false. He wants to confuse the students by maximizing the number of consecutive questions with the same answer (multiple trues or multiple falses in a row).You are given a string
answerKey, where answerKey[i] is the original answer to the i^th question. In addition, you are given an integer k, the maximum number of times you may perform the following operation:• Change the answer key for any question to
'T' or 'F' (i.e., set answerKey[i] to 'T' or 'F').Return the maximum number of consecutive
'T's or 'F's in the answer key after performing the operation at most k times.Example 1:
Input: answerKey = "TTFF", k = 2
Output: 4
Explanation: We can replace both the 'F's with 'T's to make answerKey = "TTTT".
There are four consecutive 'T's.
Example 2:
Input: answerKey = "TFFT", k = 1
Output: 3
Explanation: We can replace the first 'T' with an 'F' to make answerKey = "FFFT".
Alternatively, we can replace the second 'T' with an 'F' to make answerKey = "TFFF".
In both cases, there are three consecutive 'F's.
Example 3:
Input: answerKey = "TTFTTFTT", k = 1
Output: 5
Explanation: We can replace the first 'F' to make answerKey = "TTTTTFTT"
Alternatively, we can replace the second 'F' to make answerKey = "TTFTTTTT".
In both cases, there are five consecutive 'T's.
Constraints:
•
n == answerKey.length•
1 <= n <= 5 * 10^4•
answerKey[i] is either 'T' or 'F'•
1 <= k <= n2023-07-08
2551. Put Marbles in Bags
Topic: Array, Greedy, Sorting, Heap (Priority Queue)
Difficulty: Hard
Problem:
You have
Divide the marbles into the
• No bag is empty.
• If the
• If a bag consists of all the marbles with an index from
The score after distributing the marbles is the sum of the costs of all the
Return the difference between the maximum and minimum scores among marble distributions.
Example 1:
Example 2:
Constraints:
•
•
2551. Put Marbles in Bags
Topic: Array, Greedy, Sorting, Heap (Priority Queue)
Difficulty: Hard
Problem:
You have
k bags. You are given a 0-indexed integer array weights where weights[i] is the weight of the i^th marble. You are also given the integer k.Divide the marbles into the
k bags according to the following rules:• No bag is empty.
• If the
i^th marble and j^th marble are in a bag, then all marbles with an index between the i^th and j^th indices should also be in that same bag.• If a bag consists of all the marbles with an index from
i to j inclusively, then the cost of the bag is weights[i] + weights[j].The score after distributing the marbles is the sum of the costs of all the
k bags.Return the difference between the maximum and minimum scores among marble distributions.
Example 1:
Input: weights = [1,3,5,1], k = 2
Output: 4
Explanation:
The distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6.
The distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10.
Thus, we return their difference 10 - 6 = 4.
Example 2:
Input: weights = [1, 3], k = 2
Output: 0
Explanation: The only distribution possible is [1],[3].
Since both the maximal and minimal score are the same, we return 0.
Constraints:
•
1 <= k <= weights.length <= 10^5•
1 <= weights[i] <= 10^92023-07-09
2272. Substring With Largest Variance
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
The variance of a string is defined as the largest difference between the number of occurrences of any
Given a string
A substring is a contiguous sequence of characters within a string.
Example 1:
Example 2:
Constraints:
•
•
2272. Substring With Largest Variance
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
The variance of a string is defined as the largest difference between the number of occurrences of any
2 characters present in the string. Note the two characters may or may not be the same.Given a string
s consisting of lowercase English letters only, return the largest variance possible among all substrings of s.A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "aababbb"
Output: 3
Explanation:
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
Example 2:
Input: s = "abcde"
Output: 0
Explanation:
No letter occurs more than once in s, so the variance of every substring is 0.
Constraints:
•
1 <= s.length <= 10^4•
s consists of lowercase English letters.2023-07-10
111. Minimum Depth of Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Note: A leaf is a node with no children.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg
Example 2:
Constraints:
• The number of nodes in the tree is in the range
•
111. Minimum Depth of Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Note: A leaf is a node with no children.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg
Input: root = [3,9,20,null,null,15,7]
Output: 2
Example 2:
Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5
Constraints:
• The number of nodes in the tree is in the range
[0, 10^5].•
-1000 <= Node.val <= 10002023-07-11
863. All Nodes Distance K in Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
You can return the answer in any order.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/28/sketch0.png
Example 2:
Constraints:
• The number of nodes in the tree is in the range
•
• All the values
•
•
863. All Nodes Distance K in Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, the value of a target node target, and an integer k, return an array of the values of all nodes that have a distance k from the target node.You can return the answer in any order.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/28/sketch0.png
Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2
Output: [7,4,1]
Explanation: The nodes that are a distance 2 from the target node (with value 5) have values 7, 4, and 1.
Example 2:
Input: root = [1], target = 1, k = 3
Output: []
Constraints:
• The number of nodes in the tree is in the range
[1, 500].•
0 <= Node.val <= 500• All the values
Node.val are unique.•
target is the value of one of the nodes in the tree.•
0 <= k <= 10002023-07-12
802. Find Eventual Safe States
Topic: Depth-First Search, Breadth-First Search, Graph, Topological Sort
Difficulty: Medium
Problem:
There is a directed graph of
A node is a terminal node if there are no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node (or another safe node).
Return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/17/picture1.png
Example 2:
Constraints:
•
•
•
•
•
• The graph may contain self-loops.
• The number of edges in the graph will be in the range
802. Find Eventual Safe States
Topic: Depth-First Search, Breadth-First Search, Graph, Topological Sort
Difficulty: Medium
Problem:
There is a directed graph of
n nodes with each node labeled from 0 to n - 1. The graph is represented by a 0-indexed 2D integer array graph where graph[i] is an integer array of nodes adjacent to node i, meaning there is an edge from node i to each node in graph[i].A node is a terminal node if there are no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node (or another safe node).
Return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/17/picture1.png
Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
Output: [2,4,5,6]
Explanation: The given graph is shown above.
Nodes 5 and 6 are terminal nodes as there are no outgoing edges from either of them.
Every path starting at nodes 2, 4, 5, and 6 all lead to either node 5 or 6.
Example 2:
Input: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
Output: [4]
Explanation:
Only node 4 is a terminal node, and every path starting at node 4 leads to node 4.
Constraints:
•
n == graph.length•
1 <= n <= 10^4•
0 <= graph[i].length <= n•
0 <= graph[i][j] <= n - 1•
graph[i] is sorted in a strictly increasing order.• The graph may contain self-loops.
• The number of edges in the graph will be in the range
[1, 4 * 10^4].2023-07-13
207. Course Schedule
Topic: Depth-First Search, Breadth-First Search, Graph, Topological Sort
Difficulty: Medium
Problem:
There are a total of
• For example, the pair
Return
Example 1:
Example 2:
Constraints:
•
•
•
•
• All the pairs prerequisitesi are unique.
207. Course Schedule
Topic: Depth-First Search, Breadth-First Search, Graph, Topological Sort
Difficulty: Medium
Problem:
There are a total of
numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [a_i, b_i] indicates that you must take course b_i first if you want to take course a_i.• For example, the pair
[0, 1], indicates that to take course 0 you have to first take course 1.Return
true if you can finish all courses. Otherwise, return false.Example 1:
Input: numCourses = 2, prerequisites = [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
Example 2:
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
Constraints:
•
1 <= numCourses <= 2000•
0 <= prerequisites.length <= 5000•
prerequisites[i].length == 2•
0 <= a_i, b_i < numCourses• All the pairs prerequisitesi are unique.
2023-07-14
1218. Longest Arithmetic Subsequence of Given Difference
Topic: Array, Hash Table, Dynamic Programming
Difficulty: Medium
Problem:
Given an integer array
A subsequence is a sequence that can be derived from
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1218. Longest Arithmetic Subsequence of Given Difference
Topic: Array, Hash Table, Dynamic Programming
Difficulty: Medium
Problem:
Given an integer array
arr and an integer difference, return the length of the longest subsequence in arr which is an arithmetic sequence such that the difference between adjacent elements in the subsequence equals difference.A subsequence is a sequence that can be derived from
arr by deleting some or no elements without changing the order of the remaining elements.Example 1:
Input: arr = [1,2,3,4], difference = 1
Output: 4
Explanation: The longest arithmetic subsequence is [1,2,3,4].
Example 2:
Input: arr = [1,3,5,7], difference = 1
Output: 1
Explanation: The longest arithmetic subsequence is any single element.
Example 3:
Input: arr = [1,5,7,8,5,3,4,2,1], difference = -2
Output: 4
Explanation: The longest arithmetic subsequence is [7,5,3,1].
Constraints:
•
1 <= arr.length <= 10^5•
-10^4 <= arr[i], difference <= 10^42023-07-15
1751. Maximum Number of Events That Can Be Attended II
Topic: Array, Binary Search, Dynamic Programming, Sorting
Difficulty: Hard
Problem:
You are given an array of
You can only attend one event at a time. If you choose to attend an event, you must attend the entire event. Note that the end day is inclusive: that is, you cannot attend two events where one of them starts and the other ends on the same day.
Return the maximum sum of values that you can receive by attending events.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60048-pm.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60150-pm.png
Example 3:
Image: https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60703-pm.png
Constraints:
•
•
•
•
1751. Maximum Number of Events That Can Be Attended II
Topic: Array, Binary Search, Dynamic Programming, Sorting
Difficulty: Hard
Problem:
You are given an array of
events where events[i] = [startDay_i, endDay_i, value_i]. The i^th event starts at startDay_i and ends at endDay_i, and if you attend this event, you will receive a value of value_i. You are also given an integer k which represents the maximum number of events you can attend.You can only attend one event at a time. If you choose to attend an event, you must attend the entire event. Note that the end day is inclusive: that is, you cannot attend two events where one of them starts and the other ends on the same day.
Return the maximum sum of values that you can receive by attending events.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60048-pm.png
Input: events = [[1,2,4],[3,4,3],[2,3,1]], k = 2
Output: 7
Explanation: Choose the green events, 0 and 1 (0-indexed) for a total value of 4 + 3 = 7.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60150-pm.png
Input: events = [[1,2,4],[3,4,3],[2,3,10]], k = 2
Output: 10
Explanation: Choose event 2 for a total value of 10.
Notice that you cannot attend any other event as they overlap, and that you do not have to attend k events.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60703-pm.png
Input: events = [[1,1,1],[2,2,2],[3,3,3],[4,4,4]], k = 3
Output: 9
Explanation: Although the events do not overlap, you can only attend 3 events. Pick the highest valued three.
Constraints:
•
1 <= k <= events.length•
1 <= k * events.length <= 10^6•
1 <= startDay_i <= endDay_i <= 10^9•
1 <= value_i <= 10^62023-07-16
1125. Smallest Sufficient Team
Topic: Array, Dynamic Programming, Bit Manipulation, Bitmask
Difficulty: Hard
Problem:
In a project, you have a list of required skills
Consider a sufficient team: a set of people such that for every required skill in
• For example,
Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.
It is guaranteed an answer exists.
Example 1:
Example 2:
Constraints:
•
•
•
• All the strings of
•
•
•
•
• All the strings of
• Every skill in
• It is guaranteed a sufficient team exists.
1125. Smallest Sufficient Team
Topic: Array, Dynamic Programming, Bit Manipulation, Bitmask
Difficulty: Hard
Problem:
In a project, you have a list of required skills
req_skills, and a list of people. The i^th person people[i] contains a list of skills that the person has.Consider a sufficient team: a set of people such that for every required skill in
req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person.• For example,
team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.
It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
•
1 <= req_skills.length <= 16•
1 <= req_skills[i].length <= 16•
req_skills[i] consists of lowercase English letters.• All the strings of
req_skills are unique.•
1 <= people.length <= 60•
0 <= people[i].length <= 16•
1 <= people[i][j].length <= 16•
people[i][j] consists of lowercase English letters.• All the strings of
people[i] are unique.• Every skill in
people[i] is a skill in req_skills.• It is guaranteed a sufficient team exists.
2023-07-17
445. Add Two Numbers II
Topic: Linked List, Math, Stack
Difficulty: Medium
Problem:
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/09/sumii-linked-list.jpg
Example 2:
Example 3:
Constraints:
• The number of nodes in each linked list is in the range
•
• It is guaranteed that the list represents a number that does not have leading zeros.
Follow up: Could you solve it without reversing the input lists?
445. Add Two Numbers II
Topic: Linked List, Math, Stack
Difficulty: Medium
Problem:
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/09/sumii-linked-list.jpg
Input: l1 = [7,2,4,3], l2 = [5,6,4]
Output: [7,8,0,7]
Example 2:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [8,0,7]
Example 3:
Input: l1 = [0], l2 = [0]
Output: [0]
Constraints:
• The number of nodes in each linked list is in the range
[1, 100].•
0 <= Node.val <= 9• It is guaranteed that the list represents a number that does not have leading zeros.
Follow up: Could you solve it without reversing the input lists?
2023-07-18
146. LRU Cache
Topic: Hash Table, Linked List, Design, Doubly-Linked List
Difficulty: Medium
Problem:
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the
•
•
•
The functions
Example 1:
Constraints:
•
•
•
• At most
146. LRU Cache
Topic: Hash Table, Linked List, Design, Doubly-Linked List
Difficulty: Medium
Problem:
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the
LRUCache class:•
LRUCache(int capacity) Initialize the LRU cache with positive size capacity.•
int get(int key) Return the value of the key if the key exists, otherwise return -1.•
void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.The functions
get and put must each run in O(1) average time complexity.Example 1:
Input
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
Output
[null, null, null, 1, null, -1, null, -1, 3, 4]
Explanation
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // cache is {1=1}
lRUCache.put(2, 2); // cache is {1=1, 2=2}
lRUCache.get(1); // return 1
lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
lRUCache.get(2); // returns -1 (not found)
lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
lRUCache.get(1); // return -1 (not found)
lRUCache.get(3); // return 3
lRUCache.get(4); // return 4
Constraints:
•
1 <= capacity <= 3000•
0 <= key <= 10^4•
0 <= value <= 10^5• At most
2 * 10^5 calls will be made to get and put.2023-07-19
435. Non-overlapping Intervals
Topic: Array, Dynamic Programming, Greedy, Sorting
Difficulty: Medium
Problem:
Given an array of intervals
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
435. Non-overlapping Intervals
Topic: Array, Dynamic Programming, Greedy, Sorting
Difficulty: Medium
Problem:
Given an array of intervals
intervals where intervals[i] = [start_i, end_i], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.Example 1:
Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
Output: 1
Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
Example 2:
Input: intervals = [[1,2],[1,2],[1,2]]
Output: 2
Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.
Example 3:
Input: intervals = [[1,2],[2,3]]
Output: 0
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
Constraints:
•
1 <= intervals.length <= 10^5•
intervals[i].length == 2•
-5 * 10^4 <= start_i < end_i <= 5 * 10^42023-07-20
735. Asteroid Collision
Topic: Array, Stack, Simulation
Difficulty: Medium
Problem:
We are given an array
For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.
Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
735. Asteroid Collision
Topic: Array, Stack, Simulation
Difficulty: Medium
Problem:
We are given an array
asteroids of integers representing asteroids in a row.For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.
Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.
Example 1:
Input: asteroids = [5,10,-5]
Output: [5,10]
Explanation: The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
Example 2:
Input: asteroids = [8,-8]
Output: []
Explanation: The 8 and -8 collide exploding each other.
Example 3:
Input: asteroids = [10,2,-5]
Output: [10]
Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
Constraints:
•
2 <= asteroids.length <= 10^4•
-1000 <= asteroids[i] <= 1000•
asteroids[i] != 02023-07-21
673. Number of Longest Increasing Subsequence
Topic: Array, Dynamic Programming, Binary Indexed Tree, Segment Tree
Difficulty: Medium
Problem:
Given an integer array
Notice that the sequence has to be strictly increasing.
Example 1:
Example 2:
Constraints:
•
•
673. Number of Longest Increasing Subsequence
Topic: Array, Dynamic Programming, Binary Indexed Tree, Segment Tree
Difficulty: Medium
Problem:
Given an integer array
nums, return the number of longest increasing subsequences.Notice that the sequence has to be strictly increasing.
Example 1:
Input: nums = [1,3,5,4,7]
Output: 2
Explanation: The two longest increasing subsequences are [1, 3, 4, 7] and [1, 3, 5, 7].
Example 2:
Input: nums = [2,2,2,2,2]
Output: 5
Explanation: The length of the longest increasing subsequence is 1, and there are 5 increasing subsequences of length 1, so output 5.
Constraints:
•
1 <= nums.length <= 2000•
-10^6 <= nums[i] <= 10^62023-07-22
688. Knight Probability in Chessboard
Topic: Dynamic Programming
Difficulty: Medium
Problem:
On an
A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.
Image: https://assets.leetcode.com/uploads/2018/10/12/knight.png
Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.
The knight continues moving until it has made exactly
Return the probability that the knight remains on the board after it has stopped moving.
Example 1:
Example 2:
Constraints:
•
•
•
688. Knight Probability in Chessboard
Topic: Dynamic Programming
Difficulty: Medium
Problem:
On an
n x n chessboard, a knight starts at the cell (row, column) and attempts to make exactly k moves. The rows and columns are 0-indexed, so the top-left cell is (0, 0), and the bottom-right cell is (n - 1, n - 1).A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.
Image: https://assets.leetcode.com/uploads/2018/10/12/knight.png
Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.
The knight continues moving until it has made exactly
k moves or has moved off the chessboard.Return the probability that the knight remains on the board after it has stopped moving.
Example 1:
Input: n = 3, k = 2, row = 0, column = 0
Output: 0.06250
Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.
From each of those positions, there are also two moves that will keep the knight on the board.
The total probability the knight stays on the board is 0.0625.
Example 2:
Input: n = 1, k = 0, row = 0, column = 0
Output: 1.00000
Constraints:
•
1 <= n <= 25•
0 <= k <= 100•
0 <= row, column <= n - 12023-07-23
894. All Possible Full Binary Trees
Topic: Dynamic Programming, Tree, Recursion, Memoization, Binary Tree
Difficulty: Medium
Problem:
Given an integer
Each element of the answer is the root node of one possible tree. You may return the final list of trees in any order.
A full binary tree is a binary tree where each node has exactly
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/22/fivetrees.png
Example 2:
Constraints:
•
894. All Possible Full Binary Trees
Topic: Dynamic Programming, Tree, Recursion, Memoization, Binary Tree
Difficulty: Medium
Problem:
Given an integer
n, return a list of all possible full binary trees with n nodes. Each node of each tree in the answer must have Node.val == 0.Each element of the answer is the root node of one possible tree. You may return the final list of trees in any order.
A full binary tree is a binary tree where each node has exactly
0 or 2 children.Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/22/fivetrees.png
Input: n = 7
Output: [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]]
Example 2:
Input: n = 3
Output: [[0,0,0]]
Constraints:
•
1 <= n <= 20