2021-12-20
1200. Minimum Absolute Difference
Topic: Array, Sorting
Difficulty: Easy
Problem:
Given an array of distinct integers
Return a list of pairs in ascending order(with respect to pairs), each pair
•
•
•
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1200. Minimum Absolute Difference
Topic: Array, Sorting
Difficulty: Easy
Problem:
Given an array of distinct integers
arr, find all pairs of elements with the minimum absolute difference of any two elements. Return a list of pairs in ascending order(with respect to pairs), each pair
[a, b] follows•
a, b are from arr•
a < b•
b - a equals to the minimum absolute difference of any two elements in arrExample 1:
Input: arr = [4,2,1,3]
Output: [[1,2],[2,3],[3,4]]
Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.
Example 2:
Input: arr = [1,3,6,10,15]
Output: [[1,3]]
Example 3:
Input: arr = [3,8,-10,23,19,-4,-14,27]
Output: [[-14,-10],[19,23],[23,27]]
Constraints:
•
2 <= arr.length <= 10^5•
-10^6 <= arr[i] <= 10^62021-12-21
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?
2021-12-22
143. Reorder List
Topic: Linked List, Two Pointers, Stack, Recursion
Difficulty: Medium
Problem:
You are given the head of a singly linked-list. The list can be represented as:
Reorder the list to be on the following form:
You may not modify the values in the list's nodes. Only nodes themselves may be changed.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg
Constraints:
• The number of nodes in the list is in the range
•
143. Reorder List
Topic: Linked List, Two Pointers, Stack, Recursion
Difficulty: Medium
Problem:
You are given the head of a singly linked-list. The list can be represented as:
L_0 → L_1 → … → L_n - 1 → L_n
Reorder the list to be on the following form:
L_0 → L_n → L_1 → L_n - 1 → L_2 → L_n - 2 → …
You may not modify the values in the list's nodes. Only nodes themselves may be changed.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg
Input: head = [1,2,3,4]
Output: [1,4,2,3]
Example 2:
Image: https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg
Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]
Constraints:
• The number of nodes in the list is in the range
[1, 5 * 10^4].•
1 <= Node.val <= 10002021-12-23
210. Course Schedule II
Topic: Depth-First Search, Breadth-First Search, Graph, Topological Sort
Difficulty: Medium
Problem:
There are a total of
• For example, the pair
Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
• All the pairs
210. Course Schedule II
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 the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.
Example 1:
Input: numCourses = 2, prerequisites = [[1,0]]
Output: [0,1]
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1].
Example 2:
Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
Output: [0,2,1,3]
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
Example 3:
Input: numCourses = 1, prerequisites = []
Output: [0]
Constraints:
•
1 <= numCourses <= 2000•
0 <= prerequisites.length <= numCourses * (numCourses - 1)•
prerequisites[i].length == 2•
0 <= a_i, b_i < numCourses•
a_i != b_i• All the pairs
[a_i, b_i] are distinct.2021-12-24
56. Merge Intervals
Topic: Array, Sorting
Difficulty: Medium
Problem:
Given an array of
Example 1:
Example 2:
Constraints:
•
•
•
56. Merge Intervals
Topic: Array, Sorting
Difficulty: Medium
Problem:
Given an array of
intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.Example 1:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
Example 2:
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
Constraints:
•
1 <= intervals.length <= 10^4•
intervals[i].length == 2•
0 <= start_i <= end_i <= 10^42021-12-25
227. Basic Calculator II
Topic: Math, String, Stack
Difficulty: Medium
Problem:
Given a string
The integer division should truncate toward zero.
You may assume that the given expression is always valid. All intermediate results will be in the range of
Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
• All the integers in the expression are non-negative integers in the range
• The answer is guaranteed to fit in a 32-bit integer.
227. Basic Calculator II
Topic: Math, String, Stack
Difficulty: Medium
Problem:
Given a string
s which represents an expression, evaluate this expression and return its value. The integer division should truncate toward zero.
You may assume that the given expression is always valid. All intermediate results will be in the range of
[-2^31, 2^31 - 1].Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as
eval().Example 1:
Input: s = "3+2*2"
Output: 7
Example 2:
Input: s = " 3/2 "
Output: 1
Example 3:
Input: s = " 3+5 / 2 "
Output: 5
Constraints:
•
1 <= s.length <= 3 * 10^5•
s consists of integers and operators ('+', '-', '*', '/') separated by some number of spaces.•
s represents a valid expression.• All the integers in the expression are non-negative integers in the range
[0, 2^31 - 1].• The answer is guaranteed to fit in a 32-bit integer.
2021-12-26
973. K Closest Points to Origin
Topic: Array, Math, Divide and Conquer, Geometry, Sorting, Heap (Priority Queue), Quickselect
Difficulty: Medium
Problem:
Given an array of
The distance between two points on the X-Y plane is the Euclidean distance (i.e.,
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in).
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/03/closestplane1.jpg
Example 2:
Constraints:
•
•
973. K Closest Points to Origin
Topic: Array, Math, Divide and Conquer, Geometry, Sorting, Heap (Priority Queue), Quickselect
Difficulty: Medium
Problem:
Given an array of
points where points[i] = [x_i, y_i] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0).The distance between two points on the X-Y plane is the Euclidean distance (i.e.,
√(x_1 - x_2)^2 + (y_1 - y_2)^2).You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in).
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/03/closestplane1.jpg
Input: points = [[1,3],[-2,2]], k = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], k = 2
Output: [[3,3],[-2,4]]
Explanation: The answer [[-2,4],[3,3]] would also be accepted.
Constraints:
•
1 <= k <= points.length <= 10^4•
-10^4 < x_i, y_i < 10^42021-12-27
476. Number Complement
Topic: Bit Manipulation
Difficulty: Easy
Problem:
The complement of an integer is the integer you get when you flip all the
• For example, The integer
Given an integer
Example 1:
Example 2:
Constraints:
•
Note: This question is the same as 1009: <https://leetcode.com/problems/complement-of-base-10-integer/>
476. Number Complement
Topic: Bit Manipulation
Difficulty: Easy
Problem:
The complement of an integer is the integer you get when you flip all the
0's to 1's and all the 1's to 0's in its binary representation.• For example, The integer
5 is "101" in binary and its complement is "010" which is the integer 2.Given an integer
num, return its complement.Example 1:
Input: num = 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
Example 2:
Input: num = 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
Constraints:
•
1 <= num < 2^31Note: This question is the same as 1009: <https://leetcode.com/problems/complement-of-base-10-integer/>
2021-12-28
876. Middle of the Linked List
Topic: Linked List, Two Pointers
Difficulty: Easy
Problem:
Given the
If there are two middle nodes, return the second middle node.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/07/23/lc-midlist1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/07/23/lc-midlist2.jpg
Constraints:
• The number of nodes in the list is in the range
•
876. Middle of the Linked List
Topic: Linked List, Two Pointers
Difficulty: Easy
Problem:
Given the
head of a singly linked list, return the middle node of the linked list.If there are two middle nodes, return the second middle node.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/07/23/lc-midlist1.jpg
Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The middle node of the list is node 3.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/07/23/lc-midlist2.jpg
Input: head = [1,2,3,4,5,6]
Output: [4,5,6]
Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.
Constraints:
• The number of nodes in the list is in the range
[1, 100].•
1 <= Node.val <= 1002021-12-29
116. Populating Next Right Pointers in Each Node
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to
Initially, all next pointers are set to
Example 1:
Image: https://assets.leetcode.com/uploads/2019/02/14/116_sample.png
Example 2:
Constraints:
• The number of nodes in the tree is in the range
•
Follow-up:
• You may only use constant extra space.
• The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.
116. Populating Next Right Pointers in Each Node
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to
NULL.Initially, all next pointers are set to
NULL.Example 1:
Image: https://assets.leetcode.com/uploads/2019/02/14/116_sample.png
Input: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
Example 2:
Input: root = []
Output: []
Constraints:
• The number of nodes in the tree is in the range
[0, 2^12 - 1].•
-1000 <= Node.val <= 1000Follow-up:
• You may only use constant extra space.
• The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.
2021-12-30
1015. Smallest Integer Divisible by K
Topic: Hash Table, Math
Difficulty: Medium
Problem:
Given a positive integer
Return the length of
Note:
Example 1:
Example 2:
Example 3:
Constraints:
•
1015. Smallest Integer Divisible by K
Topic: Hash Table, Math
Difficulty: Medium
Problem:
Given a positive integer
k, you need to find the length of the smallest positive integer n such that n is divisible by k, and n only contains the digit 1.Return the length of
n. If there is no such n, return -1.Note:
n may not fit in a 64-bit signed integer.Example 1:
Input: k = 1
Output: 1
Explanation: The smallest answer is n = 1, which has length 1.
Example 2:
Input: k = 2
Output: -1
Explanation: There is no such positive integer n divisible by 2.
Example 3:
Input: k = 3
Output: 3
Explanation: The smallest answer is n = 111, which has length 3.
Constraints:
•
1 <= k <= 10^52021-12-31
1026. Maximum Difference Between Node and Ancestor
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
A node
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/09/tmp-tree.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/09/tmp-tree-1.jpg
Constraints:
• The number of nodes in the tree is in the range
•
1026. Maximum Difference Between Node and Ancestor
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, find the maximum value v for which there exist different nodes a and b where v = |a.val - b.val| and a is an ancestor of b.A node
a is an ancestor of b if either: any child of a is equal to b or any child of a is an ancestor of b.Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/09/tmp-tree.jpg
Input: root = [8,3,10,1,6,null,14,null,null,4,7,13]
Output: 7
Explanation: We have various ancestor-node differences, some of which are given below :
|8 - 3| = 5
|3 - 7| = 4
|8 - 1| = 7
|10 - 13| = 3
Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/09/tmp-tree-1.jpg
Input: root = [1,null,2,null,0,3]
Output: 3
Constraints:
• The number of nodes in the tree is in the range
[2, 5000].•
0 <= Node.val <= 10^52022-01-01
312. Burst Balloons
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
You are given
If you burst the
Return the maximum coins you can collect by bursting the balloons wisely.
Example 1:
Example 2:
Constraints:
•
•
•
312. Burst Balloons
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
You are given
n balloons, indexed from 0 to n - 1. Each balloon is painted with a number on it represented by an array nums. You are asked to burst all the balloons.If you burst the
i^th balloon, you will get nums[i - 1] * nums[i] * nums[i + 1] coins. If i - 1 or i + 1 goes out of bounds of the array, then treat it as if there is a balloon with a 1 painted on it.Return the maximum coins you can collect by bursting the balloons wisely.
Example 1:
Input: nums = [3,1,5,8]
Output: 167
Explanation:
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
Example 2:
Input: nums = [1,5]
Output: 10
Constraints:
•
n == nums.length•
1 <= n <= 500•
0 <= nums[i] <= 1002022-01-02
1010. Pairs of Songs With Total Durations Divisible by 60
Topic: Array, Hash Table, Counting
Difficulty: Medium
Problem:
You are given a list of songs where the i^th song has a duration of
Return the number of pairs of songs for which their total duration in seconds is divisible by
Example 1:
Example 2:
Constraints:
•
•
1010. Pairs of Songs With Total Durations Divisible by 60
Topic: Array, Hash Table, Counting
Difficulty: Medium
Problem:
You are given a list of songs where the i^th song has a duration of
time[i] seconds.Return the number of pairs of songs for which their total duration in seconds is divisible by
60. Formally, we want the number of indices i, j such that i < j with (time[i] + time[j]) % 60 == 0.Example 1:
Input: time = [30,20,150,100,40]
Output: 3
Explanation: Three pairs have a total duration divisible by 60:
(time[0] = 30, time[2] = 150): total duration 180
(time[1] = 20, time[3] = 100): total duration 120
(time[1] = 20, time[4] = 40): total duration 60
Example 2:
Input: time = [60,60,60]
Output: 3
Explanation: All three pairs have a total duration of 120, which is divisible by 60.
Constraints:
•
1 <= time.length <= 6 * 10^4•
1 <= time[i] <= 5002022-01-03
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.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 <= n2022-01-04
1009. Complement of Base 10 Integer
Topic: Bit Manipulation
Difficulty: Easy
Problem:
The complement of an integer is the integer you get when you flip all the
• For example, The integer
Given an integer
Example 1:
Example 2:
Example 3:
Constraints:
•
Note: This question is the same as 476: <https://leetcode.com/problems/number-complement/>
1009. Complement of Base 10 Integer
Topic: Bit Manipulation
Difficulty: Easy
Problem:
The complement of an integer is the integer you get when you flip all the
0's to 1's and all the 1's to 0's in its binary representation.• For example, The integer
5 is "101" in binary and its complement is "010" which is the integer 2.Given an integer
n, return its complement.Example 1:
Input: n = 5
Output: 2
Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.
Example 2:
Input: n = 7
Output: 0
Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.
Example 3:
Input: n = 10
Output: 5
Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.
Constraints:
•
0 <= n < 10^9Note: This question is the same as 476: <https://leetcode.com/problems/number-complement/>
2022-01-05
131. Palindrome Partitioning
Topic: String, Dynamic Programming, Backtracking
Difficulty: Medium
Problem:
Given a string
A palindrome string is a string that reads the same backward as forward.
Example 1:
Example 2:
Constraints:
•
•
131. Palindrome Partitioning
Topic: String, Dynamic Programming, Backtracking
Difficulty: Medium
Problem:
Given a string
s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.A palindrome string is a string that reads the same backward as forward.
Example 1:
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
Example 2:
Input: s = "a"
Output: [["a"]]
Constraints:
•
1 <= s.length <= 16•
s contains only lowercase English letters.2022-01-06
1094. Car Pooling
Topic: Array, Sorting, Heap (Priority Queue), Simulation, Prefix Sum
Difficulty: Medium
Problem:
There is a car with
You are given the integer
Return
Example 1:
Example 2:
Constraints:
•
•
•
•
•
1094. Car Pooling
Topic: Array, Sorting, Heap (Priority Queue), Simulation, Prefix Sum
Difficulty: Medium
Problem:
There is a car with
capacity empty seats. The vehicle only drives east (i.e., it cannot turn around and drive west).You are given the integer
capacity and an array trips where trip[i] = [numPassengers_i, from_i, to_i] indicates that the i^th trip has numPassengers_i passengers and the locations to pick them up and drop them off are from_i and to_i respectively. The locations are given as the number of kilometers due east from the car's initial location.Return
true if it is possible to pick up and drop off all passengers for all the given trips, or false otherwise.Example 1:
Input: trips = [[2,1,5],[3,3,7]], capacity = 4
Output: false
Example 2:
Input: trips = [[2,1,5],[3,3,7]], capacity = 5
Output: true
Constraints:
•
1 <= trips.length <= 1000•
trips[i].length == 3•
1 <= numPassengers_i <= 100•
0 <= from_i < to_i <= 1000•
1 <= capacity <= 10^52022-01-07
382. Linked List Random Node
Topic: Linked List, Math, Reservoir Sampling, Randomized
Difficulty: Medium
Problem:
Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen.
Implement the
•
•
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg
Constraints:
• The number of nodes in the linked list will be in the range
•
• At most
Follow up:
• What if the linked list is extremely large and its length is unknown to you?
• Could you solve this efficiently without using extra space?
382. Linked List Random Node
Topic: Linked List, Math, Reservoir Sampling, Randomized
Difficulty: Medium
Problem:
Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen.
Implement the
Solution class:•
Solution(ListNode head) Initializes the object with the integer array nums.•
int getRandom() Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be choosen.Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg
Input
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
Output
[null, 1, 3, 2, 2, 3]
Explanation
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
Constraints:
• The number of nodes in the linked list will be in the range
[1, 10^4].•
-10^4 <= Node.val <= 10^4• At most
10^4 calls will be made to getRandom.Follow up:
• What if the linked list is extremely large and its length is unknown to you?
• Could you solve this efficiently without using extra space?
2022-01-08
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] <= 100