2022-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] <= 1002022-01-09
1041. Robot Bounded In Circle
Topic: Math, String, Simulation
Difficulty: Medium
Problem:
On an infinite plane, a robot initially stands at
•
•
•
The robot performs the
Return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1041. Robot Bounded In Circle
Topic: Math, String, Simulation
Difficulty: Medium
Problem:
On an infinite plane, a robot initially stands at
(0, 0) and faces north. The robot can receive one of three instructions:•
"G": go straight 1 unit;•
"L": turn 90 degrees to the left;•
"R": turn 90 degrees to the right.The robot performs the
instructions given in order, and repeats them forever.Return
true if and only if there exists a circle in the plane such that the robot never leaves the circle.Example 1:
Input: instructions = "GGLLGG"
Output: true
Explanation: The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).
When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.
Example 2:
Input: instructions = "GG"
Output: false
Explanation: The robot moves north indefinitely.
Example 3:
Input: instructions = "GL"
Output: true
Explanation: The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ...
Constraints:
•
1 <= instructions.length <= 100•
instructions[i] is 'G', 'L' or, 'R'.2022-01-10
67. Add Binary
Topic: Math, String, Bit Manipulation, Simulation
Difficulty: Easy
Problem:
Given two binary strings
Example 1:
Example 2:
Constraints:
•
•
• Each string does not contain leading zeros except for the zero itself.
67. Add Binary
Topic: Math, String, Bit Manipulation, Simulation
Difficulty: Easy
Problem:
Given two binary strings
a and b, return their sum as a binary string.Example 1:
Input: a = "11", b = "1"
Output: "100"
Example 2:
Input: a = "1010", b = "1011"
Output: "10101"
Constraints:
•
1 <= a.length, b.length <= 10^4•
a and b consist only of '0' or '1' characters.• Each string does not contain leading zeros except for the zero itself.
2022-01-11
1022. Sum of Root To Leaf Binary Numbers
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Easy
Problem:
You are given the
• For example, if the path is
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf. Return the sum of these numbers.
The test cases are generated so that the answer fits in a 32-bits integer.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/04/04/sum-of-root-to-leaf-binary-numbers.png
Example 2:
Constraints:
• The number of nodes in the tree is in the range
•
1022. Sum of Root To Leaf Binary Numbers
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Easy
Problem:
You are given the
root of a binary tree where each node has a value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit.• For example, if the path is
0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.For all leaves in the tree, consider the numbers represented by the path from the root to that leaf. Return the sum of these numbers.
The test cases are generated so that the answer fits in a 32-bits integer.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/04/04/sum-of-root-to-leaf-binary-numbers.png
Input: root = [1,0,1,0,1,0,1]
Output: 22
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
Example 2:
Input: root = [0]
Output: 0
Constraints:
• The number of nodes in the tree is in the range
[1, 1000].•
Node.val is 0 or 1.2022-01-12
701. Insert into a Binary Search Tree
Topic: Tree, Binary Search Tree, Binary Tree
Difficulty: Medium
Problem:
You are given the
Notice that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/05/insertbst.jpg
Example 2:
Example 3:
Constraints:
• The number of nodes in the tree will be in the range
•
• All the values
•
• It's guaranteed that
701. Insert into a Binary Search Tree
Topic: Tree, Binary Search Tree, Binary Tree
Difficulty: Medium
Problem:
You are given the
root node of a binary search tree (BST) and a value to insert into the tree. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.Notice that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/05/insertbst.jpg
Input: root = [4,2,7,1,3], val = 5
Output: [4,2,7,1,3,5]
Explanation: Another accepted tree is:
Image: [https://assets.leetcode.com/uploads/2020/10/05/bst.jpg](https://assets.leetcode.com/uploads/2020/10/05/bst.jpg)
Example 2:
Input: root = [40,20,60,10,30,50,70], val = 25
Output: [40,20,60,10,30,50,70,null,null,25]
Example 3:
Input: root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
Output: [4,2,7,1,3,5]
Constraints:
• The number of nodes in the tree will be in the range
[0, 10^4].•
-10^8 <= Node.val <= 10^8• All the values
Node.val are unique.•
-10^8 <= val <= 10^8• It's guaranteed that
val does not exist in the original BST.2022-01-13
452. Minimum Number of Arrows to Burst Balloons
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array
Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with
Given the array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
452. Minimum Number of Arrows to Burst Balloons
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array
points where points[i] = [x_start, x_end] denotes a balloon whose horizontal diameter stretches between x_start and x_end. You do not know the exact y-coordinates of the balloons.Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with
x_start and x_end is burst by an arrow shot at x if x_start <= x <= x_end. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.Given the array
points, return the minimum number of arrows that must be shot to burst all balloons.Example 1:
Input: points = [[10,16],[2,8],[1,6],[7,12]]
Output: 2
Explanation: The balloons can be burst by 2 arrows:
- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
Example 2:
Input: points = [[1,2],[3,4],[5,6],[7,8]]
Output: 4
Explanation: One arrow needs to be shot for each balloon for a total of 4 arrows.
Example 3:
Input: points = [[1,2],[2,3],[3,4],[4,5]]
Output: 2
Explanation: The balloons can be burst by 2 arrows:
- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].
Constraints:
•
1 <= points.length <= 10^5•
points[i].length == 2•
-2^31 <= x_start < x_end <= 2^31 - 12022-01-14
8. String to Integer (atoi)
Topic: String
Difficulty: Medium
Problem:
Implement the
The algorithm for
1. Read in and ignore any leading whitespace.
2. Check if the next character (if not already at the end of the string) is
3. Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored.
4. Convert these digits into an integer (i.e.
5. If the integer is out of the 32-bit signed integer range
6. Return the integer as the final result.
Note:
• Only the space character
• Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
8. String to Integer (atoi)
Topic: String
Difficulty: Medium
Problem:
Implement the
myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function).The algorithm for
myAtoi(string s) is as follows:1. Read in and ignore any leading whitespace.
2. Check if the next character (if not already at the end of the string) is
'-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.3. Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored.
4. Convert these digits into an integer (i.e.
"123" -> 123, "0032" -> 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).5. If the integer is out of the 32-bit signed integer range
[-2^31, 2^31 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -2^31 should be clamped to -2^31, and integers greater than 2^31 - 1 should be clamped to 2^31 - 1.6. Return the integer as the final result.
Note:
• Only the space character
' ' is considered a whitespace character.• Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.
Example 1:
Input: s = "42"
Output: 42
Explanation: The underlined characters are what is read in, the caret is the current reader position.
Step 1: "42" (no characters read because there is no leading whitespace)
^
Step 2: "42" (no characters read because there is neither a '-' nor '+')
^
Step 3: "42" ("42" is read in)
^
The parsed integer is 42.
Since 42 is in the range [-2^31, 2^31 - 1], the final result is 42.
Example 2:
Input: s = " -42"
Output: -42
Explanation:
Step 1: " -42" (leading whitespace is read and ignored)
^
Step 2: " -42" ('-' is read, so the result should be negative)
^
Step 3: " -42" ("42" is read in)
^
The parsed integer is -42.
Since -42 is in the range [-2^31, 2^31 - 1], the final result is -42.
Example 3:
Input: s = "4193 with words"
Output: 4193
Explanation:
Step 1: "4193 with words" (no characters read because there is no leading whitespace)
^
Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+')
^
Step 3: "4193 with words" ("4193" is read in; reading stops because the next character is a non-digit)
^
The parsed integer is 4193.
Since 4193 is in the range [-2^31, 2^31 - 1], the final result is 4193.
Constraints:
•
0 <= s.length <= 200•
s consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'.2022-01-15
1345. Jump Game IV
Topic: Array, Hash Table, Breadth-First Search
Difficulty: Hard
Problem:
Given an array of integers
In one step you can jump from index
•
•
•
Return the minimum number of steps to reach the last index of the array.
Notice that you can not jump outside of the array at any time.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1345. Jump Game IV
Topic: Array, Hash Table, Breadth-First Search
Difficulty: Hard
Problem:
Given an array of integers
arr, you are initially positioned at the first index of the array.In one step you can jump from index
i to index:•
i + 1 where: i + 1 < arr.length.•
i - 1 where: i - 1 >= 0.•
j where: arr[i] == arr[j] and i != j.Return the minimum number of steps to reach the last index of the array.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [100,-23,-23,404,100,23,23,23,3,404]
Output: 3
Explanation: You need three jumps from index 0 --> 4 --> 3 --> 9. Note that index 9 is the last index of the array.
Example 2:
Input: arr = [7]
Output: 0
Explanation: Start index is the last index. You do not need to jump.
Example 3:
Input: arr = [7,6,9,6,9,6,9,7]
Output: 1
Explanation: You can jump directly from index 0 to index 7 which is last index of the array.
Constraints:
•
1 <= arr.length <= 5 * 10^4•
-10^8 <= arr[i] <= 10^82022-01-16
849. Maximize Distance to Closest Person
Topic: Array
Difficulty: Medium
Problem:
You are given an array representing a row of
There is at least one empty seat, and at least one person sitting.
Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.
Return that maximum distance to the closest person.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/10/distance.jpg
Example 2:
Example 3:
Constraints:
•
•
• At least one seat is empty.
• At least one seat is occupied.
849. Maximize Distance to Closest Person
Topic: Array
Difficulty: Medium
Problem:
You are given an array representing a row of
seats where seats[i] = 1 represents a person sitting in the i^th seat, and seats[i] = 0 represents that the i^th seat is empty (0-indexed).There is at least one empty seat, and at least one person sitting.
Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.
Return that maximum distance to the closest person.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/10/distance.jpg
Input: seats = [1,0,0,0,1,0,1]
Output: 2
Explanation:
If Alex sits in the second open seat (i.e. seats[2]), then the closest person has distance 2.
If Alex sits in any other open seat, the closest person has distance 1.
Thus, the maximum distance to the closest person is 2.
Example 2:
Input: seats = [1,0,0,0]
Output: 3
Explanation:
If Alex sits in the last seat (i.e. seats[3]), the closest person is 3 seats away.
This is the maximum distance possible, so the answer is 3.
Example 3:
Input: seats = [0,1]
Output: 1
Constraints:
•
2 <= seats.length <= 2 * 10^4•
seats[i] is 0 or 1.• At least one seat is empty.
• At least one seat is occupied.
2022-01-17
290. Word Pattern
Topic: Hash Table, String
Difficulty: Easy
Problem:
Given a
Here follow means a full match, such that there is a bijection between a letter in
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
• All the words in
290. Word Pattern
Topic: Hash Table, String
Difficulty: Easy
Problem:
Given a
pattern and a string s, find if s follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in
pattern and a non-empty word in s.Example 1:
Input: pattern = "abba", s = "dog cat cat dog"
Output: true
Example 2:
Input: pattern = "abba", s = "dog cat cat fish"
Output: false
Example 3:
Input: pattern = "aaaa", s = "dog cat cat dog"
Output: false
Constraints:
•
1 <= pattern.length <= 300•
pattern contains only lower-case English letters.•
1 <= s.length <= 3000•
s contains only lowercase English letters and spaces ' '.•
s does not contain any leading or trailing spaces.• All the words in
s are separated by a single space.2022-01-18
605. Can Place Flowers
Topic: Array, Greedy
Difficulty: Easy
Problem:
You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
• There are no two adjacent flowers in
•
605. Can Place Flowers
Topic: Array, Greedy
Difficulty: Easy
Problem:
You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.
Given an integer array
flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule.Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: true
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2
Output: false
Constraints:
•
1 <= flowerbed.length <= 2 * 10^4•
flowerbed[i] is 0 or 1.• There are no two adjacent flowers in
flowerbed.•
0 <= n <= flowerbed.length2022-01-19
142. Linked List Cycle II
Topic: Hash Table, Linked List, Two Pointers
Difficulty: Medium
Problem:
Given the
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
Do not modify the linked list.
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
142. Linked List Cycle II
Topic: Hash Table, Linked List, Two Pointers
Difficulty: Medium
Problem:
Given the
head of a linked list, return the node where the cycle begins. If there is no cycle, return null.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 (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter.Do not modify the linked list.
Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png
Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png
Input: head = [1,2], pos = 0
Output: tail connects to node index 0
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Image: https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png
Input: head = [1], pos = -1
Output: no cycle
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-01-20
875. Koko Eating Bananas
Topic: Array, Binary Search
Difficulty: Medium
Problem:
Koko loves to eat bananas. There are
Koko can decide her bananas-per-hour eating speed of
Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.
Return the minimum integer
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
875. Koko Eating Bananas
Topic: Array, Binary Search
Difficulty: Medium
Problem:
Koko loves to eat bananas. There are
n piles of bananas, the i^th pile has piles[i] bananas. The guards have gone and will come back in h hours.Koko can decide her bananas-per-hour eating speed of
k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour.Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.
Return the minimum integer
k such that she can eat all the bananas within h hours.Example 1:
Input: piles = [3,6,7,11], h = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], h = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], h = 6
Output: 23
Constraints:
•
1 <= piles.length <= 10^4•
piles.length <= h <= 10^9•
1 <= piles[i] <= 10^92022-01-21
134. Gas Station
Topic: Array, Greedy
Difficulty: Medium
Problem:
There are
You have a car with an unlimited gas tank and it costs
Given two integer arrays
Example 1:
Example 2:
Constraints:
•
•
•
•
134. Gas Station
Topic: Array, Greedy
Difficulty: Medium
Problem:
There are
n gas stations along a circular route, where the amount of gas at the i^th station is gas[i].You have a car with an unlimited gas tank and it costs
cost[i] of gas to travel from the i^th station to its next (i + 1)^th station. You begin the journey with an empty tank at one of the gas stations.Given two integer arrays
gas and cost, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be uniqueExample 1:
Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
Output: 3
Explanation:
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 4. Your tank = 4 - 1 + 5 = 8
Travel to station 0. Your tank = 8 - 2 + 1 = 7
Travel to station 1. Your tank = 7 - 3 + 2 = 6
Travel to station 2. Your tank = 6 - 4 + 3 = 5
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
Therefore, return 3 as the starting index.
Example 2:
Input: gas = [2,3,4], cost = [3,4,3]
Output: -1
Explanation:
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 0. Your tank = 4 - 3 + 2 = 3
Travel to station 1. Your tank = 3 - 3 + 3 = 3
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
Therefore, you can't travel around the circuit once no matter where you start.
Constraints:
•
gas.length == n•
cost.length == n•
1 <= n <= 10^5•
0 <= gas[i], cost[i] <= 10^42022-01-22
1510. Stone Game IV
Topic: Math, Dynamic Programming, Game Theory
Difficulty: Hard
Problem:
Alice and Bob take turns playing a game, with Alice starting first.
Initially, there are
Also, if a player cannot make a move, he/she loses the game.
Given a positive integer
Example 1:
Example 2:
Example 3:
Constraints:
•
1510. Stone Game IV
Topic: Math, Dynamic Programming, Game Theory
Difficulty: Hard
Problem:
Alice and Bob take turns playing a game, with Alice starting first.
Initially, there are
n stones in a pile. On each player's turn, that player makes a move consisting of removing any non-zero square number of stones in the pile.Also, if a player cannot make a move, he/she loses the game.
Given a positive integer
n, return true if and only if Alice wins the game otherwise return false, assuming both players play optimally.Example 1:
Input: n = 1
Output: true
Explanation: Alice can remove 1 stone winning the game because Bob doesn't have any moves.
Example 2:
Input: n = 2
Output: false
Explanation: Alice can only remove 1 stone, after that Bob removes the last one winning the game (2 -> 1 -> 0).
Example 3:
Input: n = 4
Output: true
Explanation: n is already a perfect square, Alice can win with one move, removing 4 stones (4 -> 0).
Constraints:
•
1 <= n <= 10^52022-01-23
1291. Sequential Digits
Topic: Enumeration
Difficulty: Medium
Problem:
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range
Example 1:
Example 2:
Constraints:
•
1291. Sequential Digits
Topic: Enumeration
Difficulty: Medium
Problem:
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range
[low, high] inclusive that have sequential digits.Example 1:
Input: low = 100, high = 300
Output: [123,234]
Example 2:
Input: low = 1000, high = 13000
Output: [1234,2345,3456,4567,5678,6789,12345]
Constraints:
•
10 <= low <= high <= 10^92022-01-24
520. Detect Capital
Topic: String
Difficulty: Easy
Problem:
We define the usage of capitals in a word to be right when one of the following cases holds:
• All letters in this word are capitals, like
• All letters in this word are not capitals, like
• Only the first letter in this word is capital, like
Given a string
Example 1:
Example 2:
Constraints:
•
•
520. Detect Capital
Topic: String
Difficulty: Easy
Problem:
We define the usage of capitals in a word to be right when one of the following cases holds:
• All letters in this word are capitals, like
"USA".• All letters in this word are not capitals, like
"leetcode".• Only the first letter in this word is capital, like
"Google".Given a string
word, return true if the usage of capitals in it is right.Example 1:
Input: word = "USA"
Output: true
Example 2:
Input: word = "FlaG"
Output: false
Constraints:
•
1 <= word.length <= 100•
word consists of lowercase and uppercase English letters.2022-01-25
941. Valid Mountain Array
Topic: Array
Difficulty: Easy
Problem:
Given an array of integers
Recall that arr is a mountain array if and only if:
•
• There exists some
•
•
Image: https://assets.leetcode.com/uploads/2019/10/20/hint_valid_mountain_array.png
Example 1:
Example 2:
Example 3:
Constraints:
•
•
941. Valid Mountain Array
Topic: Array
Difficulty: Easy
Problem:
Given an array of integers
arr, return true if and only if it is a valid mountain array.Recall that arr is a mountain array if and only if:
•
arr.length >= 3• There exists some
i with 0 < i < arr.length - 1 such that:•
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]•
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]Image: https://assets.leetcode.com/uploads/2019/10/20/hint_valid_mountain_array.png
Example 1:
Input: arr = [2,1]
Output: false
Example 2:
Input: arr = [3,5,5]
Output: false
Example 3:
Input: arr = [0,3,2,1]
Output: true
Constraints:
•
1 <= arr.length <= 10^4•
0 <= arr[i] <= 10^42022-01-26
1305. All Elements in Two Binary Search Trees
Topic: Tree, Depth-First Search, Binary Search Tree, Sorting, Binary Tree
Difficulty: Medium
Problem:
Given two binary search trees
Example 1:
Image: https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png
Constraints:
• The number of nodes in each tree is in the range
•
1305. All Elements in Two Binary Search Trees
Topic: Tree, Depth-First Search, Binary Search Tree, Sorting, Binary Tree
Difficulty: Medium
Problem:
Given two binary search trees
root1 and root2, return a list containing all the integers from both trees sorted in ascending order.Example 1:
Image: https://assets.leetcode.com/uploads/2019/12/18/q2-e1.png
Input: root1 = [2,1,4], root2 = [1,0,3]
Output: [0,1,1,2,3,4]
Example 2:
Image: https://assets.leetcode.com/uploads/2019/12/18/q2-e5-.png
Input: root1 = [1,null,8], root2 = [8,1]
Output: [1,1,8,8]
Constraints:
• The number of nodes in each tree is in the range
[0, 5000].•
-10^5 <= Node.val <= 10^52022-01-27
421. Maximum XOR of Two Numbers in an Array
Topic: Array, Hash Table, Bit Manipulation, Trie
Difficulty: Medium
Problem:
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
421. Maximum XOR of Two Numbers in an Array
Topic: Array, Hash Table, Bit Manipulation, Trie
Difficulty: Medium
Problem:
Given an integer array
nums, return the maximum result of nums[i] XOR nums[j], where 0 <= i <= j < n.Example 1:
Input: nums = [3,10,5,25,2,8]
Output: 28
Explanation: The maximum result is 5 XOR 25 = 28.
Example 2:
Input: nums = [14,70,53,83,49,91,36,80,92,51,66,70]
Output: 127
Constraints:
•
1 <= nums.length <= 2 * 10^5•
0 <= nums[i] <= 2^31 - 1