2021-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] <= 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.