2022-06-30
462. Minimum Moves to Equal Array Elements II
Topic: Array, Math, Sorting
Difficulty: Medium
Problem:
Given an integer array
In one move, you can increment or decrement an element of the array by
Test cases are designed so that the answer will fit in a 32-bit integer.
Example 1:
Example 2:
Constraints:
•
•
•
462. Minimum Moves to Equal Array Elements II
Topic: Array, Math, Sorting
Difficulty: Medium
Problem:
Given an integer array
nums of size n, return the minimum number of moves required to make all array elements equal.In one move, you can increment or decrement an element of the array by
1.Test cases are designed so that the answer will fit in a 32-bit integer.
Example 1:
Input: nums = [1,2,3]
Output: 2
Explanation:
Only two moves are needed (remember each move increments or decrements one element):
[1,2,3] => [2,2,3] => [2,2,2]
Example 2:
Input: nums = [1,10,2,9]
Output: 16
Constraints:
•
n == nums.length•
1 <= nums.length <= 10^5•
-10^9 <= nums[i] <= 10^92022-07-01
1710. Maximum Units on a Truck
Topic: Array, Greedy, Sorting
Difficulty: Easy
Problem:
You are assigned to put some amount of boxes onto one truck. You are given a 2D array
•
•
You are also given an integer
Return the maximum total number of units that can be put on the truck.
Example 1:
Example 2:
Constraints:
•
•
•
1710. Maximum Units on a Truck
Topic: Array, Greedy, Sorting
Difficulty: Easy
Problem:
You are assigned to put some amount of boxes onto one truck. You are given a 2D array
boxTypes, where boxTypes[i] = [numberOfBoxes_i, numberOfUnitsPerBox_i]:•
numberOfBoxes_i is the number of boxes of type i.•
numberOfUnitsPerBox_i is the number of units in each box of the type i.You are also given an integer
truckSize, which is the maximum number of boxes that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed truckSize.Return the maximum total number of units that can be put on the truck.
Example 1:
Input: boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
Output: 8
Explanation: There are:
- 1 box of the first type that contains 3 units.
- 2 boxes of the second type that contain 2 units each.
- 3 boxes of the third type that contain 1 unit each.
You can take all the boxes of the first and second types, and one box of the third type.
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
Example 2:
Input: boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
Output: 91
Constraints:
•
1 <= boxTypes.length <= 1000•
1 <= numberOfBoxes_i, numberOfUnitsPerBox_i <= 1000•
1 <= truckSize <= 10^62022-07-02
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
You are given a rectangular cake of size
•
•
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays
Example 1:
Image: https://assets.leetcode.com/uploads/2020/05/14/leetcode_max_area_2.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/05/14/leetcode_max_area_3.png
Example 3:
Constraints:
•
•
•
•
•
• All the elements in
• All the elements in
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
You are given a rectangular cake of size
h x w and two arrays of integers horizontalCuts and verticalCuts where:•
horizontalCuts[i] is the distance from the top of the rectangular cake to the i^th horizontal cut and similarly, and•
verticalCuts[j] is the distance from the left of the rectangular cake to the j^th vertical cut.Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays
horizontalCuts and verticalCuts. Since the answer can be a large number, return this modulo 10^9 + 7.Example 1:
Image: https://assets.leetcode.com/uploads/2020/05/14/leetcode_max_area_2.png
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/05/14/leetcode_max_area_3.png
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
•
2 <= h, w <= 10^9•
1 <= horizontalCuts.length <= min(h - 1, 10^5)•
1 <= verticalCuts.length <= min(w - 1, 10^5)•
1 <= horizontalCuts[i] < h•
1 <= verticalCuts[i] < w• All the elements in
horizontalCuts are distinct.• All the elements in
verticalCuts are distinct.2022-07-03
376. Wiggle Subsequence
Topic: Array, Dynamic Programming, Greedy
Difficulty: Medium
Problem:
A wiggle sequence is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequences.
• For example,
• In contrast,
A subsequence is obtained by deleting some elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.
Given an integer array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
Follow up: Could you solve this in
376. Wiggle Subsequence
Topic: Array, Dynamic Programming, Greedy
Difficulty: Medium
Problem:
A wiggle sequence is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequences.
• For example,
[1, 7, 4, 9, 2, 5] is a wiggle sequence because the differences (6, -3, 5, -7, 3) alternate between positive and negative.• In contrast,
[1, 4, 7, 2, 5] and [1, 7, 4, 5, 5] are not wiggle sequences. The first is not because its first two differences are positive, and the second is not because its last difference is zero.A subsequence is obtained by deleting some elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.
Given an integer array
nums, return the length of the longest wiggle subsequence of nums.Example 1:
Input: nums = [1,7,4,9,2,5]
Output: 6
Explanation: The entire sequence is a wiggle sequence with differences (6, -3, 5, -7, 3).
Example 2:
Input: nums = [1,17,5,10,13,15,10,5,16,8]
Output: 7
Explanation: There are several subsequences that achieve this length.
One is [1, 17, 10, 13, 10, 16, 8] with differences (16, -7, 3, -3, 6, -8).
Example 3:
Input: nums = [1,2,3,4,5,6,7,8,9]
Output: 2
Constraints:
•
1 <= nums.length <= 1000•
0 <= nums[i] <= 1000Follow up: Could you solve this in
O(n) time?2022-07-04
135. Candy
Topic: Array, Greedy
Difficulty: Hard
Problem:
There are
You are giving candies to these children subjected to the following requirements:
• Each child must have at least one candy.
• Children with a higher rating get more candies than their neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Example 2:
Constraints:
•
•
•
135. Candy
Topic: Array, Greedy
Difficulty: Hard
Problem:
There are
n children standing in a line. Each child is assigned a rating value given in the integer array ratings.You are giving candies to these children subjected to the following requirements:
• Each child must have at least one candy.
• Children with a higher rating get more candies than their neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input: ratings = [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input: ratings = [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it satisfies the above two conditions.
Constraints:
•
n == ratings.length•
1 <= n <= 2 * 10^4•
0 <= ratings[i] <= 2 * 10^42022-07-05
128. Longest Consecutive Sequence
Topic: Array, Hash Table, Union Find
Difficulty: Medium
Problem:
Given an unsorted array of integers
You must write an algorithm that runs in
Example 1:
Example 2:
Constraints:
•
•
128. Longest Consecutive Sequence
Topic: Array, Hash Table, Union Find
Difficulty: Medium
Problem:
Given an unsorted array of integers
nums, return the length of the longest consecutive elements sequence.You must write an algorithm that runs in
O(n) time.Example 1:
Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
Example 2:
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
Constraints:
•
0 <= nums.length <= 10^5•
-10^9 <= nums[i] <= 10^92022-07-06
509. Fibonacci Number
Topic: Math, Dynamic Programming, Recursion, Memoization
Difficulty: Easy
Problem:
The Fibonacci numbers, commonly denoted
Given
Example 1:
Example 2:
Example 3:
Constraints:
•
509. Fibonacci Number
Topic: Math, Dynamic Programming, Recursion, Memoization
Difficulty: Easy
Problem:
The Fibonacci numbers, commonly denoted
F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
Given
n, calculate F(n).Example 1:
Input: n = 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
Example 2:
Input: n = 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
Example 3:
Input: n = 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
Constraints:
•
0 <= n <= 302022-07-07
97. Interleaving String
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given strings
An interleaving of two strings
•
•
•
• The interleaving is
Note:
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/02/interleave.jpg
Example 2:
Example 3:
Constraints:
•
•
•
Follow up: Could you solve it using only
97. Interleaving String
Topic: String, Dynamic Programming
Difficulty: Medium
Problem:
Given strings
s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2.An interleaving of two strings
s and t is a configuration where they are divided into non-empty substrings such that:•
s = s_1 + s_2 + ... + s_n•
t = t_1 + t_2 + ... + t_m•
|n - m| <= 1• The interleaving is
s_1 + t_1 + s_2 + t_2 + s_3 + t_3 + ... or t_1 + s_1 + t_2 + s_2 + t_3 + s_3 + ...Note:
a + b is the concatenation of strings a and b.Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/02/interleave.jpg
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
Example 2:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false
Example 3:
Input: s1 = "", s2 = "", s3 = ""
Output: true
Constraints:
•
0 <= s1.length, s2.length <= 100•
0 <= s3.length <= 200•
s1, s2, and s3 consist of lowercase English letters.Follow up: Could you solve it using only
O(s2.length) additional memory space?2022-07-08
1473. Paint House III
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
There is a row of
A neighborhood is a maximal group of continuous houses that are painted with the same color.
• For example:
Given an array
•
•
Return the minimum cost of painting all the remaining houses in such a way that there are exactly
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
•
•
1473. Paint House III
Topic: Array, Dynamic Programming
Difficulty: Hard
Problem:
There is a row of
m houses in a small city, each house must be painted with one of the n colors (labeled from 1 to n), some houses that have been painted last summer should not be painted again.A neighborhood is a maximal group of continuous houses that are painted with the same color.
• For example:
houses = [1,2,2,3,3,2,1,1] contains 5 neighborhoods [{1}, {2,2}, {3,3}, {2}, {1,1}].Given an array
houses, an m x n matrix cost and an integer target where:•
houses[i]: is the color of the house i, and 0 if the house is not painted yet.•
cost[i][j]: is the cost of paint the house i with the color j + 1.Return the minimum cost of painting all the remaining houses in such a way that there are exactly
target neighborhoods. If it is not possible, return -1.Example 1:
Input: houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3
Output: 9
Explanation: Paint houses of this way [1,2,2,1,1]
This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}].
Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9.
Example 2:
Input: houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3
Output: 11
Explanation: Some houses are already painted, Paint the houses of this way [2,2,1,2,2]
This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}].
Cost of paint the first and last house (10 + 1) = 11.
Example 3:
Input: houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3
Output: -1
Explanation: Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3.
Constraints:
•
m == houses.length == cost.length•
n == cost[i].length•
1 <= m <= 100•
1 <= n <= 20•
1 <= target <= m•
0 <= houses[i] <= n•
1 <= cost[i][j] <= 10^42022-07-09
1696. Jump Game VI
Topic: Array, Dynamic Programming, Queue, Sliding Window, Heap (Priority Queue), Monotonic Queue
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
You are initially standing at index
You want to reach the last index of the array (index
Return the maximum score you can get.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1696. Jump Game VI
Topic: Array, Dynamic Programming, Queue, Sliding Window, Heap (Priority Queue), Monotonic Queue
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
nums and an integer k.You are initially standing at index
0. In one move, you can jump at most k steps forward without going outside the boundaries of the array. That is, you can jump from index i to any index in the range [i + 1, min(n - 1, i + k)] inclusive.You want to reach the last index of the array (index
n - 1). Your score is the sum of all nums[j] for each index j you visited in the array.Return the maximum score you can get.
Example 1:
Input: nums = [1,-1,-2,4,-7,3], k = 2
Output: 7
Explanation: You can choose your jumps forming the subsequence [1,-1,4,3] (underlined above). The sum is 7.
Example 2:
Input: nums = [10,-5,-2,4,0,3], k = 3
Output: 17
Explanation: You can choose your jumps forming the subsequence [10,4,3] (underlined above). The sum is 17.
Example 3:
Input: nums = [1,-5,-20,4,-1,3,-6,-3], k = 2
Output: 0
Constraints:
•
1 <= nums.length, k <= 10^5•
-10^4 <= nums[i] <= 10^42022-07-10
746. Min Cost Climbing Stairs
Topic: Array, Dynamic Programming
Difficulty: Easy
Problem:
You are given an integer array
You can either start from the step with index
Return the minimum cost to reach the top of the floor.
Example 1:
Example 2:
Constraints:
•
•
746. Min Cost Climbing Stairs
Topic: Array, Dynamic Programming
Difficulty: Easy
Problem:
You are given an integer array
cost where cost[i] is the cost of i^th step on a staircase. Once you pay the cost, you can either climb one or two steps.You can either start from the step with index
0, or the step with index 1.Return the minimum cost to reach the top of the floor.
Example 1:
Input: cost = [10,15,20]
Output: 15
Explanation: You will start at index 1.
- Pay 15 and climb two steps to reach the top.
The total cost is 15.
Example 2:
Input: cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6
Explanation: You will start at index 0.
- Pay 1 and climb two steps to reach index 2.
- Pay 1 and climb two steps to reach index 4.
- Pay 1 and climb two steps to reach index 6.
- Pay 1 and climb one step to reach index 7.
- Pay 1 and climb two steps to reach index 9.
- Pay 1 and climb one step to reach the top.
The total cost is 6.
Constraints:
•
2 <= cost.length <= 1000•
0 <= cost[i] <= 9992022-07-11
199. Binary Tree Right Side View
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2021/02/14/tree.jpg
Example 2:
Example 3:
Constraints:
• The number of nodes in the tree is in the range
•
199. Binary Tree Right Side View
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.Example 1:
Image: https://assets.leetcode.com/uploads/2021/02/14/tree.jpg
Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]
Example 2:
Input: root = [1,null,3]
Output: [1,3]
Example 3:
Input: root = []
Output: []
Constraints:
• The number of nodes in the tree is in the range
[0, 100].•
-100 <= Node.val <= 1002022-07-12
473. Matchsticks to Square
Topic: Array, Dynamic Programming, Backtracking, Bit Manipulation, Bitmask
Difficulty: Medium
Problem:
You are given an integer array
Return
Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg
Example 2:
Constraints:
•
•
473. Matchsticks to Square
Topic: Array, Dynamic Programming, Backtracking, Bit Manipulation, Bitmask
Difficulty: Medium
Problem:
You are given an integer array
matchsticks where matchsticks[i] is the length of the i^th matchstick. You want to use all the matchsticks to make one square. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.Return
true if you can make this square and false otherwise.Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg
Input: matchsticks = [1,1,2,2,2]
Output: true
Explanation: You can form a square with length 2, one side of the square came two sticks with length 1.
Example 2:
Input: matchsticks = [3,3,3,3,4]
Output: false
Explanation: You cannot find a way to form a square with all the matchsticks.
Constraints:
•
1 <= matchsticks.length <= 15•
1 <= matchsticks[i] <= 10^82022-07-13
102. Binary Tree Level Order Traversal
Topic: Tree, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg
Example 2:
Example 3:
Constraints:
• The number of nodes in the tree is in the range
•
102. Binary Tree Level Order Traversal
Topic: Tree, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).Example 1:
Image: https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]
Example 2:
Input: root = [1]
Output: [[1]]
Example 3:
Input: root = []
Output: []
Constraints:
• The number of nodes in the tree is in the range
[0, 2000].•
-1000 <= Node.val <= 10002022-07-14
105. Construct Binary Tree from Preorder and Inorder Traversal
Topic: Array, Hash Table, Divide and Conquer, Tree, Binary Tree
Difficulty: Medium
Problem:
Given two integer arrays
Example 1:
Image: https://assets.leetcode.com/uploads/2021/02/19/tree.jpg
Example 2:
Constraints:
•
•
•
•
• Each value of
•
•
105. Construct Binary Tree from Preorder and Inorder Traversal
Topic: Array, Hash Table, Divide and Conquer, Tree, Binary Tree
Difficulty: Medium
Problem:
Given two integer arrays
preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.Example 1:
Image: https://assets.leetcode.com/uploads/2021/02/19/tree.jpg
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
Example 2:
Input: preorder = [-1], inorder = [-1]
Output: [-1]
Constraints:
•
1 <= preorder.length <= 3000•
inorder.length == preorder.length•
-3000 <= preorder[i], inorder[i] <= 3000•
preorder and inorder consist of unique values.• Each value of
inorder also appears in preorder.•
preorder is guaranteed to be the preorder traversal of the tree.•
inorder is guaranteed to be the inorder traversal of the tree.2022-07-15
695. Max Area of Island
Topic: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix
Difficulty: Medium
Problem:
You are given an
The area of an island is the number of cells with a value
Return the maximum area of an island in
Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/01/maxarea1-grid.jpg
Example 2:
Constraints:
•
•
•
•
695. Max Area of Island
Topic: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix
Difficulty: Medium
Problem:
You are given an
m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.The area of an island is the number of cells with a value
1 in the island.Return the maximum area of an island in
grid. If there is no island, return 0.Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/01/maxarea1-grid.jpg
Input: grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Output: 6
Explanation: The answer is not 11, because the island must be connected 4-directionally.
Example 2:
Input: grid = [[0,0,0,0,0,0,0,0]]
Output: 0
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m, n <= 50•
grid[i][j] is either 0 or 1.2022-07-16
576. Out of Boundary Paths
Topic: Dynamic Programming
Difficulty: Medium
Problem:
There is an
Given the five integers
Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_2.png
Constraints:
•
•
•
•
576. Out of Boundary Paths
Topic: Dynamic Programming
Difficulty: Medium
Problem:
There is an
m x n grid with a ball. The ball is initially at the position [startRow, startColumn]. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply at most maxMove moves to the ball.Given the five integers
m, n, maxMove, startRow, startColumn, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it modulo 10^9 + 7.Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_1.png
Input: m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
Output: 6
Example 2:
Image: https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_2.png
Input: m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
Output: 12
Constraints:
•
1 <= m, n <= 50•
0 <= maxMove <= 50•
0 <= startRow < m•
0 <= startColumn < n2022-07-17
629. K Inverse Pairs Array
Topic: Dynamic Programming
Difficulty: Hard
Problem:
For an integer array
Given two integers n and k, return the number of different arrays consist of numbers from
Example 1:
Example 2:
Constraints:
•
•
629. K Inverse Pairs Array
Topic: Dynamic Programming
Difficulty: Hard
Problem:
For an integer array
nums, an inverse pair is a pair of integers [i, j] where 0 <= i < j < nums.length and nums[i] > nums[j].Given two integers n and k, return the number of different arrays consist of numbers from
1 to n such that there are exactly k inverse pairs. Since the answer can be huge, return it modulo 10^9 + 7.Example 1:
Input: n = 3, k = 0
Output: 1
Explanation: Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pairs.
Example 2:
Input: n = 3, k = 1
Output: 2
Explanation: The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.
Constraints:
•
1 <= n <= 1000•
0 <= k <= 10002022-07-18
1074. Number of Submatrices That Sum to Target
Topic: Array, Hash Table, Matrix, Prefix Sum
Difficulty: Hard
Problem:
Given a
A submatrix
Two submatrices
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/02/mate1.jpg
Example 2:
Example 3:
Constraints:
•
•
•
•
1074. Number of Submatrices That Sum to Target
Topic: Array, Hash Table, Matrix, Prefix Sum
Difficulty: Hard
Problem:
Given a
matrix and a target, return the number of non-empty submatrices that sum to target.A submatrix
x1, y1, x2, y2 is the set of all cells matrix[x][y] with x1 <= x <= x2 and y1 <= y <= y2.Two submatrices
(x1, y1, x2, y2) and (x1', y1', x2', y2') are different if they have some coordinate that is different: for example, if x1 != x1'.Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/02/mate1.jpg
Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
Output: 4
Explanation: The four 1x1 submatrices that only contain 0.
Example 2:
Input: matrix = [[1,-1],[-1,1]], target = 0
Output: 5
Explanation: The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.
Example 3:
Input: matrix = [[904]], target = 0
Output: 0
Constraints:
•
1 <= matrix.length <= 100•
1 <= matrix[0].length <= 100•
-1000 <= matrix[i] <= 1000•
-10^8 <= target <= 10^82022-07-19
118. Pascal's Triangle
Topic: Array, Dynamic Programming
Difficulty: Easy
Problem:
Given an integer
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
Image: https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif
Example 1:
Example 2:
Constraints:
•
118. Pascal's Triangle
Topic: Array, Dynamic Programming
Difficulty: Easy
Problem:
Given an integer
numRows, return the first numRows of Pascal's triangle.In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
Image: https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif
Example 1:
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Example 2:
Input: numRows = 1
Output: [[1]]
Constraints:
•
1 <= numRows <= 302022-07-20
792. Number of Matching Subsequences
Topic: Hash Table, String, Trie, Sorting
Difficulty: Medium
Problem:
Given a string
A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
• For example,
Example 1:
Example 2:
Constraints:
•
•
•
•
792. Number of Matching Subsequences
Topic: Hash Table, String, Trie, Sorting
Difficulty: Medium
Problem:
Given a string
s and an array of strings words, return the number of words[i] that is a subsequence of s.A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
• For example,
"ace" is a subsequence of "abcde".Example 1:
Input: s = "abcde", words = ["a","bb","acd","ace"]
Output: 3
Explanation: There are three strings in words that are a subsequence of s: "a", "acd", "ace".
Example 2:
Input: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
Output: 2
Constraints:
•
1 <= s.length <= 5 * 10^4•
1 <= words.length <= 5000•
1 <= words[i].length <= 50•
s and words[i] consist of only lowercase English letters.