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.
2022-03-11
61. Rotate List
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg
Constraints:
• The number of nodes in the list is in the range
•
•
61. Rotate List
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
Given the
head of a linked list, rotate the list to the right by k places.Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg
Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg
Input: head = [0,1,2], k = 4
Output: [2,0,1]
Constraints:
• The number of nodes in the list is in the range
[0, 500].•
-100 <= Node.val <= 100•
0 <= k <= 2 * 10^92022-03-12
138. Copy List with Random Pointer
Topic: Hash Table, Linked List
Difficulty: Medium
Problem:
A linked list of length
Construct a deep copy of the list. The deep copy should consist of exactly
For example, if there are two nodes
Return the head of the copied linked list.
The linked list is represented in the input/output as a list of
•
•
Your code will only be given the
Example 1:
Image: https://assets.leetcode.com/uploads/2019/12/18/e1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2019/12/18/e2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2019/12/18/e3.png
Constraints:
•
•
•
138. Copy List with Random Pointer
Topic: Hash Table, Linked List
Difficulty: Medium
Problem:
A linked list of length
n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.Construct a deep copy of the list. The deep copy should consist of exactly
n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.For example, if there are two nodes
X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.Return the head of the copied linked list.
The linked list is represented in the input/output as a list of
n nodes. Each node is represented as a pair of [val, random_index] where:•
val: an integer representing Node.val•
random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.Your code will only be given the
head of the original linked list.Example 1:
Image: https://assets.leetcode.com/uploads/2019/12/18/e1.png
Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
Example 2:
Image: https://assets.leetcode.com/uploads/2019/12/18/e2.png
Input: head = [[1,1],[2,1]]
Output: [[1,1],[2,1]]
Example 3:
Image: https://assets.leetcode.com/uploads/2019/12/18/e3.png
Input: head = [[3,null],[3,0],[3,null]]
Output: [[3,null],[3,0],[3,null]]
Constraints:
•
0 <= n <= 1000•
-10^4 <= Node.val <= 10^4•
Node.random is null or is pointing to some node in the linked list.2022-03-13
20. Valid Parentheses
Topic: String, Stack
Difficulty: Easy
Problem:
Given a string
An input string is valid if:
1. Open brackets must be closed by the same type of brackets.
2. Open brackets must be closed in the correct order.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
20. Valid Parentheses
Topic: String, Stack
Difficulty: Easy
Problem:
Given a string
s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.An input string is valid if:
1. Open brackets must be closed by the same type of brackets.
2. Open brackets must be closed in the correct order.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Constraints:
•
1 <= s.length <= 10^4•
s consists of parentheses only '()[]{}'.2022-03-14
71. Simplify Path
Topic: String, Stack
Difficulty: Medium
Problem:
Given a string
In a Unix-style file system, a period
The canonical path should have the following format:
• The path starts with a single slash
• Any two directories are separated by a single slash
• The path does not end with a trailing
• The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period
Return the simplified canonical path.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
71. Simplify Path
Topic: String, Stack
Difficulty: Medium
Problem:
Given a string
path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path.In a Unix-style file system, a period
'.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names.The canonical path should have the following format:
• The path starts with a single slash
'/'.• Any two directories are separated by a single slash
'/'.• The path does not end with a trailing
'/'.• The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period
'.' or double period '..')Return the simplified canonical path.
Example 1:
Input: path = "/home/"
Output: "/home"
Explanation: Note that there is no trailing slash after the last directory name.
Example 2:
Input: path = "/../"
Output: "/"
Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go.
Example 3:
Input: path = "/home//foo/"
Output: "/home/foo"
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.
Constraints:
•
1 <= path.length <= 3000•
path consists of English letters, digits, period '.', slash '/' or '_'.•
path is a valid absolute Unix path.2022-03-15
1249. Minimum Remove to Make Valid Parentheses
Topic: String, Stack
Difficulty: Medium
Problem:
Given a string s of
Your task is to remove the minimum number of parentheses (
Formally, a parentheses string is valid if and only if:
• It is the empty string, contains only lowercase characters, or
• It can be written as
• It can be written as
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1249. Minimum Remove to Make Valid Parentheses
Topic: String, Stack
Difficulty: Medium
Problem:
Given a string s of
'(' , ')' and lowercase English characters.Your task is to remove the minimum number of parentheses (
'(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string.Formally, a parentheses string is valid if and only if:
• It is the empty string, contains only lowercase characters, or
• It can be written as
AB (A concatenated with B), where A and B are valid strings, or• It can be written as
(A), where A is a valid string.Example 1:
Input: s = "lee(t(c)o)de)"
Output: "lee(t(c)o)de"
Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
Example 2:
Input: s = "a)b(c)d"
Output: "ab(c)d"
Example 3:
Input: s = "))(("
Output: ""
Explanation: An empty string is also valid.
Constraints:
•
1 <= s.length <= 10^5•
s[i] is either'(' , ')', or lowercase English letter.2022-03-16
946. Validate Stack Sequences
Topic: Array, Stack, Simulation
Difficulty: Medium
Problem:
Given two integer arrays
Example 1:
Example 2:
Constraints:
•
•
• All the elements of
•
•
946. Validate Stack Sequences
Topic: Array, Stack, Simulation
Difficulty: Medium
Problem:
Given two integer arrays
pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise.Example 1:
Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
Output: true
Explanation: We might do the following sequence:
push(1), push(2), push(3), push(4),
pop() -> 4,
push(5),
pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Example 2:
Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
Output: false
Explanation: 1 cannot be popped before 2.
Constraints:
•
1 <= pushed.length <= 1000•
0 <= pushed[i] <= 1000• All the elements of
pushed are unique.•
popped.length == pushed.length•
popped is a permutation of pushed.2022-03-17
856. Score of Parentheses
Topic: String, Stack
Difficulty: Medium
Problem:
Given a balanced parentheses string
The score of a balanced parentheses string is based on the following rule:
•
•
•
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
856. Score of Parentheses
Topic: String, Stack
Difficulty: Medium
Problem:
Given a balanced parentheses string
s, return the score of the string.The score of a balanced parentheses string is based on the following rule:
•
"()" has score 1.•
AB has score A + B, where A and B are balanced parentheses strings.•
(A) has score 2 * A, where A is a balanced parentheses string.Example 1:
Input: s = "()"
Output: 1
Example 2:
Input: s = "(())"
Output: 2
Example 3:
Input: s = "()()"
Output: 2
Constraints:
•
2 <= s.length <= 50•
s consists of only '(' and ')'.•
s is a balanced parentheses string.2022-03-18
316. Remove Duplicate Letters
Topic: String, Stack, Greedy, Monotonic Stack
Difficulty: Medium
Problem:
Given a string
Example 1:
Example 2:
Constraints:
•
•
Note: This question is the same as 1081: <https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/>
316. Remove Duplicate Letters
Topic: String, Stack, Greedy, Monotonic Stack
Difficulty: Medium
Problem:
Given a string
s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.Example 1:
Input: s = "bcabc"
Output: "abc"
Example 2:
Input: s = "cbacdcbc"
Output: "acdb"
Constraints:
•
1 <= s.length <= 10^4•
s consists of lowercase English letters.Note: This question is the same as 1081: <https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/>
2022-03-19
895. Maximum Frequency Stack
Topic: Hash Table, Stack, Design, Ordered Set
Difficulty: Hard
Problem:
Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.
Implement the
•
•
•
• If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned.
Example 1:
Constraints:
•
• At most
• It is guaranteed that there will be at least one element in the stack before calling
895. Maximum Frequency Stack
Topic: Hash Table, Stack, Design, Ordered Set
Difficulty: Hard
Problem:
Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.
Implement the
FreqStack class:•
FreqStack() constructs an empty frequency stack.•
void push(int val) pushes an integer val onto the top of the stack.•
int pop() removes and returns the most frequent element in the stack.• If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned.
Example 1:
Input
["FreqStack", "push", "push", "push", "push", "push", "push", "pop", "pop", "pop", "pop"]
[[], [5], [7], [5], [7], [4], [5], [], [], [], []]
Output
[null, null, null, null, null, null, null, 5, 7, 5, 4]
Explanation
FreqStack freqStack = new FreqStack();
freqStack.push(5); // The stack is [5]
freqStack.push(7); // The stack is [5,7]
freqStack.push(5); // The stack is [5,7,5]
freqStack.push(7); // The stack is [5,7,5,7]
freqStack.push(4); // The stack is [5,7,5,7,4]
freqStack.push(5); // The stack is [5,7,5,7,4,5]
freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,5,7,4].
freqStack.pop(); // return 7, as 5 and 7 is the most frequent, but 7 is closest to the top. The stack becomes [5,7,5,4].
freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,4].
freqStack.pop(); // return 4, as 4, 5 and 7 is the most frequent, but 4 is closest to the top. The stack becomes [5,7].
Constraints:
•
0 <= val <= 10^9• At most
2 * 10^4 calls will be made to push and pop.• It is guaranteed that there will be at least one element in the stack before calling
pop.2022-03-20
1007. Minimum Domino Rotations For Equal Row
Topic: Array, Greedy
Difficulty: Medium
Problem:
In a row of dominoes,
We may rotate the
Return the minimum number of rotations so that all the values in
If it cannot be done, return
Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/14/domino.png
Example 2:
Constraints:
•
•
•
1007. Minimum Domino Rotations For Equal Row
Topic: Array, Greedy
Difficulty: Medium
Problem:
In a row of dominoes,
tops[i] and bottoms[i] represent the top and bottom halves of the i^th domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)We may rotate the
i^th domino, so that tops[i] and bottoms[i] swap values.Return the minimum number of rotations so that all the values in
tops are the same, or all the values in bottoms are the same.If it cannot be done, return
-1.Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/14/domino.png
Input: tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2]
Output: 2
Explanation:
The first figure represents the dominoes as given by tops and bottoms: before we do any rotations.
If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.
Example 2:
Input: tops = [3,5,1,2,3], bottoms = [3,6,3,3,4]
Output: -1
Explanation:
In this case, it is not possible to rotate the dominoes to make one row of values equal.
Constraints:
•
2 <= tops.length <= 2 * 10^4•
bottoms.length == tops.length•
1 <= tops[i], bottoms[i] <= 62022-03-21
763. Partition Labels
Topic: Hash Table, Two Pointers, String, Greedy
Difficulty: Medium
Problem:
You are given a string
Note that the partition is done so that after concatenating all the parts in order, the resultant string should be
Return a list of integers representing the size of these parts.
Example 1:
Example 2:
Constraints:
•
•
763. Partition Labels
Topic: Hash Table, Two Pointers, String, Greedy
Difficulty: Medium
Problem:
You are given a string
s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.Note that the partition is done so that after concatenating all the parts in order, the resultant string should be
s.Return a list of integers representing the size of these parts.
Example 1:
Input: s = "ababcbacadefegdehijhklij"
Output: [9,7,8]
Explanation:
The partition is "ababcbaca", "defegde", "hijhklij".
This is a partition so that each letter appears in at most one part.
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts.
Example 2:
Input: s = "eccbbbbdec"
Output: [10]
Constraints:
•
1 <= s.length <= 500•
s consists of lowercase English letters.2022-03-22
1663. Smallest String With A Given Numeric Value
Topic: String, Greedy
Difficulty: Medium
Problem:
The numeric value of a lowercase character is defined as its position
The numeric value of a string consisting of lowercase characters is defined as the sum of its characters' numeric values. For example, the numeric value of the string
You are given two integers
Note that a string
Example 1:
Example 2:
Constraints:
•
•
1663. Smallest String With A Given Numeric Value
Topic: String, Greedy
Difficulty: Medium
Problem:
The numeric value of a lowercase character is defined as its position
(1-indexed) in the alphabet, so the numeric value of a is 1, the numeric value of b is 2, the numeric value of c is 3, and so on.The numeric value of a string consisting of lowercase characters is defined as the sum of its characters' numeric values. For example, the numeric value of the string
"abe" is equal to 1 + 2 + 5 = 8.You are given two integers
n and k. Return the lexicographically smallest string with length equal to n and numeric value equal to k.Note that a string
x is lexicographically smaller than string y if x comes before y in dictionary order, that is, either x is a prefix of y, or if i is the first position such that x[i] != y[i], then x[i] comes before y[i] in alphabetic order.Example 1:
Input: n = 3, k = 27
Output: "aay"
Explanation: The numeric value of the string is 1 + 1 + 25 = 27, and it is the smallest string with such a value and length equal to 3.
Example 2:
Input: n = 5, k = 73
Output: "aaszz"
Constraints:
•
1 <= n <= 10^5•
n <= k <= 26 * n2022-03-23
991. Broken Calculator
Topic: Math, Greedy
Difficulty: Medium
Problem:
There is a broken calculator that has the integer
• multiply the number on display by
• subtract
Given two integers
Example 1:
Example 2:
Example 3:
Constraints:
•
991. Broken Calculator
Topic: Math, Greedy
Difficulty: Medium
Problem:
There is a broken calculator that has the integer
startValue on its display initially. In one operation, you can:• multiply the number on display by
2, or• subtract
1 from the number on display.Given two integers
startValue and target, return the minimum number of operations needed to display target on the calculator.Example 1:
Input: startValue = 2, target = 3
Output: 2
Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.
Example 2:
Input: startValue = 5, target = 8
Output: 2
Explanation: Use decrement and then double {5 -> 4 -> 8}.
Example 3:
Input: startValue = 3, target = 10
Output: 3
Explanation: Use double, decrement and double {3 -> 6 -> 5 -> 10}.
Constraints:
•
1 <= x, y <= 10^92022-03-24
881. Boats to Save People
Topic: Array, Two Pointers, Greedy, Sorting
Difficulty: Medium
Problem:
You are given an array
Return the minimum number of boats to carry every given person.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
881. Boats to Save People
Topic: Array, Two Pointers, Greedy, Sorting
Difficulty: Medium
Problem:
You are given an array
people where people[i] is the weight of the i^th person, and an infinite number of boats where each boat can carry a maximum weight of limit. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit.Return the minimum number of boats to carry every given person.
Example 1:
Input: people = [1,2], limit = 3
Output: 1
Explanation: 1 boat (1, 2)
Example 2:
Input: people = [3,2,2,1], limit = 3
Output: 3
Explanation: 3 boats (1, 2), (2) and (3)
Example 3:
Input: people = [3,5,3,4], limit = 5
Output: 4
Explanation: 4 boats (3), (3), (4), (5)
Constraints:
•
1 <= people.length <= 5 * 10^4•
1 <= people[i] <= limit <= 3 * 10^42022-03-25
1029. Two City Scheduling
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
A company is planning to interview
Return the minimum cost to fly every person to a city such that exactly
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
1029. Two City Scheduling
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
A company is planning to interview
2n people. Given the array costs where costs[i] = [aCost_i, bCost_i], the cost of flying the i^th person to city a is aCost_i, and the cost of flying the i^th person to city b is bCost_i.Return the minimum cost to fly every person to a city such that exactly
n people arrive in each city.Example 1:
Input: costs = [[10,20],[30,200],[400,50],[30,20]]
Output: 110
Explanation:
The first person goes to city A for a cost of 10.
The second person goes to city A for a cost of 30.
The third person goes to city B for a cost of 50.
The fourth person goes to city B for a cost of 20.
The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.
Example 2:
Input: costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
Output: 1859
Example 3:
Input: costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]
Output: 3086
Constraints:
•
2 * n == costs.length•
2 <= costs.length <= 100•
costs.length is even.•
1 <= aCost_i, bCost_i <= 10002022-03-26
704. Binary Search
Topic: Array, Binary Search
Difficulty: Easy
Problem:
Given an array of integers
You must write an algorithm with
Example 1:
Example 2:
Constraints:
•
•
• All the integers in
•
704. Binary Search
Topic: Array, Binary Search
Difficulty: Easy
Problem:
Given an array of integers
nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.You must write an algorithm with
O(log n) runtime complexity.Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
Constraints:
•
1 <= nums.length <= 10^4•
-10^4 < nums[i], target < 10^4• All the integers in
nums are unique.•
nums is sorted in ascending order.2022-03-27
1337. The K Weakest Rows in a Matrix
Topic: Array, Binary Search, Sorting, Heap (Priority Queue), Matrix
Difficulty: Easy
Problem:
You are given an
A row
• The number of soldiers in row
• Both rows have the same number of soldiers and
Return the indices of the
Example 1:
Example 2:
Constraints:
•
•
•
•
•
1337. The K Weakest Rows in a Matrix
Topic: Array, Binary Search, Sorting, Heap (Priority Queue), Matrix
Difficulty: Easy
Problem:
You are given an
m x n binary matrix mat of 1's (representing soldiers) and 0's (representing civilians). The soldiers are positioned in front of the civilians. That is, all the 1's will appear to the left of all the 0's in each row.A row
i is weaker than a row j if one of the following is true:• The number of soldiers in row
i is less than the number of soldiers in row j.• Both rows have the same number of soldiers and
i < j.Return the indices of the
k weakest rows in the matrix ordered from weakest to strongest.Example 1:
Input: mat =
[[1,1,0,0,0],
[1,1,1,1,0],
[1,0,0,0,0],
[1,1,0,0,0],
[1,1,1,1,1]],
k = 3
Output: [2,0,3]
Explanation:
The number of soldiers in each row is:
- Row 0: 2
- Row 1: 4
- Row 2: 1
- Row 3: 2
- Row 4: 5
The rows ordered from weakest to strongest are [2,0,3,1,4].
Example 2:
Input: mat =
[[1,0,0,0],
[1,1,1,1],
[1,0,0,0],
[1,0,0,0]],
k = 2
Output: [0,2]
Explanation:
The number of soldiers in each row is:
- Row 0: 1
- Row 1: 4
- Row 2: 1
- Row 3: 1
The rows ordered from weakest to strongest are [0,2,3,1].
Constraints:
•
m == mat.length•
n == mat[i].length•
2 <= n, m <= 100•
1 <= k <= m•
matrix[i][j] is either 0 or 1.2022-03-28
81. Search in Rotated Sorted Array II
Topic: Array, Binary Search
Difficulty: Medium
Problem:
There is an integer array
Before being passed to your function,
Given the array
You must decrease the overall operation steps as much as possible.
Example 1:
Example 2:
Constraints:
•
•
•
•
Follow up: This problem is similar to Search in Rotated Sorted Array, but
81. Search in Rotated Sorted Array II
Topic: Array, Binary Search
Difficulty: Medium
Problem:
There is an integer array
nums sorted in non-decreasing order (not necessarily with distinct values).Before being passed to your function,
nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,4,4,5,6,6,7] might be rotated at pivot index 5 and become [4,5,6,6,7,0,1,2,4,4].Given the array
nums after the rotation and an integer target, return true if target is in nums, or false if it is not in nums.You must decrease the overall operation steps as much as possible.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Constraints:
•
1 <= nums.length <= 5000•
-10^4 <= nums[i] <= 10^4•
nums is guaranteed to be rotated at some pivot.•
-10^4 <= target <= 10^4Follow up: This problem is similar to Search in Rotated Sorted Array, but
nums may contain duplicates. Would this affect the runtime complexity? How and why?2022-03-29
287. Find the Duplicate Number
Topic: Array, Two Pointers, Binary Search, Bit Manipulation
Difficulty: Medium
Problem:
Given an array of integers
There is only one repeated number in
You must solve the problem without modifying the array
Example 1:
Example 2:
Constraints:
•
•
•
• All the integers in
Follow up:
• How can we prove that at least one duplicate number must exist in
• Can you solve the problem in linear runtime complexity?
287. Find the Duplicate Number
Topic: Array, Two Pointers, Binary Search, Bit Manipulation
Difficulty: Medium
Problem:
Given an array of integers
nums containing n + 1 integers where each integer is in the range [1, n] inclusive.There is only one repeated number in
nums, return this repeated number.You must solve the problem without modifying the array
nums and uses only constant extra space.Example 1:
Input: nums = [1,3,4,2,2]
Output: 2
Example 2:
Input: nums = [3,1,3,4,2]
Output: 3
Constraints:
•
1 <= n <= 10^5•
nums.length == n + 1•
1 <= nums[i] <= n• All the integers in
nums appear only once except for precisely one integer which appears two or more times.Follow up:
• How can we prove that at least one duplicate number must exist in
nums?• Can you solve the problem in linear runtime complexity?