2022-05-30
29. Divide Two Integers
Topic: Math, Bit Manipulation
Difficulty: Medium
Problem:
Given two integers
The integer division should truncate toward zero, which means losing its fractional part. For example,
Return the quotient after dividing
Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range:
Example 1:
Example 2:
Constraints:
•
•
29. Divide Two Integers
Topic: Math, Bit Manipulation
Difficulty: Medium
Problem:
Given two integers
dividend and divisor, divide two integers without using multiplication, division, and mod operator.The integer division should truncate toward zero, which means losing its fractional part. For example,
8.345 would be truncated to 8, and -2.7335 would be truncated to -2.Return the quotient after dividing
dividend by divisor.Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range:
[−2^31, 2^31 − 1]. For this problem, if the quotient is strictly greater than 2^31 - 1, then return 2^31 - 1, and if the quotient is strictly less than -2^31, then return -2^31.Example 1:
Input: dividend = 10, divisor = 3
Output: 3
Explanation: 10/3 = 3.33333.. which is truncated to 3.
Example 2:
Input: dividend = 7, divisor = -3
Output: -2
Explanation: 7/-3 = -2.33333.. which is truncated to -2.
Constraints:
•
-2^31 <= dividend, divisor <= 2^31 - 1•
divisor != 02022-05-31
1461. Check If a String Contains All Binary Codes of Size K
Topic: Hash Table, String, Bit Manipulation, Rolling Hash, Hash Function
Difficulty: Medium
Problem:
Given a binary string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1461. Check If a String Contains All Binary Codes of Size K
Topic: Hash Table, String, Bit Manipulation, Rolling Hash, Hash Function
Difficulty: Medium
Problem:
Given a binary string
s and an integer k, return true if every binary code of length k is a substring of s. Otherwise, return false.Example 1:
Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indices 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 3:
Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and does not exist in the array.
Constraints:
•
1 <= s.length <= 5 * 10^5•
s[i] is either '0' or '1'.•
1 <= k <= 202022-06-01
1480. Running Sum of 1d Array
Topic: Array, Prefix Sum
Difficulty: Easy
Problem:
Given an array
Return the running sum of
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1480. Running Sum of 1d Array
Topic: Array, Prefix Sum
Difficulty: Easy
Problem:
Given an array
nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).Return the running sum of
nums.Example 1:
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
Example 2:
Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
Example 3:
Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]
Constraints:
•
1 <= nums.length <= 1000•
-10^6 <= nums[i] <= 10^62022-06-02
867. Transpose Matrix
Topic: Array, Matrix, Simulation
Difficulty: Easy
Problem:
Given a 2D integer array
The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.
Image: https://assets.leetcode.com/uploads/2021/02/10/hint_transpose.png
Example 1:
Example 2:
Constraints:
•
•
•
•
•
867. Transpose Matrix
Topic: Array, Matrix, Simulation
Difficulty: Easy
Problem:
Given a 2D integer array
matrix, return the transpose of matrix.The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.
Image: https://assets.leetcode.com/uploads/2021/02/10/hint_transpose.png
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,4,7],[2,5,8],[3,6,9]]
Example 2:
Input: matrix = [[1,2,3],[4,5,6]]
Output: [[1,4],[2,5],[3,6]]
Constraints:
•
m == matrix.length•
n == matrix[i].length•
1 <= m, n <= 1000•
1 <= m * n <= 10^5•
-10^9 <= matrix[i][j] <= 10^92022-06-03
304. Range Sum Query 2D - Immutable
Topic: Array, Design, Matrix, Prefix Sum
Difficulty: Medium
Problem:
Given a 2D matrix
• Calculate the sum of the elements of
Implement the NumMatrix class:
•
•
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/14/sum-grid.jpg
Constraints:
•
•
•
•
•
•
• At most
304. Range Sum Query 2D - Immutable
Topic: Array, Design, Matrix, Prefix Sum
Difficulty: Medium
Problem:
Given a 2D matrix
matrix, handle multiple queries of the following type:• Calculate the sum of the elements of
matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).Implement the NumMatrix class:
•
NumMatrix(int[][] matrix) Initializes the object with the integer matrix matrix.•
int sumRegion(int row1, int col1, int row2, int col2) Returns the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/14/sum-grid.jpg
Input
["NumMatrix", "sumRegion", "sumRegion", "sumRegion"]
[[[[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]], [2, 1, 4, 3], [1, 1, 2, 2], [1, 2, 2, 4]]
Output
[null, 8, 11, 12]
Explanation
NumMatrix numMatrix = new NumMatrix([[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]);
numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle)
numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle)
numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle)
Constraints:
•
m == matrix.length•
n == matrix[i].length•
1 <= m, n <= 200•
-10^5 <= matrix[i][j] <= 10^5•
0 <= row1 <= row2 < m•
0 <= col1 <= col2 < n• At most
10^4 calls will be made to sumRegion.2022-06-04
51. N-Queens
Topic: Array, Backtracking
Difficulty: Hard
Problem:
The n-queens puzzle is the problem of placing
Given an integer
Each solution contains a distinct board configuration of the n-queens' placement, where
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/13/queens.jpg
Example 2:
Constraints:
•
51. N-Queens
Topic: Array, Backtracking
Difficulty: Hard
Problem:
The n-queens puzzle is the problem of placing
n queens on an n x n chessboard such that no two queens attack each other.Given an integer
n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.Each solution contains a distinct board configuration of the n-queens' placement, where
'Q' and '.' both indicate a queen and an empty space, respectively.Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/13/queens.jpg
Input: n = 4
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above
Example 2:
Input: n = 1
Output: [["Q"]]
Constraints:
•
1 <= n <= 92022-06-05
52. N-Queens II
Topic: Backtracking
Difficulty: Hard
Problem:
The n-queens puzzle is the problem of placing
Given an integer
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/13/queens.jpg
Example 2:
Constraints:
•
52. N-Queens II
Topic: Backtracking
Difficulty: Hard
Problem:
The n-queens puzzle is the problem of placing
n queens on an n x n chessboard such that no two queens attack each other.Given an integer
n, return the number of distinct solutions to the n-queens puzzle.Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/13/queens.jpg
Input: n = 4
Output: 2
Explanation: There are two distinct solutions to the 4-queens puzzle as shown.
Example 2:
Input: n = 1
Output: 1
Constraints:
•
1 <= n <= 92022-06-06
160. Intersection of Two Linked Lists
Topic: Hash Table, Linked List, Two Pointers
Difficulty: Easy
Problem:
Given the heads of two singly linked-lists
For example, the following two linked lists begin to intersect at node
Image: https://assets.leetcode.com/uploads/2021/03/05/160_statement.png
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
Note that the linked lists must retain their original structure after the function returns.
Custom Judge:
The inputs to the judge are given as follows (your program is not given these inputs):
•
•
•
•
•
The judge will then create the linked structure based on these inputs and pass the two heads,
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png
Constraints:
• The number of nodes of
• The number of nodes of
•
•
•
•
•
•
Follow up: Could you write a solution that runs in
160. Intersection of Two Linked Lists
Topic: Hash Table, Linked List, Two Pointers
Difficulty: Easy
Problem:
Given the heads of two singly linked-lists
headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.For example, the following two linked lists begin to intersect at node
c1:Image: https://assets.leetcode.com/uploads/2021/03/05/160_statement.png
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
Note that the linked lists must retain their original structure after the function returns.
Custom Judge:
The inputs to the judge are given as follows (your program is not given these inputs):
•
intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node.•
listA - The first linked list.•
listB - The second linked list.•
skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.•
skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.The judge will then create the linked structure based on these inputs and pass the two heads,
headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
Output: Intersected at '8'
Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png
Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Intersected at '2'
Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: No intersection
Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
Explanation: The two lists do not intersect, so return null.
Constraints:
• The number of nodes of
listA is in the m.• The number of nodes of
listB is in the n.•
1 <= m, n <= 3 * 10^4•
1 <= Node.val <= 10^5•
0 <= skipA < m•
0 <= skipB < n•
intersectVal is 0 if listA and listB do not intersect.•
intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.Follow up: Could you write a solution that runs in
O(m + n) time and use only O(1) memory?2022-06-07
88. Merge Sorted Array
Topic: Array, Two Pointers, Sorting
Difficulty: Easy
Problem:
You are given two integer arrays
Merge
The final sorted array should not be returned by the function, but instead be stored inside the array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
Follow up: Can you come up with an algorithm that runs in
88. Merge Sorted Array
Topic: Array, Two Pointers, Sorting
Difficulty: Easy
Problem:
You are given two integer arrays
nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.Merge
nums1 and nums2 into a single array sorted in non-decreasing order.The final sorted array should not be returned by the function, but instead be stored inside the array
nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.Example 1:
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
Example 2:
Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]
Explanation: The arrays we are merging are [1] and [].
The result of the merge is [1].
Example 3:
Input: nums1 = [0], m = 0, nums2 = [1], n = 1
Output: [1]
Explanation: The arrays we are merging are [] and [1].
The result of the merge is [1].
Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.
Constraints:
•
nums1.length == m + n•
nums2.length == n•
0 <= m, n <= 200•
1 <= m + n <= 200•
-10^9 <= nums1[i], nums2[j] <= 10^9Follow up: Can you come up with an algorithm that runs in
O(m + n) time?2022-06-08
1332. Remove Palindromic Subsequences
Topic: Two Pointers, String
Difficulty: Easy
Problem:
You are given a string
Return the minimum number of steps to make the given string empty.
A string is a subsequence of a given string if it is generated by deleting some characters of a given string without changing its order. Note that a subsequence does not necessarily need to be contiguous.
A string is called palindrome if is one that reads the same backward as well as forward.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1332. Remove Palindromic Subsequences
Topic: Two Pointers, String
Difficulty: Easy
Problem:
You are given a string
s consisting only of letters 'a' and 'b'. In a single step you can remove one palindromic subsequence from s.Return the minimum number of steps to make the given string empty.
A string is a subsequence of a given string if it is generated by deleting some characters of a given string without changing its order. Note that a subsequence does not necessarily need to be contiguous.
A string is called palindrome if is one that reads the same backward as well as forward.
Example 1:
Input: s = "ababa"
Output: 1
Explanation: s is already a palindrome, so its entirety can be removed in a single step.
Example 2:
Input: s = "abb"
Output: 2
Explanation: "abb" -> "bb" -> "".
Remove palindromic subsequence "a" then "bb".
Example 3:
Input: s = "baabb"
Output: 2
Explanation: "baabb" -> "b" -> "".
Remove palindromic subsequence "baab" then "b".
Constraints:
•
1 <= s.length <= 1000•
s[i] is either 'a' or 'b'.2022-06-09
167. Two Sum II - Input Array Is Sorted
Topic: Array, Two Pointers, Binary Search
Difficulty: Medium
Problem:
Given a 1-indexed array of integers
Return the indices of the two numbers,
The tests are generated such that there is exactly one solution. You may not use the same element twice.
Your solution must use only constant extra space.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
• The tests are generated such that there is exactly one solution.
167. Two Sum II - Input Array Is Sorted
Topic: Array, Two Pointers, Binary Search
Difficulty: Medium
Problem:
Given a 1-indexed array of integers
numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index_1] and numbers[index_2] where 1 <= index_1 < index_2 <= numbers.length.Return the indices of the two numbers,
index_1 and index_2, added by one as an integer array [index_1, index_2] of length 2.The tests are generated such that there is exactly one solution. You may not use the same element twice.
Your solution must use only constant extra space.
Example 1:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore, index_1 = 1, index_2 = 2. We return [1, 2].
Example 2:
Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is 6. Therefore index_1 = 1, index_2 = 3. We return [1, 3].
Example 3:
Input: numbers = [-1,0], target = -1
Output: [1,2]
Explanation: The sum of -1 and 0 is -1. Therefore index_1 = 1, index_2 = 2. We return [1, 2].
Constraints:
•
2 <= numbers.length <= 3 * 10^4•
-1000 <= numbers[i] <= 1000•
numbers is sorted in non-decreasing order.•
-1000 <= target <= 1000• The tests are generated such that there is exactly one solution.
2022-06-10
3. Longest Substring Without Repeating Characters
Topic: Hash Table, String, Sliding Window
Difficulty: Medium
Problem:
Given a string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
3. Longest Substring Without Repeating Characters
Topic: Hash Table, String, Sliding Window
Difficulty: Medium
Problem:
Given a string
s, find the length of the longest substring without repeating characters.Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Constraints:
•
0 <= s.length <= 5 * 10^4•
s consists of English letters, digits, symbols and spaces.2022-06-11
1658. Minimum Operations to Reduce X to Zero
Topic: Array, Hash Table, Binary Search, Sliding Window, Prefix Sum
Difficulty: Medium
Problem:
You are given an integer array
Return the minimum number of operations to reduce
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1658. Minimum Operations to Reduce X to Zero
Topic: Array, Hash Table, Binary Search, Sliding Window, Prefix Sum
Difficulty: Medium
Problem:
You are given an integer array
nums and an integer x. In one operation, you can either remove the leftmost or the rightmost element from the array nums and subtract its value from x. Note that this modifies the array for future operations.Return the minimum number of operations to reduce
x to exactly 0 if it is possible, otherwise, return -1.Example 1:
Input: nums = [1,1,4,2,3], x = 5
Output: 2
Explanation: The optimal solution is to remove the last two elements to reduce x to zero.
Example 2:
Input: nums = [5,6,7,8,9], x = 4
Output: -1
Example 3:
Input: nums = [3,2,20,1,1,3], x = 10
Output: 5
Explanation: The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^4•
1 <= x <= 10^92022-06-12
1695. Maximum Erasure Value
Topic: Array, Hash Table, Sliding Window
Difficulty: Medium
Problem:
You are given an array of positive integers
Return the maximum score you can get by erasing exactly one subarray.
An array
Example 1:
Example 2:
Constraints:
•
•
1695. Maximum Erasure Value
Topic: Array, Hash Table, Sliding Window
Difficulty: Medium
Problem:
You are given an array of positive integers
nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements.Return the maximum score you can get by erasing exactly one subarray.
An array
b is called to be a subarray of a if it forms a contiguous subsequence of a, that is, if it is equal to a[l],a[l+1],...,a[r] for some (l,r).Example 1:
Input: nums = [4,2,4,5,6]
Output: 17
Explanation: The optimal subarray here is [2,4,5,6].
Example 2:
Input: nums = [5,2,1,2,5,2,1,2,5]
Output: 8
Explanation: The optimal subarray here is [5,2,1] or [1,2,5].
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^42022-06-13
120. Triangle
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
Given a
For each step, you may move to an adjacent number of the row below. More formally, if you are on index
Example 1:
Example 2:
Constraints:
•
•
•
•
Follow up: Could you do this using only
120. Triangle
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
Given a
triangle array, return the minimum path sum from top to bottom.For each step, you may move to an adjacent number of the row below. More formally, if you are on index
i on the current row, you may move to either index i or index i + 1 on the next row.Example 1:
Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
Output: 11
Explanation: The triangle looks like:
2
3 4
6 5 7
4 1 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
Example 2:
Input: triangle = [[-10]]
Output: -10
Constraints:
•
1 <= triangle.length <= 200•
triangle[0].length == 1•
triangle[i].length == triangle[i - 1].length + 1•
-10^4 <= triangle[i][j] <= 10^4Follow up: Could you do this using only
O(n) extra space, where n is the total number of rows in the triangle?2022-06-14
583. Delete Operation for Two Strings
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given two strings
In one step, you can delete exactly one character in either string.
Example 1:
Example 2:
Constraints:
•
•
583. Delete Operation for Two Strings
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given two strings
word1 and word2, return the minimum number of steps required to make word1 and word2 the same.In one step, you can delete exactly one character in either string.
Example 1:
Input: word1 = "sea", word2 = "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
Example 2:
Input: word1 = "leetcode", word2 = "etco"
Output: 4
Constraints:
•
1 <= word1.length, word2.length <= 500•
word1 and word2 consist of only lowercase English letters.2022-06-15
1048. Longest String Chain
Topic: Array, Hash Table, Two Pointers, String, Dynamic Programming
Difficulty: Medium
Problem:
You are given an array of
• For example,
A word chain is a sequence of words
Return the length of the longest possible word chain with words chosen from the given list of
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1048. Longest String Chain
Topic: Array, Hash Table, Two Pointers, String, Dynamic Programming
Difficulty: Medium
Problem:
You are given an array of
words where each word consists of lowercase English letters.word_A is a predecessor of word_B if and only if we can insert exactly one letter anywhere in word_A without changing the order of the other characters to make it equal to word_B.• For example,
"abc" is a predecessor of "abac", while "cba" is not a predecessor of "bcad".A word chain is a sequence of words
[word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. A single word is trivially a word chain with k == 1.Return the length of the longest possible word chain with words chosen from the given list of
words.Example 1:
Input: words = ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: One of the longest word chains is ["a","ba","bda","bdca"].
Example 2:
Input: words = ["xbc","pcxbcf","xb","cxbc","pcxbc"]
Output: 5
Explanation: All the words can be put in a word chain ["xb", "xbc", "cxbc", "pcxbc", "pcxbcf"].
Example 3:
Input: words = ["abcd","dbqca"]
Output: 1
Explanation: The trivial word chain ["abcd"] is one of the longest word chains.
["abcd","dbqca"] is not a valid word chain because the ordering of the letters is changed.
Constraints:
•
1 <= words.length <= 1000•
1 <= words[i].length <= 16•
words[i] only consists of lowercase English letters.2022-06-16
5. Longest Palindromic Substring
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given a string
Example 1:
Example 2:
Constraints:
•
•
5. Longest Palindromic Substring
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given a string
s, return the longest palindromic substring in s.Example 1:
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.
Example 2:
Input: s = "cbbd"
Output: "bb"
Constraints:
•
1 <= s.length <= 1000•
s consist of only digits and English letters.2022-06-17
968. Binary Tree Cameras
Topic: Dynamic Programming, Tree, Depth-First Search, Binary Tree
Difficulty: Hard
Problem:
You are given the
Return the minimum number of cameras needed to monitor all nodes of the tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_01.png
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_02.png
Constraints:
• The number of nodes in the tree is in the range
•
968. Binary Tree Cameras
Topic: Dynamic Programming, Tree, Depth-First Search, Binary Tree
Difficulty: Hard
Problem:
You are given the
root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children.Return the minimum number of cameras needed to monitor all nodes of the tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_01.png
Input: root = [0,0,null,0,0]
Output: 1
Explanation: One camera is enough to monitor all nodes if placed as shown.
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_02.png
Input: root = [0,0,null,0,null,0,null,null,0]
Output: 2
Explanation: At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.
Constraints:
• The number of nodes in the tree is in the range
[1, 1000].•
Node.val == 02022-06-18
745. Prefix and Suffix Search
Topic: String, Design, Trie
Difficulty: Hard
Problem:
Design a special dictionary with some words that searchs the words in it by a prefix and a suffix.
Implement the
•
•
Example 1:
Constraints:
•
•
•
•
• At most
745. Prefix and Suffix Search
Topic: String, Design, Trie
Difficulty: Hard
Problem:
Design a special dictionary with some words that searchs the words in it by a prefix and a suffix.
Implement the
WordFilter class:•
WordFilter(string[] words) Initializes the object with the words in the dictionary.•
f(string prefix, string suffix) Returns the index of the word in the dictionary, which has the prefix prefix and the suffix suffix. If there is more than one valid index, return the largest of them. If there is no such word in the dictionary, return -1.Example 1:
Input
["WordFilter", "f"]
[[["apple"]], ["a", "e"]]
Output
[null, 0]
Explanation
WordFilter wordFilter = new WordFilter(["apple"]);
wordFilter.f("a", "e"); // return 0, because the word at index 0 has prefix = "a" and suffix = 'e".
Constraints:
•
1 <= words.length <= 15000•
1 <= words[i].length <= 10•
1 <= prefix.length, suffix.length <= 10•
words[i], prefix and suffix consist of lower-case English letters only.• At most
15000 calls will be made to the function f.