2022-02-19
1675. Minimize Deviation in Array
Topic: Array, Greedy, Heap (Priority Queue), Ordered Set
Difficulty: Hard
Problem:
You are given an array
You can perform two types of operations on any element of the array any number of times:
• If the element is even, divide it by
• For example, if the array is
• If the element is odd, multiply it by
• For example, if the array is
The deviation of the array is the maximum difference between any two elements in the array.
Return the minimum deviation the array can have after performing some number of operations.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1675. Minimize Deviation in Array
Topic: Array, Greedy, Heap (Priority Queue), Ordered Set
Difficulty: Hard
Problem:
You are given an array
nums of n positive integers.You can perform two types of operations on any element of the array any number of times:
• If the element is even, divide it by
2.• For example, if the array is
[1,2,3,4], then you can do this operation on the last element, and the array will be [1,2,3,2].• If the element is odd, multiply it by
2.• For example, if the array is
[1,2,3,4], then you can do this operation on the first element, and the array will be [2,2,3,4].The deviation of the array is the maximum difference between any two elements in the array.
Return the minimum deviation the array can have after performing some number of operations.
Example 1:
Input: nums = [1,2,3,4]
Output: 1
Explanation: You can transform the array to [1,2,3,2], then to [2,2,3,2], then the deviation will be 3 - 2 = 1.
Example 2:
Input: nums = [4,1,5,20,3]
Output: 3
Explanation: You can transform the array after two operations to [4,2,5,5,3], then the deviation will be 5 - 2 = 3.
Example 3:
Input: nums = [2,10,8]
Output: 3
Constraints:
•
n == nums.length•
2 <= n <= 10^5•
1 <= nums[i] <= 10^92022-02-20
1288. Remove Covered Intervals
Topic: Array, Sorting
Difficulty: Medium
Problem:
Given an array
The interval
Return the number of remaining intervals.
Example 1:
Example 2:
Constraints:
•
•
•
• All the given intervals are unique.
1288. Remove Covered Intervals
Topic: Array, Sorting
Difficulty: Medium
Problem:
Given an array
intervals where intervals[i] = [l_i, r_i] represent the interval [l_i, r_i), remove all intervals that are covered by another interval in the list.The interval
[a, b) is covered by the interval [c, d) if and only if c <= a and b <= d.Return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Constraints:
•
1 <= intervals.length <= 1000•
intervals[i].length == 2•
0 <= l_i <= r_i <= 10^5• All the given intervals are unique.
2022-02-21
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•
-2^31 <= nums[i] <= 2^31 - 1Follow-up: Could you solve the problem in linear time and in
O(1) space?2022-02-22
171. Excel Sheet Column Number
Topic: Math, String
Difficulty: Easy
Problem:
Given a string
For example:
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
171. Excel Sheet Column Number
Topic: Math, String
Difficulty: Easy
Problem:
Given a string
columnTitle that represents the column title as appear in an Excel sheet, return its corresponding column number.For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Example 1:
Input: columnTitle = "A"
Output: 1
Example 2:
Input: columnTitle = "AB"
Output: 28
Example 3:
Input: columnTitle = "ZY"
Output: 701
Constraints:
•
1 <= columnTitle.length <= 7•
columnTitle consists only of uppercase English letters.•
columnTitle is in the range ["A", "FXSHRXW"].2022-02-23
133. Clone Graph
Topic: Hash Table, Depth-First Search, Breadth-First Search, Graph
Difficulty: Medium
Problem:
Given a reference of a node in a connected undirected graph.
Return a deep copy (clone) of the graph.
Each node in the graph contains a value (
Test case format:
For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with
An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.
The given node will always be the first node with
Example 1:
Image: https://assets.leetcode.com/uploads/2019/11/04/133_clone_graph_question.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/01/07/graph.png
Example 3:
Constraints:
• The number of nodes in the graph is in the range
•
•
• There are no repeated edges and no self-loops in the graph.
• The Graph is connected and all nodes can be visited starting from the given node.
133. Clone Graph
Topic: Hash Table, Depth-First Search, Breadth-First Search, Graph
Difficulty: Medium
Problem:
Given a reference of a node in a connected undirected graph.
Return a deep copy (clone) of the graph.
Each node in the graph contains a value (
int) and a list (List[Node]) of its neighbors.class Node {
public int val;
public List<Node> neighbors;
}
Test case format:
For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with
val == 1, the second node with val == 2, and so on. The graph is represented in the test case using an adjacency list.An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.
The given node will always be the first node with
val = 1. You must return the copy of the given node as a reference to the cloned graph.Example 1:
Image: https://assets.leetcode.com/uploads/2019/11/04/133_clone_graph_question.png
Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
Output: [[2,4],[1,3],[2,4],[1,3]]
Explanation: There are 4 nodes in the graph.
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
Example 2:
Image: https://assets.leetcode.com/uploads/2020/01/07/graph.png
Input: adjList = [[]]
Output: [[]]
Explanation: Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors.
Example 3:
Input: adjList = []
Output: []
Explanation: This an empty graph, it does not have any nodes.
Constraints:
• The number of nodes in the graph is in the range
[0, 100].•
1 <= Node.val <= 100•
Node.val is unique for each node.• There are no repeated edges and no self-loops in the graph.
• The Graph is connected and all nodes can be visited starting from the given node.
2022-02-24
148. Sort List
Topic: Linked List, Two Pointers, Divide and Conquer, Sorting, Merge Sort
Difficulty: Medium
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg
Example 3:
Constraints:
• The number of nodes in the list is in the range
•
Follow up: Can you sort the linked list in
148. Sort List
Topic: Linked List, Two Pointers, Divide and Conquer, Sorting, Merge Sort
Difficulty: Medium
Problem:
Given the
head of a linked list, return the list after sorting it in ascending order.Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg
Input: head = [4,2,1,3]
Output: [1,2,3,4]
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg
Input: head = [-1,5,3,4,0]
Output: [-1,0,3,4,5]
Example 3:
Input: head = []
Output: []
Constraints:
• The number of nodes in the list is in the range
[0, 5 * 10^4].•
-10^5 <= Node.val <= 10^5Follow up: Can you sort the linked list in
O(n logn) time and O(1) memory (i.e. constant space)?2022-02-25
165. Compare Version Numbers
Topic: Two Pointers, String
Difficulty: Medium
Problem:
Given two version numbers,
Version numbers consist of one or more revisions joined by a dot
To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions
Return the following:
• If
• If
• Otherwise, return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
• All the given revisions in
165. Compare Version Numbers
Topic: Two Pointers, String
Difficulty: Medium
Problem:
Given two version numbers,
version1 and version2, compare them.Version numbers consist of one or more revisions joined by a dot
'.'. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example 2.5.33 and 0.1 are valid version numbers.To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions
1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.Return the following:
• If
version1 < version2, return -1.• If
version1 > version2, return 1.• Otherwise, return
0.Example 1:
Input: version1 = "1.01", version2 = "1.001"
Output: 0
Explanation: Ignoring leading zeroes, both "01" and "001" represent the same integer "1".
Example 2:
Input: version1 = "1.0", version2 = "1.0.0"
Output: 0
Explanation: version1 does not specify revision 2, which means it is treated as "0".
Example 3:
Input: version1 = "0.1", version2 = "1.1"
Output: -1
Explanation: version1's revision 0 is "0", while version2's revision 0 is "1". 0 < 1, so version1 < version2.
Constraints:
•
1 <= version1.length, version2.length <= 500•
version1 and version2 only contain digits and '.'.•
version1 and version2 are valid version numbers.• All the given revisions in
version1 and version2 can be stored in a 32-bit integer.2022-02-26
847. Shortest Path Visiting All Nodes
Topic: Dynamic Programming, Bit Manipulation, Breadth-First Search, Graph, Bitmask
Difficulty: Hard
Problem:
You have an undirected, connected graph of
Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/12/shortest1-graph.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/05/12/shortest2-graph.jpg
Constraints:
•
•
•
•
• If
• The input graph is always connected.
847. Shortest Path Visiting All Nodes
Topic: Dynamic Programming, Bit Manipulation, Breadth-First Search, Graph, Bitmask
Difficulty: Hard
Problem:
You have an undirected, connected graph of
n nodes labeled from 0 to n - 1. You are given an array graph where graph[i] is a list of all the nodes connected with node i by an edge.Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/12/shortest1-graph.jpg
Input: graph = [[1,2,3],[0],[0],[0]]
Output: 4
Explanation: One possible path is [1,0,2,0,3]
Example 2:
Image: https://assets.leetcode.com/uploads/2021/05/12/shortest2-graph.jpg
Input: graph = [[1],[0,2,4],[1,3,4],[2],[1,2]]
Output: 4
Explanation: One possible path is [0,1,4,2,3]
Constraints:
•
n == graph.length•
1 <= n <= 12•
0 <= graph[i].length < n•
graph[i] does not contain i.• If
graph[a] contains b, then graph[b] contains a.• The input graph is always connected.
2022-02-27
662. Maximum Width of Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
The maximum width of a tree is the maximum width among all levels.
The width of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes are also counted into the length calculation.
It is guaranteed that the answer will in the range of 32-bit signed integer.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/03/width1-tree.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/05/03/width2-tree.jpg
Example 3:
Image: https://assets.leetcode.com/uploads/2021/05/03/width3-tree.jpg
Constraints:
• The number of nodes in the tree is in the range
•
662. Maximum Width of Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, return the maximum width of the given tree.The maximum width of a tree is the maximum width among all levels.
The width of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes are also counted into the length calculation.
It is guaranteed that the answer will in the range of 32-bit signed integer.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/03/width1-tree.jpg
Input: root = [1,3,2,5,3,null,9]
Output: 4
Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).
Example 2:
Image: https://assets.leetcode.com/uploads/2021/05/03/width2-tree.jpg
Input: root = [1,3,null,5,3]
Output: 2
Explanation: The maximum width existing in the third level with the length 2 (5,3).
Example 3:
Image: https://assets.leetcode.com/uploads/2021/05/03/width3-tree.jpg
Input: root = [1,3,2,5]
Output: 2
Explanation: The maximum width existing in the second level with the length 2 (3,2).
Constraints:
• The number of nodes in the tree is in the range
[1, 3000].•
-100 <= Node.val <= 1002022-02-28
228. Summary Ranges
Topic: Array
Difficulty: Easy
Problem:
You are given a sorted unique integer array
Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of
Each range
•
•
Example 1:
Example 2:
Constraints:
•
•
• All the values of
•
228. Summary Ranges
Topic: Array
Difficulty: Easy
Problem:
You are given a sorted unique integer array
nums.Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of
nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.Each range
[a,b] in the list should be output as:•
"a->b" if a != b•
"a" if a == bExample 1:
Input: nums = [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
Example 2:
Input: nums = [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
Constraints:
•
0 <= nums.length <= 20•
-2^31 <= nums[i] <= 2^31 - 1• All the values of
nums are unique.•
nums is sorted in ascending order.2022-03-01
338. Counting Bits
Topic: Dynamic Programming, Bit Manipulation
Difficulty: Easy
Problem:
Given an integer
Example 1:
Example 2:
Constraints:
•
Follow up:
• It is very easy to come up with a solution with a runtime of
• Can you do it without using any built-in function (i.e., like
338. Counting Bits
Topic: Dynamic Programming, Bit Manipulation
Difficulty: Easy
Problem:
Given an integer
n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.Example 1:
Input: n = 2
Output: [0,1,1]
Explanation:
0 --> 0
1 --> 1
2 --> 10
Example 2:
Input: n = 5
Output: [0,1,1,2,1,2]
Explanation:
0 --> 0
1 --> 1
2 --> 10
3 --> 11
4 --> 100
5 --> 101
Constraints:
•
0 <= n <= 10^5Follow up:
• It is very easy to come up with a solution with a runtime of
O(n log n). Can you do it in linear time O(n) and possibly in a single pass?• Can you do it without using any built-in function (i.e., like
__builtin_popcount in C++)?2022-03-02
392. Is Subsequence
Topic: Two Pointers, String, Dynamic Programming
Difficulty: Easy
Problem:
Given two strings
A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e.,
Example 1:
Example 2:
Constraints:
•
•
•
Follow up: Suppose there are lots of incoming
392. Is Subsequence
Topic: Two Pointers, String, Dynamic Programming
Difficulty: Easy
Problem:
Given two strings
s and t, return true if s is a subsequence of t, or false otherwise.A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e.,
"ace" is a subsequence of "abcde" while "aec" is not).Example 1:
Input: s = "abc", t = "ahbgdc"
Output: true
Example 2:
Input: s = "axc", t = "ahbgdc"
Output: false
Constraints:
•
0 <= s.length <= 100•
0 <= t.length <= 10^4•
s and t consist only of lowercase English letters.Follow up: Suppose there are lots of incoming
s, say s_1, s_2, ..., s_k where k >= 10^9, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code?2022-03-03
413. Arithmetic Slices
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
An integer array is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
• For example,
Given an integer array
A subarray is a contiguous subsequence of the array.
Example 1:
Example 2:
Constraints:
•
•
413. Arithmetic Slices
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
An integer array is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
• For example,
[1,3,5,7,9], [7,7,7,7], and [3,-1,-5,-9] are arithmetic sequences.Given an integer array
nums, return the number of arithmetic subarrays of nums.A subarray is a contiguous subsequence of the array.
Example 1:
Input: nums = [1,2,3,4]
Output: 3
Explanation: We have 3 arithmetic slices in nums: [1, 2, 3], [2, 3, 4] and [1,2,3,4] itself.
Example 2:
Input: nums = [1]
Output: 0
Constraints:
•
1 <= nums.length <= 5000•
-1000 <= nums[i] <= 10002022-03-04
799. Champagne Tower
Topic: Dynamic Programming
Difficulty: Medium
Problem:
We stack glasses in a pyramid, where the first row has
Then, some champagne is poured into the first glass at the top. When the topmost glass is full, any excess liquid poured will fall equally to the glass immediately to the left and right of it. When those glasses become full, any excess champagne will fall equally to the left and right of those glasses, and so on. (A glass at the bottom row has its excess champagne fall on the floor.)
For example, after one cup of champagne is poured, the top most glass is full. After two cups of champagne are poured, the two glasses on the second row are half full. After three cups of champagne are poured, those two cups become full - there are 3 full glasses total now. After four cups of champagne are poured, the third row has the middle glass half full, and the two outside glasses are a quarter full, as pictured below.
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/09/tower.png
Now after pouring some non-negative integer cups of champagne, return how full the
Example 1:
Example 2:
Example 3:
Constraints:
•
•
799. Champagne Tower
Topic: Dynamic Programming
Difficulty: Medium
Problem:
We stack glasses in a pyramid, where the first row has
1 glass, the second row has 2 glasses, and so on until the 100^th row. Each glass holds one cup of champagne.Then, some champagne is poured into the first glass at the top. When the topmost glass is full, any excess liquid poured will fall equally to the glass immediately to the left and right of it. When those glasses become full, any excess champagne will fall equally to the left and right of those glasses, and so on. (A glass at the bottom row has its excess champagne fall on the floor.)
For example, after one cup of champagne is poured, the top most glass is full. After two cups of champagne are poured, the two glasses on the second row are half full. After three cups of champagne are poured, those two cups become full - there are 3 full glasses total now. After four cups of champagne are poured, the third row has the middle glass half full, and the two outside glasses are a quarter full, as pictured below.
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/09/tower.png
Now after pouring some non-negative integer cups of champagne, return how full the
j^th glass in the i^th row is (both i and j are 0-indexed.)Example 1:
Input: poured = 1, query\_row = 1, query\_glass = 1
Output: 0.00000
Explanation: We poured 1 cup of champange to the top glass of the tower (which is indexed as (0, 0)). There will be no excess liquid so all the glasses under the top glass will remain empty.
Example 2:
Input: poured = 2, query\_row = 1, query\_glass = 1
Output: 0.50000
Explanation: We poured 2 cups of champange to the top glass of the tower (which is indexed as (0, 0)). There is one cup of excess liquid. The glass indexed as (1, 0) and the glass indexed as (1, 1) will share the excess liquid equally, and each will get half cup of champange.
Example 3:
Input: poured = 100000009, query\_row = 33, query\_glass = 17
Output: 1.00000
Constraints:
•
0 <= poured <= 10^9•
0 <= query_glass <= query_row < 1002022-03-05
740. Delete and Earn
Topic: Array, Hash Table, Dynamic Programming
Difficulty: Medium
Problem:
You are given an integer array
• Pick any
Return the maximum number of points you can earn by applying the above operation some number of times.
Example 1:
Example 2:
Constraints:
•
•
740. Delete and Earn
Topic: Array, Hash Table, Dynamic Programming
Difficulty: Medium
Problem:
You are given an integer array
nums. You want to maximize the number of points you get by performing the following operation any number of times:• Pick any
nums[i] and delete it to earn nums[i] points. Afterwards, you must delete every element equal to nums[i] - 1 and every element equal to nums[i] + 1.Return the maximum number of points you can earn by applying the above operation some number of times.
Example 1:
Input: nums = [3,4,2]
Output: 6
Explanation: You can perform the following operations:
- Delete 4 to earn 4 points. Consequently, 3 is also deleted. nums = [2].
- Delete 2 to earn 2 points. nums = [].
You earn a total of 6 points.
Example 2:
Input: nums = [2,2,3,3,3,4]
Output: 9
Explanation: You can perform the following operations:
- Delete a 3 to earn 3 points. All 2's and 4's are also deleted. nums = [3,3].
- Delete a 3 again to earn 3 points. nums = [3].
- Delete a 3 once more to earn 3 points. nums = [].
You earn a total of 9 points.
Constraints:
•
1 <= nums.length <= 2 * 10^4•
1 <= nums[i] <= 10^42022-03-06
1359. Count All Valid Pickup and Delivery Options
Topic: Math, Dynamic Programming, Combinatorics
Difficulty: Hard
Problem:
Given
Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i).
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Example 2:
Example 3:
Constraints:
•
1359. Count All Valid Pickup and Delivery Options
Topic: Math, Dynamic Programming, Combinatorics
Difficulty: Hard
Problem:
Given
n orders, each order consist in pickup and delivery services. Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i).
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: n = 1
Output: 1
Explanation: Unique order (P1, D1), Delivery 1 always is after of Pickup 1.
Example 2:
Input: n = 2
Output: 6
Explanation: All possible orders:
(P1,P2,D1,D2), (P1,P2,D2,D1), (P1,D1,P2,D2), (P2,P1,D1,D2), (P2,P1,D2,D1) and (P2,D2,P1,D1).
This is an invalid order (P1,D2,P2,D1) because Pickup 2 is after of Delivery 2.
Example 3:
Input: n = 3
Output: 90
Constraints:
•
1 <= n <= 5002022-03-07
21. Merge Two Sorted Lists
Topic: Linked List, Recursion
Difficulty: Easy
Problem:
You are given the heads of two sorted linked lists
Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.
Return the head of the merged linked list.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/03/merge_ex1.jpg
Example 2:
Example 3:
Constraints:
• The number of nodes in both lists is in the range
•
• Both
21. Merge Two Sorted Lists
Topic: Linked List, Recursion
Difficulty: Easy
Problem:
You are given the heads of two sorted linked lists
list1 and list2.Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.
Return the head of the merged linked list.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/03/merge_ex1.jpg
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]
Example 2:
Input: list1 = [], list2 = []
Output: []
Example 3:
Input: list1 = [], list2 = [0]
Output: [0]
Constraints:
• The number of nodes in both lists is in the range
[0, 50].•
-100 <= Node.val <= 100• Both
list1 and list2 are sorted in non-decreasing order.2022-03-08
141. Linked List Cycle
Topic: Hash Table, Linked List, Two Pointers
Difficulty: Easy
Problem:
Given
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the
Return
Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png
Constraints:
• The number of the nodes in the list is in the range
•
•
Follow up: Can you solve it using
141. Linked List Cycle
Topic: Hash Table, Linked List, Two Pointers
Difficulty: Easy
Problem:
Given
head, the head of a linked list, determine if the linked list has a cycle in it.There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the
next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.Return
true if there is a cycle in the linked list. Otherwise, return false.Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
Example 3:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
Constraints:
• The number of the nodes in the list is in the range
[0, 10^4].•
-10^5 <= Node.val <= 10^5•
pos is -1 or a valid index in the linked-list.Follow up: Can you solve it using
O(1) (i.e. constant) memory?2022-03-09
82. Remove Duplicates from Sorted List II
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/04/linkedlist1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/01/04/linkedlist2.jpg
Constraints:
• The number of nodes in the list is in the range
•
• The list is guaranteed to be sorted in ascending order.
82. Remove Duplicates from Sorted List II
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
Given the
head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/04/linkedlist1.jpg
Input: head = [1,2,3,3,4,4,5]
Output: [1,2,5]
Example 2:
Image: https://assets.leetcode.com/uploads/2021/01/04/linkedlist2.jpg
Input: head = [1,1,1,2,3]
Output: [2,3]
Constraints:
• The number of nodes in the list is in the range
[0, 300].•
-100 <= Node.val <= 100• The list is guaranteed to be sorted in ascending order.
2022-03-10
2. Add Two Numbers
Topic: Linked List, Math, Recursion
Difficulty: Medium
Problem:
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, 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/2020/10/02/addtwonumber1.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.
2. Add Two Numbers
Topic: Linked List, Math, Recursion
Difficulty: Medium
Problem:
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, 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/2020/10/02/addtwonumber1.jpg
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
Example 2:
Input: l1 = [0], l2 = [0]
Output: [0]
Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
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.