2025-04-01
2140. Solving Questions With Brainpower
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are given a 0-indexed 2D integer array
The array describes the questions of an exam, where you have to process the questions in order (i.e., starting from question
• For example, given
• If question
• If instead, question
Return the maximum points you can earn for the exam.
Example 1:
Example 2:
Constraints:
•
•
•
2140. Solving Questions With Brainpower
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are given a 0-indexed 2D integer array
questions where questions[i] = [points_i, brainpower_i].The array describes the questions of an exam, where you have to process the questions in order (i.e., starting from question
0) and make a decision whether to solve or skip each question. Solving question i will earn you points_i points but you will be unable to solve each of the next brainpower_i questions. If you skip question i, you get to make the decision on the next question.• For example, given
questions = [[3, 2], [4, 3], [4, 4], [2, 5]]:• If question
0 is solved, you will earn 3 points but you will be unable to solve questions 1 and 2.• If instead, question
0 is skipped and question 1 is solved, you will earn 4 points but you will be unable to solve questions 2 and 3.Return the maximum points you can earn for the exam.
Example 1:
Input: questions = [[3,2],[4,3],[4,4],[2,5]]
Output: 5
Explanation: The maximum points can be earned by solving questions 0 and 3.
- Solve question 0: Earn 3 points, will be unable to solve the next 2 questions
- Unable to solve questions 1 and 2
- Solve question 3: Earn 2 points
Total points earned: 3 + 2 = 5. There is no other way to earn 5 or more points.
Example 2:
Input: questions = [[1,1],[2,2],[3,3],[4,4],[5,5]]
Output: 7
Explanation: The maximum points can be earned by solving questions 1 and 4.
- Skip question 0
- Solve question 1: Earn 2 points, will be unable to solve the next 2 questions
- Unable to solve questions 2 and 3
- Solve question 4: Earn 5 points
Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.
Constraints:
•
1 <= questions.length <= 10^5•
questions[i].length == 2•
1 <= points_i, brainpower_i <= 10^52025-04-02
2873. Maximum Value of an Ordered Triplet I
Topic: Array
Difficulty: Easy
Problem:
You are given a 0-indexed integer array
Return the maximum value over all triplets of indices
The value of a triplet of indices
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2873. Maximum Value of an Ordered Triplet I
Topic: Array
Difficulty: Easy
Problem:
You are given a 0-indexed integer array
nums.Return the maximum value over all triplets of indices
(i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.The value of a triplet of indices
(i, j, k) is equal to (nums[i] - nums[j]) * nums[k].Example 1:
Input: nums = [12,6,1,2,7]
Output: 77
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
Example 2:
Input: nums = [1,10,3,4,19]
Output: 133
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
It can be shown that there are no ordered triplets of indices with a value greater than 133.
Example 3:
Input: nums = [1,2,3]
Output: 0
Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
Constraints:
•
3 <= nums.length <= 100•
1 <= nums[i] <= 10^62025-04-03
2874. Maximum Value of an Ordered Triplet II
Topic: Array
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
Return the maximum value over all triplets of indices
The value of a triplet of indices
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2874. Maximum Value of an Ordered Triplet II
Topic: Array
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
nums.Return the maximum value over all triplets of indices
(i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.The value of a triplet of indices
(i, j, k) is equal to (nums[i] - nums[j]) * nums[k].Example 1:
Input: nums = [12,6,1,2,7]
Output: 77
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
Example 2:
Input: nums = [1,10,3,4,19]
Output: 133
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
It can be shown that there are no ordered triplets of indices with a value greater than 133.
Example 3:
Input: nums = [1,2,3]
Output: 0
Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
Constraints:
•
3 <= nums.length <= 10^5•
1 <= nums[i] <= 10^62025-04-04
1123. Lowest Common Ancestor of Deepest Leaves
Topic: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
Recall that:
• The node of a binary tree is a leaf if and only if it has no children
• The depth of the root of the tree is
• The lowest common ancestor of a set
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/01/sketch1.png
Example 2:
Example 3:
Constraints:
• The number of nodes in the tree will be in the range
•
• The values of the nodes in the tree are unique.
Note: This question is the same as 865: <https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/>
1123. Lowest Common Ancestor of Deepest Leaves
Topic: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, return the lowest common ancestor of its deepest leaves.Recall that:
• The node of a binary tree is a leaf if and only if it has no children
• The depth of the root of the tree is
0. if the depth of a node is d, the depth of each of its children is d + 1.• The lowest common ancestor of a set
S of nodes, is the node A with the largest depth such that every node in S is in the subtree with root A.Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/01/sketch1.png
Input: root = [3,5,1,6,2,0,8,null,null,7,4]
Output: [2,7,4]
Explanation: We return the node with value 2, colored in yellow in the diagram.
The nodes coloured in blue are the deepest leaf-nodes of the tree.
Note that nodes 6, 0, and 8 are also leaf nodes, but the depth of them is 2, but the depth of nodes 7 and 4 is 3.
Example 2:
Input: root = [1]
Output: [1]
Explanation: The root is the deepest node in the tree, and it's the lca of itself.
Example 3:
Input: root = [0,1,3,null,2]
Output: [2]
Explanation: The deepest leaf node in the tree is 2, the lca of one node is itself.
Constraints:
• The number of nodes in the tree will be in the range
[1, 1000].•
0 <= Node.val <= 1000• The values of the nodes in the tree are unique.
Note: This question is the same as 865: <https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/>
2025-04-05
1863. Sum of All Subset XOR Totals
Topic: Array, Math, Backtracking, Bit Manipulation, Combinatorics, Enumeration
Difficulty: Easy
Problem:
The XOR total of an array is defined as the bitwise
• For example, the XOR total of the array
Given an array
Note: Subsets with the same elements should be counted multiple times.
An array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1863. Sum of All Subset XOR Totals
Topic: Array, Math, Backtracking, Bit Manipulation, Combinatorics, Enumeration
Difficulty: Easy
Problem:
The XOR total of an array is defined as the bitwise
XOR of all its elements, or 0 if the array is empty.• For example, the XOR total of the array
[2,5,6] is 2 XOR 5 XOR 6 = 1.Given an array
nums, return the sum of all XOR totals for every subset of nums. Note: Subsets with the same elements should be counted multiple times.
An array
a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b.Example 1:
Input: nums = [1,3]
Output: 6
Explanation: The 4 subsets of [1,3] are:
- The empty subset has an XOR total of 0.
- [1] has an XOR total of 1.
- [3] has an XOR total of 3.
- [1,3] has an XOR total of 1 XOR 3 = 2.
0 + 1 + 3 + 2 = 6
Example 2:
Input: nums = [5,1,6]
Output: 28
Explanation: The 8 subsets of [5,1,6] are:
- The empty subset has an XOR total of 0.
- [5] has an XOR total of 5.
- [1] has an XOR total of 1.
- [6] has an XOR total of 6.
- [5,1] has an XOR total of 5 XOR 1 = 4.
- [5,6] has an XOR total of 5 XOR 6 = 3.
- [1,6] has an XOR total of 1 XOR 6 = 7.
- [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2.
0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28
Example 3:
Input: nums = [3,4,5,6,7,8]
Output: 480
Explanation: The sum of all XOR totals for every subset is 480.
Constraints:
•
1 <= nums.length <= 12•
1 <= nums[i] <= 202025-04-06
368. Largest Divisible Subset
Topic: Array, Math, Dynamic Programming, Sorting
Difficulty: Medium
Problem:
Given a set of distinct positive integers
•
•
If there are multiple solutions, return any of them.
Example 1:
Example 2:
Constraints:
•
•
• All the integers in
368. Largest Divisible Subset
Topic: Array, Math, Dynamic Programming, Sorting
Difficulty: Medium
Problem:
Given a set of distinct positive integers
nums, return the largest subset answer such that every pair (answer[i], answer[j]) of elements in this subset satisfies:•
answer[i] % answer[j] == 0, or•
answer[j] % answer[i] == 0If there are multiple solutions, return any of them.
Example 1:
Input: nums = [1,2,3]
Output: [1,2]
Explanation: [1,3] is also accepted.
Example 2:
Input: nums = [1,2,4,8]
Output: [1,2,4,8]
Constraints:
•
1 <= nums.length <= 1000•
1 <= nums[i] <= 2 * 10^9• All the integers in
nums are unique.2025-04-07
416. Partition Equal Subset Sum
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
416. Partition Equal Subset Sum
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
Given an integer array
nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise.Example 1:
Input: nums = [1,5,11,5]
Output: true
Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:
Input: nums = [1,2,3,5]
Output: false
Explanation: The array cannot be partitioned into equal sum subsets.
Constraints:
•
1 <= nums.length <= 200•
1 <= nums[i] <= 1002025-04-08
3396. Minimum Number of Operations to Make Elements in Array Distinct
Topic: Array, Hash Table
Difficulty: Easy
Problem:
You are given an integer array
• Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.
Note that an empty array is considered to have distinct elements. Return the minimum number of operations needed to make the elements in the array distinct.
Example 1:
Input: nums = 1,2,3,4,2,3,3,5,7
Output: 2
Explanation:
• In the first operation, the first 3 elements are removed, resulting in the array
• In the second operation, the next 3 elements are removed, resulting in the array
Therefore, the answer is 2.
Example 2:
Input: nums = 4,5,6,4,4
Output: 2
Explanation:
• In the first operation, the first 3 elements are removed, resulting in the array
• In the second operation, all remaining elements are removed, resulting in an empty array.
Therefore, the answer is 2.
Example 3:
Input: nums = 6,7,8,9
Output: 0
Explanation:
The array already contains distinct elements. Therefore, the answer is 0.
Constraints:
•
•
3396. Minimum Number of Operations to Make Elements in Array Distinct
Topic: Array, Hash Table
Difficulty: Easy
Problem:
You are given an integer array
nums. You need to ensure that the elements in the array are distinct. To achieve this, you can perform the following operation any number of times:• Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.
Note that an empty array is considered to have distinct elements. Return the minimum number of operations needed to make the elements in the array distinct.
Example 1:
Input: nums = 1,2,3,4,2,3,3,5,7
Output: 2
Explanation:
• In the first operation, the first 3 elements are removed, resulting in the array
[4, 2, 3, 3, 5, 7].• In the second operation, the next 3 elements are removed, resulting in the array
[3, 5, 7], which has distinct elements.Therefore, the answer is 2.
Example 2:
Input: nums = 4,5,6,4,4
Output: 2
Explanation:
• In the first operation, the first 3 elements are removed, resulting in the array
[4, 4].• In the second operation, all remaining elements are removed, resulting in an empty array.
Therefore, the answer is 2.
Example 3:
Input: nums = 6,7,8,9
Output: 0
Explanation:
The array already contains distinct elements. Therefore, the answer is 0.
Constraints:
•
1 <= nums.length <= 100•
1 <= nums[i] <= 1002025-04-09
3375. Minimum Operations to Make Array Values Equal to K
Topic: Array, Hash Table
Difficulty: Easy
Problem:
You are given an integer array
An integer
For example, if
You are allowed to perform the following operation on
• Select an integer
• For each index
Return the minimum number of operations required to make every element in
Example 1:
Input: nums = 5,2,5,4,5, k = 2
Output: 2
Explanation:
The operations can be performed in order using valid integers 4 and then 2.
Example 2:
Input: nums = 2,1,2, k = 2
Output: -1
Explanation:
It is impossible to make all the values equal to 2.
Example 3:
Input: nums = 9,7,5,3, k = 1
Output: 4
Explanation:
The operations can be performed using valid integers in the order 7, 5, 3, and 1.
Constraints:
•
•
•
3375. Minimum Operations to Make Array Values Equal to K
Topic: Array, Hash Table
Difficulty: Easy
Problem:
You are given an integer array
nums and an integer k.An integer
h is called valid if all values in the array that are strictly greater than h are identical.For example, if
nums = [10, 8, 10, 8], a valid integer is h = 9 because all nums[i] > 9 are equal to 10, but 5 is not a valid integer.You are allowed to perform the following operation on
nums:• Select an integer
h that is valid for the current values in nums.• For each index
i where nums[i] > h, set nums[i] to h.Return the minimum number of operations required to make every element in
nums equal to k. If it is impossible to make all elements equal to k, return -1.Example 1:
Input: nums = 5,2,5,4,5, k = 2
Output: 2
Explanation:
The operations can be performed in order using valid integers 4 and then 2.
Example 2:
Input: nums = 2,1,2, k = 2
Output: -1
Explanation:
It is impossible to make all the values equal to 2.
Example 3:
Input: nums = 9,7,5,3, k = 1
Output: 4
Explanation:
The operations can be performed using valid integers in the order 7, 5, 3, and 1.
Constraints:
•
1 <= nums.length <= 100•
1 <= nums[i] <= 100•
1 <= k <= 1002025-04-10
2999. Count the Number of Powerful Integers
Topic: Math, String, Dynamic Programming
Difficulty: Hard
Problem:
You are given three integers
A positive integer
Return the total number of powerful integers in the range
A string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
2999. Count the Number of Powerful Integers
Topic: Math, String, Dynamic Programming
Difficulty: Hard
Problem:
You are given three integers
start, finish, and limit. You are also given a 0-indexed string s representing a positive integer.A positive integer
x is called powerful if it ends with s (in other words, s is a suffix of x) and each digit in x is at most limit.Return the total number of powerful integers in the range
[start..finish].A string
x is a suffix of a string y if and only if x is a substring of y that starts from some index (including 0) in y and extends to the index y.length - 1. For example, 25 is a suffix of 5125 whereas 512 is not.Example 1:
Input: start = 1, finish = 6000, limit = 4, s = "124"
Output: 5
Explanation: The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit <= 4, and "124" as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4.
It can be shown that there are only 5 powerful integers in this range.
Example 2:
Input: start = 15, finish = 215, limit = 6, s = "10"
Output: 2
Explanation: The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit <= 6, and "10" as a suffix.
It can be shown that there are only 2 powerful integers in this range.
Example 3:
Input: start = 1000, finish = 2000, limit = 4, s = "3000"
Output: 0
Explanation: All integers in the range [1000..2000] are smaller than 3000, hence "3000" cannot be a suffix of any integer in this range.
Constraints:
•
1 <= start <= finish <= 10^15•
1 <= limit <= 9•
1 <= s.length <= floor(log_10(finish)) + 1•
s only consists of numeric digits which are at most limit.•
s does not have leading zeros.2025-04-11
2843. Count Symmetric Integers
Topic: Math, Enumeration
Difficulty: Easy
Problem:
You are given two positive integers
An integer
Return the number of symmetric integers in the range
Example 1:
Example 2:
Constraints:
•
2843. Count Symmetric Integers
Topic: Math, Enumeration
Difficulty: Easy
Problem:
You are given two positive integers
low and high.An integer
x consisting of 2 * n digits is symmetric if the sum of the first n digits of x is equal to the sum of the last n digits of x. Numbers with an odd number of digits are never symmetric.Return the number of symmetric integers in the range
[low, high].Example 1:
Input: low = 1, high = 100
Output: 9
Explanation: There are 9 symmetric integers between 1 and 100: 11, 22, 33, 44, 55, 66, 77, 88, and 99.
Example 2:
Input: low = 1200, high = 1230
Output: 4
Explanation: There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230.
Constraints:
•
1 <= low <= high <= 10^42025-04-12
3272. Find the Count of Good Integers
Topic: Hash Table, Math, Combinatorics, Enumeration
Difficulty: Hard
Problem:
You are given two positive integers
An integer
•
•
An integer is called good if its digits can be rearranged to form a k-palindromic integer. For example, for
Return the count of good integers containing
Note that any integer must not have leading zeros, neither before nor after rearrangement. For example, 1010 cannot be rearranged to form 101.
Example 1:
Input: n = 3, k = 5
Output: 27
Explanation:
Some of the good integers are:
• 551 because it can be rearranged to form 515.
• 525 because it is already k-palindromic.
Example 2:
Input: n = 1, k = 4
Output: 2
Explanation:
The two good integers are 4 and 8.
Example 3:
Input: n = 5, k = 6
Output: 2468
Constraints:
•
•
3272. Find the Count of Good Integers
Topic: Hash Table, Math, Combinatorics, Enumeration
Difficulty: Hard
Problem:
You are given two positive integers
n and k.An integer
x is called k-palindromic if:•
x is a palindrome.•
x is divisible by k.An integer is called good if its digits can be rearranged to form a k-palindromic integer. For example, for
k = 2, 2020 can be rearranged to form the k-palindromic integer 2002, whereas 1010 cannot be rearranged to form a k-palindromic integer.Return the count of good integers containing
n digits.Note that any integer must not have leading zeros, neither before nor after rearrangement. For example, 1010 cannot be rearranged to form 101.
Example 1:
Input: n = 3, k = 5
Output: 27
Explanation:
Some of the good integers are:
• 551 because it can be rearranged to form 515.
• 525 because it is already k-palindromic.
Example 2:
Input: n = 1, k = 4
Output: 2
Explanation:
The two good integers are 4 and 8.
Example 3:
Input: n = 5, k = 6
Output: 2468
Constraints:
•
1 <= n <= 10•
1 <= k <= 92025-04-13
1922. Count Good Numbers
Topic: Math, Recursion
Difficulty: Medium
Problem:
A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime (
• For example,
Given an integer
A digit string is a string consisting of digits
Example 1:
Example 2:
Example 3:
Constraints:
•
1922. Count Good Numbers
Topic: Math, Recursion
Difficulty: Medium
Problem:
A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime (
2, 3, 5, or 7).• For example,
"2582" is good because the digits (2 and 8) at even positions are even and the digits (5 and 2) at odd positions are prime. However, "3245" is not good because 3 is at an even index but is not even.Given an integer
n, return the total number of good digit strings of length n. Since the answer may be large, return it modulo 10^9 + 7.A digit string is a string consisting of digits
0 through 9 that may contain leading zeros.Example 1:
Input: n = 1
Output: 5
Explanation: The good numbers of length 1 are "0", "2", "4", "6", "8".
Example 2:
Input: n = 4
Output: 400
Example 3:
Input: n = 50
Output: 564908303
Constraints:
•
1 <= n <= 10^152025-04-14
1534. Count Good Triplets
Topic: Array, Enumeration
Difficulty: Easy
Problem:
Given an array of integers
A triplet
•
•
•
•
Where
Return the number of good triplets.
Example 1:
Example 2:
Constraints:
•
•
•
1534. Count Good Triplets
Topic: Array, Enumeration
Difficulty: Easy
Problem:
Given an array of integers
arr, and three integers a, b and c. You need to find the number of good triplets.A triplet
(arr[i], arr[j], arr[k]) is good if the following conditions are true:•
0 <= i < j < k < arr.length•
|arr[i] - arr[j]| <= a•
|arr[j] - arr[k]| <= b•
|arr[i] - arr[k]| <= cWhere
|x| denotes the absolute value of x.Return the number of good triplets.
Example 1:
Input: arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3
Output: 4
Explanation: There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)].
Example 2:
Input: arr = [1,1,2,2,3], a = 0, b = 0, c = 1
Output: 0
Explanation: No triplet satisfies all conditions.
Constraints:
•
3 <= arr.length <= 100•
0 <= arr[i] <= 1000•
0 <= a, b, c <= 10002025-04-15
2179. Count Good Triplets in an Array
Topic: Array, Binary Search, Divide and Conquer, Binary Indexed Tree, Segment Tree, Merge Sort, Ordered Set
Difficulty: Hard
Problem:
You are given two 0-indexed arrays
A good triplet is a set of
Return the total number of good triplets.
Example 1:
Example 2:
Constraints:
•
•
•
•
2179. Count Good Triplets in an Array
Topic: Array, Binary Search, Divide and Conquer, Binary Indexed Tree, Segment Tree, Merge Sort, Ordered Set
Difficulty: Hard
Problem:
You are given two 0-indexed arrays
nums1 and nums2 of length n, both of which are permutations of [0, 1, ..., n - 1].A good triplet is a set of
3 distinct values which are present in increasing order by position both in nums1 and nums2. In other words, if we consider pos1_v as the index of the value v in nums1 and pos2_v as the index of the value v in nums2, then a good triplet will be a set (x, y, z) where 0 <= x, y, z <= n - 1, such that pos1_x < pos1_y < pos1_z and pos2_x < pos2_y < pos2_z.Return the total number of good triplets.
Example 1:
Input: nums1 = [2,0,1,3], nums2 = [0,1,2,3]
Output: 1
Explanation:
There are 4 triplets (x,y,z) such that pos1_x < pos1_y < pos1_z. They are (2,0,1), (2,0,3), (2,1,3), and (0,1,3).
Out of those triplets, only the triplet (0,1,3) satisfies pos2_x < pos2_y < pos2_z. Hence, there is only 1 good triplet.
Example 2:
Input: nums1 = [4,0,1,3,2], nums2 = [4,1,0,2,3]
Output: 4
Explanation: The 4 good triplets are (4,0,3), (4,0,2), (4,1,3), and (4,1,2).
Constraints:
•
n == nums1.length == nums2.length•
3 <= n <= 10^5•
0 <= nums1[i], nums2[i] <= n - 1•
nums1 and nums2 are permutations of [0, 1, ..., n - 1].2025-04-16
2537. Count the Number of Good Subarrays
Topic: Array, Hash Table, Sliding Window
Difficulty: Medium
Problem:
Given an integer array
A subarray
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Example 2:
Constraints:
•
•
2537. Count the Number of Good Subarrays
Topic: Array, Hash Table, Sliding Window
Difficulty: Medium
Problem:
Given an integer array
nums and an integer k, return the number of good subarrays of nums.A subarray
arr is good if there are at least k pairs of indices (i, j) such that i < j and arr[i] == arr[j].A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,1,1,1,1], k = 10
Output: 1
Explanation: The only good subarray is the array nums itself.
Example 2:
Input: nums = [3,1,4,3,2,2,4], k = 2
Output: 4
Explanation: There are 4 different good subarrays:
- [3,1,4,3,2,2] that has 2 pairs.
- [3,1,4,3,2,2,4] that has 3 pairs.
- [1,4,3,2,2,4] that has 2 pairs.
- [4,3,2,2,4] that has 2 pairs.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i], k <= 10^92025-04-17
2176. Count Equal and Divisible Pairs in an Array
Topic: Array
Difficulty: Easy
Problem:
Given a 0-indexed integer array
Example 1:
Example 2:
Constraints:
•
•
2176. Count Equal and Divisible Pairs in an Array
Topic: Array
Difficulty: Easy
Problem:
Given a 0-indexed integer array
nums of length n and an integer k, return the number of pairs (i, j) where 0 <= i < j < n, such that nums[i] == nums[j] and (i * j) is divisible by k.Example 1:
Input: nums = [3,1,2,2,2,1,3], k = 2
Output: 4
Explanation:
There are 4 pairs that meet all the requirements:
- nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2.
- nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2.
- nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2.
- nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2.
Example 2:
Input: nums = [1,2,3,4], k = 1
Output: 0
Explanation: Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements.
Constraints:
•
1 <= nums.length <= 100•
1 <= nums[i], k <= 1002025-04-18
38. Count and Say
Topic: String
Difficulty: Medium
Problem:
The count-and-say sequence is a sequence of digit strings defined by the recursive formula:
•
•
Run-length encoding (RLE) is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string
Given a positive integer
Example 1:
Input: n = 4
Output: "1211"
Explanation:
Example 2:
Input: n = 1
Output: "1"
Explanation:
This is the base case.
Constraints:
•
Follow up: Could you solve it iteratively?
38. Count and Say
Topic: String
Difficulty: Medium
Problem:
The count-and-say sequence is a sequence of digit strings defined by the recursive formula:
•
countAndSay(1) = "1"•
countAndSay(n) is the run-length encoding of countAndSay(n - 1).Run-length encoding (RLE) is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string
"3322251" we replace "33" with "23", replace "222" with "32", replace "5" with "15" and replace "1" with "11". Thus the compressed string becomes "23321511".Given a positive integer
n, return the n^th element of the count-and-say sequence.Example 1:
Input: n = 4
Output: "1211"
Explanation:
countAndSay(1) = "1"
countAndSay(2) = RLE of "1" = "11"
countAndSay(3) = RLE of "11" = "21"
countAndSay(4) = RLE of "21" = "1211"
Example 2:
Input: n = 1
Output: "1"
Explanation:
This is the base case.
Constraints:
•
1 <= n <= 30Follow up: Could you solve it iteratively?
2025-04-19
2563. Count the Number of Fair Pairs
Topic: Array, Two Pointers, Binary Search, Sorting
Difficulty: Medium
Problem:
Given a 0-indexed integer array
A pair
•
•
Example 1:
Example 2:
Constraints:
•
•
•
•
2563. Count the Number of Fair Pairs
Topic: Array, Two Pointers, Binary Search, Sorting
Difficulty: Medium
Problem:
Given a 0-indexed integer array
nums of size n and two integers lower and upper, return the number of fair pairs.A pair
(i, j) is fair if:•
0 <= i < j < n, and•
lower <= nums[i] + nums[j] <= upperExample 1:
Input: nums = [0,1,7,4,4,5], lower = 3, upper = 6
Output: 6
Explanation: There are 6 fair pairs: (0,3), (0,4), (0,5), (1,3), (1,4), and (1,5).
Example 2:
Input: nums = [1,7,9,2,5], lower = 11, upper = 11
Output: 1
Explanation: There is a single fair pair: (2,3).
Constraints:
•
1 <= nums.length <= 10^5•
nums.length == n•
-10^9 <= nums[i] <= 10^9•
-10^9 <= lower <= upper <= 10^92025-04-20
781. Rabbits in Forest
Topic: Array, Hash Table, Math, Greedy
Difficulty: Medium
Problem:
There is a forest with an unknown number of rabbits. We asked n rabbits "How many rabbits have the same color as you?" and collected the answers in an integer array
Given the array
Example 1:
Example 2:
Constraints:
•
•
781. Rabbits in Forest
Topic: Array, Hash Table, Math, Greedy
Difficulty: Medium
Problem:
There is a forest with an unknown number of rabbits. We asked n rabbits "How many rabbits have the same color as you?" and collected the answers in an integer array
answers where answers[i] is the answer of the i^th rabbit.Given the array
answers, return the minimum number of rabbits that could be in the forest.Example 1:
Input: answers = [1,1,2]
Output: 5
Explanation:
The two rabbits that answered "1" could both be the same color, say red.
The rabbit that answered "2" can't be red or the answers would be inconsistent.
Say the rabbit that answered "2" was blue.
Then there should be 2 other blue rabbits in the forest that didn't answer into the array.
The smallest possible number of rabbits in the forest is therefore 5: 3 that answered plus 2 that didn't.
Example 2:
Input: answers = [10,10,10]
Output: 11
Constraints:
•
1 <= answers.length <= 1000•
0 <= answers[i] < 1000