2022-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.2022-07-21
92. Reverse Linked List II
Topic: Linked List
Difficulty: Medium
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2021/02/19/rev2ex2.jpg
Example 2:
Constraints:
• The number of nodes in the list is
•
•
•
Follow up: Could you do it in one pass?
92. Reverse Linked List II
Topic: Linked List
Difficulty: Medium
Problem:
Given the
head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.Example 1:
Image: https://assets.leetcode.com/uploads/2021/02/19/rev2ex2.jpg
Input: head = [1,2,3,4,5], left = 2, right = 4
Output: [1,4,3,2,5]
Example 2:
Input: head = [5], left = 1, right = 1
Output: [5]
Constraints:
• The number of nodes in the list is
n.•
1 <= n <= 500•
-500 <= Node.val <= 500•
1 <= left <= right <= nFollow up: Could you do it in one pass?
2022-07-22
86. Partition List
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
Given the
You should preserve the original relative order of the nodes in each of the two partitions.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/04/partition.jpg
Example 2:
Constraints:
• The number of nodes in the list is in the range
•
•
86. Partition List
Topic: Linked List, Two Pointers
Difficulty: Medium
Problem:
Given the
head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/04/partition.jpg
Input: head = [1,4,3,2,5,2], x = 3
Output: [1,2,2,4,3,5]
Example 2:
Input: head = [2,1], x = 2
Output: [1,2]
Constraints:
• The number of nodes in the list is in the range
[0, 200].•
-100 <= Node.val <= 100•
-200 <= x <= 2002022-07-23
315. Count of Smaller Numbers After Self
Topic: Array, Binary Search, Divide and Conquer, Binary Indexed Tree, Segment Tree, Merge Sort, Ordered Set
Difficulty: Hard
Problem:
You are given an integer array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
315. Count of Smaller Numbers After Self
Topic: Array, Binary Search, Divide and Conquer, Binary Indexed Tree, Segment Tree, Merge Sort, Ordered Set
Difficulty: Hard
Problem:
You are given an integer array
nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].Example 1:
Input: nums = [5,2,6,1]
Output: [2,1,1,0]
Explanation:
To the right of 5 there are 2 smaller elements (2 and 1).
To the right of 2 there is only 1 smaller element (1).
To the right of 6 there is 1 smaller element (1).
To the right of 1 there is 0 smaller element.
Example 2:
Input: nums = [-1]
Output: [0]
Example 3:
Input: nums = [-1,-1]
Output: [0,0]
Constraints:
•
1 <= nums.length <= 10^5•
-10^4 <= nums[i] <= 10^42022-07-24
240. Search a 2D Matrix II
Topic: Array, Binary Search, Divide and Conquer, Matrix
Difficulty: Medium
Problem:
Write an efficient algorithm that searches for a value
• Integers in each row are sorted in ascending from left to right.
• Integers in each column are sorted in ascending from top to bottom.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg
Constraints:
•
•
•
•
• All the integers in each row are sorted in ascending order.
• All the integers in each column are sorted in ascending order.
•
240. Search a 2D Matrix II
Topic: Array, Binary Search, Divide and Conquer, Matrix
Difficulty: Medium
Problem:
Write an efficient algorithm that searches for a value
target in an m x n integer matrix matrix. This matrix has the following properties:• Integers in each row are sorted in ascending from left to right.
• Integers in each column are sorted in ascending from top to bottom.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg
Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
Output: true
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg
Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
Output: false
Constraints:
•
m == matrix.length•
n == matrix[i].length•
1 <= n, m <= 300•
-10^9 <= matrix[i][j] <= 10^9• All the integers in each row are sorted in ascending order.
• All the integers in each column are sorted in ascending order.
•
-10^9 <= target <= 10^92022-07-25
34. Find First and Last Position of Element in Sorted Array
Topic: Array, Binary Search
Difficulty: Medium
Problem:
Given an array of integers
If
You must write an algorithm with
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
34. Find First and Last Position of Element in Sorted Array
Topic: Array, Binary Search
Difficulty: Medium
Problem:
Given an array of integers
nums sorted in non-decreasing order, find the starting and ending position of a given target value.If
target is not found in the array, return [-1, -1].You must write an algorithm with
O(log n) runtime complexity.Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
Example 3:
Input: nums = [], target = 0
Output: [-1,-1]
Constraints:
•
0 <= nums.length <= 10^5•
-10^9 <= nums[i] <= 10^9•
nums is a non-decreasing array.•
-10^9 <= target <= 10^92022-07-26
236. Lowest Common Ancestor of a Binary Tree
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes
Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/14/binarytree.png
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/14/binarytree.png
Example 3:
Constraints:
• The number of nodes in the tree is in the range
•
• All
•
•
236. Lowest Common Ancestor of a Binary Tree
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes
p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/14/binarytree.png
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/14/binarytree.png
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
Example 3:
Input: root = [1,2], p = 1, q = 2
Output: 1
Constraints:
• The number of nodes in the tree is in the range
[2, 10^5].•
-10^9 <= Node.val <= 10^9• All
Node.val are unique.•
p != q•
p and q will exist in the tree.2022-07-27
114. Flatten Binary Tree to Linked List
Topic: Linked List, Stack, Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
• The "linked list" should use the same
• The "linked list" should be in the same order as a pre-order traversal of the binary tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg
Example 2:
Example 3:
Constraints:
• The number of nodes in the tree is in the range
•
Follow up: Can you flatten the tree in-place (with
114. Flatten Binary Tree to Linked List
Topic: Linked List, Stack, Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, flatten the tree into a "linked list":• The "linked list" should use the same
TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.• The "linked list" should be in the same order as a pre-order traversal of the binary tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg
Input: root = [1,2,5,3,4,null,6]
Output: [1,null,2,null,3,null,4,null,5,null,6]
Example 2:
Input: root = []
Output: []
Example 3:
Input: root = [0]
Output: [0]
Constraints:
• The number of nodes in the tree is in the range
[0, 2000].•
-100 <= Node.val <= 100Follow up: Can you flatten the tree in-place (with
O(1) extra space)?2022-07-28
242. Valid Anagram
Topic: Hash Table, String, Sorting
Difficulty: Easy
Problem:
Given two strings
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Example 2:
Constraints:
•
•
Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
242. Valid Anagram
Topic: Hash Table, String, Sorting
Difficulty: Easy
Problem:
Given two strings
s and t, return true if t is an anagram of s, and false otherwise.An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Constraints:
•
1 <= s.length, t.length <= 5 * 10^4•
s and t consist of lowercase English letters.Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
2022-07-29
890. Find and Replace Pattern
Topic: Array, Hash Table, String
Difficulty: Medium
Problem:
Given a list of strings
A word matches the pattern if there exists a permutation of letters
Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.
Example 1:
Example 2:
Constraints:
•
•
•
•
890. Find and Replace Pattern
Topic: Array, Hash Table, String
Difficulty: Medium
Problem:
Given a list of strings
words and a string pattern, return a list of words[i] that match pattern. You may return the answer in any order.A word matches the pattern if there exists a permutation of letters
p so that after replacing every letter x in the pattern with p(x), we get the desired word.Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.
Example 1:
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
"ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation, since a and b map to the same letter.
Example 2:
Input: words = ["a","b","c"], pattern = "a"
Output: ["a","b","c"]
Constraints:
•
1 <= pattern.length <= 20•
1 <= words.length <= 50•
words[i].length == pattern.length•
pattern and words[i] are lowercase English letters.2022-07-30
916. Word Subsets
Topic: Array, Hash Table, String
Difficulty: Medium
Problem:
You are given two string arrays
A string
• For example,
A string
Return an array of all the universal strings in
Example 1:
Example 2:
Constraints:
•
•
•
• All the strings of
916. Word Subsets
Topic: Array, Hash Table, String
Difficulty: Medium
Problem:
You are given two string arrays
words1 and words2.A string
b is a subset of string a if every letter in b occurs in a including multiplicity.• For example,
"wrr" is a subset of "warrior" but is not a subset of "world".A string
a from words1 is universal if for every string b in words2, b is a subset of a.Return an array of all the universal strings in
words1. You may return the answer in any order.Example 1:
Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"]
Output: ["facebook","google","leetcode"]
Example 2:
Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"]
Output: ["apple","google","leetcode"]
Constraints:
•
1 <= words1.length, words2.length <= 10^4•
1 <= words1[i].length, words2[i].length <= 10•
words1[i] and words2[i] consist only of lowercase English letters.• All the strings of
words1 are unique.2022-07-31
307. Range Sum Query - Mutable
Topic: Array, Design, Binary Indexed Tree, Segment Tree
Difficulty: Medium
Problem:
Given an integer array
1. Update the value of an element in
2. Calculate the sum of the elements of
Implement the
•
•
•
Example 1:
Constraints:
•
•
•
•
•
• At most
307. Range Sum Query - Mutable
Topic: Array, Design, Binary Indexed Tree, Segment Tree
Difficulty: Medium
Problem:
Given an integer array
nums, handle multiple queries of the following types:1. Update the value of an element in
nums.2. Calculate the sum of the elements of
nums between indices left and right inclusive where left <= right.Implement the
NumArray class:•
NumArray(int[] nums) Initializes the object with the integer array nums.•
void update(int index, int val) Updates the value of nums[index] to be val.•
int sumRange(int left, int right) Returns the sum of the elements of nums between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]).Example 1:
Input
["NumArray", "sumRange", "update", "sumRange"]
[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]
Output
[null, 9, null, 8]
Explanation
NumArray numArray = new NumArray([1, 3, 5]);
numArray.sumRange(0, 2); // return 1 + 3 + 5 = 9
numArray.update(1, 2); // nums = [1, 2, 5]
numArray.sumRange(0, 2); // return 1 + 2 + 5 = 8
Constraints:
•
1 <= nums.length <= 3 * 10^4•
-100 <= nums[i] <= 100•
0 <= index < nums.length•
-100 <= val <= 100•
0 <= left <= right < nums.length• At most
3 * 10^4 calls will be made to update and sumRange.2022-08-01
62. Unique Paths
Topic: Math, Dynamic Programming, Combinatorics
Difficulty: Medium
Problem:
There is a robot on an
Given the two integers
The test cases are generated so that the answer will be less than or equal to
Example 1:
Image: https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png
Example 2:
Constraints:
•
62. Unique Paths
Topic: Math, Dynamic Programming, Combinatorics
Difficulty: Medium
Problem:
There is a robot on an
m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.Given the two integers
m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.The test cases are generated so that the answer will be less than or equal to
2 * 10^9.Example 1:
Image: https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png
Input: m = 3, n = 7
Output: 28
Example 2:
Input: m = 3, n = 2
Output: 3
Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Down -> Down
2. Down -> Down -> Right
3. Down -> Right -> Down
Constraints:
•
1 <= m, n <= 1002022-08-02
378. Kth Smallest Element in a Sorted Matrix
Topic: Array, Binary Search, Sorting, Heap (Priority Queue), Matrix
Difficulty: Medium
Problem:
Given an
Note that it is the
You must find a solution with a memory complexity better than
Example 1:
Example 2:
Constraints:
•
•
•
• All the rows and columns of
•
Follow up:
• Could you solve the problem with a constant memory (i.e.,
• Could you solve the problem in
378. Kth Smallest Element in a Sorted Matrix
Topic: Array, Binary Search, Sorting, Heap (Priority Queue), Matrix
Difficulty: Medium
Problem:
Given an
n x n matrix where each of the rows and columns is sorted in ascending order, return the k^th smallest element in the matrix.Note that it is the
k^th smallest element in the sorted order, not the k^th distinct element.You must find a solution with a memory complexity better than
O(n^2).Example 1:
Input: matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
Output: 13
Explanation: The elements in the matrix are [1,5,9,10,11,12,13,13,15], and the 8^th smallest number is 13
Example 2:
Input: matrix = [[-5]], k = 1
Output: -5
Constraints:
•
n == matrix.length == matrix[i].length•
1 <= n <= 300•
-10^9 <= matrix[i][j] <= 10^9• All the rows and columns of
matrix are guaranteed to be sorted in non-decreasing order.•
1 <= k <= n^2Follow up:
• Could you solve the problem with a constant memory (i.e.,
O(1) memory complexity)?• Could you solve the problem in
O(n) time complexity? The solution may be too advanced for an interview but you may find reading this paper fun.2022-08-03
729. My Calendar I
Topic: Binary Search, Design, Segment Tree, Ordered Set
Difficulty: Medium
Problem:
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking.
A double booking happens when two events have some non-empty intersection (i.e., some moment is common to both events.).
The event can be represented as a pair of integers
Implement the
•
•
Example 1:
Constraints:
•
• At most
729. My Calendar I
Topic: Binary Search, Design, Segment Tree, Ordered Set
Difficulty: Medium
Problem:
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking.
A double booking happens when two events have some non-empty intersection (i.e., some moment is common to both events.).
The event can be represented as a pair of integers
start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start <= x < end.Implement the
MyCalendar class:•
MyCalendar() Initializes the calendar object.•
boolean book(int start, int end) Returns true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.Example 1:
Input
["MyCalendar", "book", "book", "book"]
[[], [10, 20], [15, 25], [20, 30]]
Output
[null, true, false, true]
Explanation
MyCalendar myCalendar = new MyCalendar();
myCalendar.book(10, 20); // return True
myCalendar.book(15, 25); // return False, It can not be booked because time 15 is already booked by another event.
myCalendar.book(20, 30); // return True, The event can be booked, as the first event takes every time less than 20, but not including 20.
Constraints:
•
0 <= start < end <= 10^9• At most
1000 calls will be made to book.2022-08-04
858. Mirror Reflection
Topic: Math, Geometry
Difficulty: Medium
Problem:
There is a special square room with mirrors on each of the four walls. Except for the southwest corner, there are receptors on each of the remaining corners, numbered
The square room has walls of length
Given the two integers
The test cases are guaranteed so that the ray will meet a receptor eventually.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/18/reflection.png
Example 2:
Constraints:
•
858. Mirror Reflection
Topic: Math, Geometry
Difficulty: Medium
Problem:
There is a special square room with mirrors on each of the four walls. Except for the southwest corner, there are receptors on each of the remaining corners, numbered
0, 1, and 2.The square room has walls of length
p and a laser ray from the southwest corner first meets the east wall at a distance q from the 0^th receptor.Given the two integers
p and q, return the number of the receptor that the ray meets first.The test cases are guaranteed so that the ray will meet a receptor eventually.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/18/reflection.png
Input: p = 2, q = 1
Output: 2
Explanation: The ray meets receptor 2 the first time it gets reflected back to the left wall.
Example 2:
Input: p = 3, q = 1
Output: 1
Constraints:
•
1 <= q <= p <= 10002022-08-05
377. Combination Sum IV
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
Given an array of distinct integers
The test cases are generated so that the answer can fit in a 32-bit integer.
Example 1:
Example 2:
Constraints:
•
•
• All the elements of
•
Follow up: What if negative numbers are allowed in the given array? How does it change the problem? What limitation we need to add to the question to allow negative numbers?
377. Combination Sum IV
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
Given an array of distinct integers
nums and a target integer target, return the number of possible combinations that add up to target.The test cases are generated so that the answer can fit in a 32-bit integer.
Example 1:
Input: nums = [1,2,3], target = 4
Output: 7
Explanation:
The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Note that different sequences are counted as different combinations.
Example 2:
Input: nums = [9], target = 3
Output: 0
Constraints:
•
1 <= nums.length <= 200•
1 <= nums[i] <= 1000• All the elements of
nums are unique.•
1 <= target <= 1000Follow up: What if negative numbers are allowed in the given array? How does it change the problem? What limitation we need to add to the question to allow negative numbers?
2022-08-06
458. Poor Pigs
Topic: Math, Dynamic Programming, Combinatorics
Difficulty: Hard
Problem:
There are
You can feed the pigs according to these steps:
1. Choose some live pigs to feed.
2. For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time.
3. Wait for
4. After
5. Repeat this process until you run out of time.
Given
Example 1:
Example 2:
Example 3:
Constraints:
•
•
458. Poor Pigs
Topic: Math, Dynamic Programming, Combinatorics
Difficulty: Hard
Problem:
There are
buckets buckets of liquid, where exactly one of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have minutesToTest minutes to determine which bucket is poisonous.You can feed the pigs according to these steps:
1. Choose some live pigs to feed.
2. For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time.
3. Wait for
minutesToDie minutes. You may not feed any other pigs during this time.4. After
minutesToDie minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.5. Repeat this process until you run out of time.
Given
buckets, minutesToDie, and minutesToTest, return the minimum number of pigs needed to figure out which bucket is poisonous within the allotted time.Example 1:
Input: buckets = 1000, minutesToDie = 15, minutesToTest = 60
Output: 5
Example 2:
Input: buckets = 4, minutesToDie = 15, minutesToTest = 15
Output: 2
Example 3:
Input: buckets = 4, minutesToDie = 15, minutesToTest = 30
Output: 2
Constraints:
•
1 <= buckets <= 1000•
1 <= minutesToDie <= minutesToTest <= 100