2025-06-03
1298. Maximum Candies You Can Get from Boxes
Topic: Array, Breadth-First Search, Graph
Difficulty: Hard
Problem:
You have
•
•
•
•
You are given an integer array
Return the maximum number of candies you can get following the rules above.
Example 1:
Example 2:
Constraints:
•
•
•
•
•
•
• All values of
•
•
• All values of
• Each box is contained in one box at most.
•
•
1298. Maximum Candies You Can Get from Boxes
Topic: Array, Breadth-First Search, Graph
Difficulty: Hard
Problem:
You have
n boxes labeled from 0 to n - 1. You are given four arrays: status, candies, keys, and containedBoxes where:•
status[i] is 1 if the i^th box is open and 0 if the i^th box is closed,•
candies[i] is the number of candies in the i^th box,•
keys[i] is a list of the labels of the boxes you can open after opening the i^th box.•
containedBoxes[i] is a list of the boxes you found inside the i^th box.You are given an integer array
initialBoxes that contains the labels of the boxes you initially have. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it.Return the maximum number of candies you can get following the rules above.
Example 1:
Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0]
Output: 16
Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2.
Box 1 is closed and you do not have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2.
In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed.
Total number of candies collected = 7 + 4 + 5 = 16 candy.
Example 2:
Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0]
Output: 6
Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys.
The total number of candies will be 6.
Constraints:
•
n == status.length == candies.length == keys.length == containedBoxes.length•
1 <= n <= 1000•
status[i] is either 0 or 1.•
1 <= candies[i] <= 1000•
0 <= keys[i].length <= n•
0 <= keys[i][j] < n• All values of
keys[i] are unique.•
0 <= containedBoxes[i].length <= n•
0 <= containedBoxes[i][j] < n• All values of
containedBoxes[i] are unique.• Each box is contained in one box at most.
•
0 <= initialBoxes.length <= n•
0 <= initialBoxes[i] < n2025-06-04
3403. Find the Lexicographically Largest String From the Box I
Topic: Two Pointers, String, Enumeration
Difficulty: Medium
Problem:
You are given a string
Alice is organizing a game for her
•
• All the split words are put into a box.
Find the lexicographically largest string from the box after all the rounds are finished.
Example 1:
Input: word = "dbca", numFriends = 2
Output: "dbc"
Explanation:
All possible splits are:
•
•
•
Example 2:
Input: word = "gggg", numFriends = 4
Output: "g"
Explanation:
The only possible split is:
Constraints:
•
•
•
3403. Find the Lexicographically Largest String From the Box I
Topic: Two Pointers, String, Enumeration
Difficulty: Medium
Problem:
You are given a string
word, and an integer numFriends.Alice is organizing a game for her
numFriends friends. There are multiple rounds in the game, where in each round:•
word is split into numFriends non-empty strings, such that no previous round has had the exact same split.• All the split words are put into a box.
Find the lexicographically largest string from the box after all the rounds are finished.
Example 1:
Input: word = "dbca", numFriends = 2
Output: "dbc"
Explanation:
All possible splits are:
•
"d" and "bca".•
"db" and "ca".•
"dbc" and "a".Example 2:
Input: word = "gggg", numFriends = 4
Output: "g"
Explanation:
The only possible split is:
"g", "g", "g", and "g".Constraints:
•
1 <= word.length <= 5 * 10^3•
word consists only of lowercase English letters.•
1 <= numFriends <= word.length2025-06-05
1061. Lexicographically Smallest Equivalent String
Topic: String, Union Find
Difficulty: Medium
Problem:
You are given two strings of the same length
We say
• For example, if
Equivalent characters follow the usual rules of any equivalence relation:
• Reflexivity:
• Symmetry:
• Transitivity:
For example, given the equivalency information from
Return the lexicographically smallest equivalent string of
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1061. Lexicographically Smallest Equivalent String
Topic: String, Union Find
Difficulty: Medium
Problem:
You are given two strings of the same length
s1 and s2 and a string baseStr.We say
s1[i] and s2[i] are equivalent characters.• For example, if
s1 = "abc" and s2 = "cde", then we have 'a' == 'c', 'b' == 'd', and 'c' == 'e'.Equivalent characters follow the usual rules of any equivalence relation:
• Reflexivity:
'a' == 'a'.• Symmetry:
'a' == 'b' implies 'b' == 'a'.• Transitivity:
'a' == 'b' and 'b' == 'c' implies 'a' == 'c'.For example, given the equivalency information from
s1 = "abc" and s2 = "cde", "acd" and "aab" are equivalent strings of baseStr = "eed", and "aab" is the lexicographically smallest equivalent string of baseStr.Return the lexicographically smallest equivalent string of
baseStr by using the equivalency information from s1 and s2.Example 1:
Input: s1 = "parker", s2 = "morris", baseStr = "parser"
Output: "makkek"
Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
Example 2:
Input: s1 = "hello", s2 = "world", baseStr = "hold"
Output: "hdld"
Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
Example 3:
Input: s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
Output: "aauaaaaada"
Explanation: We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
Constraints:
•
1 <= s1.length, s2.length, baseStr <= 1000•
s1.length == s2.length•
s1, s2, and baseStr consist of lowercase English letters.2025-06-06
2434. Using a Robot to Print the Lexicographically Smallest String
Topic: Hash Table, String, Stack, Greedy
Difficulty: Medium
Problem:
You are given a string
• Remove the first character of a string
• Remove the last character of a string
Return the lexicographically smallest string that can be written on the paper.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2434. Using a Robot to Print the Lexicographically Smallest String
Topic: Hash Table, String, Stack, Greedy
Difficulty: Medium
Problem:
You are given a string
s and a robot that currently holds an empty string t. Apply one of the following operations until s and t are both empty:• Remove the first character of a string
s and give it to the robot. The robot will append this character to the string t.• Remove the last character of a string
t and give it to the robot. The robot will write this character on paper.Return the lexicographically smallest string that can be written on the paper.
Example 1:
Input: s = "zza"
Output: "azz"
Explanation: Let p denote the written string.
Initially p="", s="zza", t="".
Perform first operation three times p="", s="", t="zza".
Perform second operation three times p="azz", s="", t="".
Example 2:
Input: s = "bac"
Output: "abc"
Explanation: Let p denote the written string.
Perform first operation twice p="", s="c", t="ba".
Perform second operation twice p="ab", s="c", t="".
Perform first operation p="ab", s="", t="c".
Perform second operation p="abc", s="", t="".
Example 3:
Input: s = "bdda"
Output: "addb"
Explanation: Let p denote the written string.
Initially p="", s="bdda", t="".
Perform first operation four times p="", s="", t="bdda".
Perform second operation four times p="addb", s="", t="".
Constraints:
•
1 <= s.length <= 10^5•
s consists of only English lowercase letters.2025-06-08
386. Lexicographical Numbers
Topic: Depth-First Search, Trie
Difficulty: Medium
Problem:
Given an integer
You must write an algorithm that runs in
Example 1:
Example 2:
Constraints:
•
386. Lexicographical Numbers
Topic: Depth-First Search, Trie
Difficulty: Medium
Problem:
Given an integer
n, return all the numbers in the range [1, n] sorted in lexicographical order.You must write an algorithm that runs in
O(n) time and uses O(1) extra space. Example 1:
Input: n = 13
Output: [1,10,11,12,13,2,3,4,5,6,7,8,9]
Example 2:
Input: n = 2
Output: [1,2]
Constraints:
•
1 <= n <= 5 * 10^42025-06-09
440. K-th Smallest in Lexicographical Order
Topic: Trie
Difficulty: Hard
Problem:
Given two integers
Example 1:
Example 2:
Constraints:
•
440. K-th Smallest in Lexicographical Order
Topic: Trie
Difficulty: Hard
Problem:
Given two integers
n and k, return the k^th lexicographically smallest integer in the range [1, n].Example 1:
Input: n = 13, k = 2
Output: 10
Explanation: The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10.
Example 2:
Input: n = 1, k = 1
Output: 1
Constraints:
•
1 <= k <= n <= 10^92025-06-10
3442. Maximum Difference Between Even and Odd Frequency I
Topic: Hash Table, String, Counting
Difficulty: Easy
Problem:
You are given a string
Your task is to find the maximum difference
•
•
Return this maximum difference.
Example 1:
Input: s = "aaaaabbc"
Output: 3
Explanation:
• The character
• The maximum difference is
Example 2:
Input: s = "abcabcab"
Output: 1
Explanation:
• The character
• The maximum difference is
Constraints:
•
•
•
3442. Maximum Difference Between Even and Odd Frequency I
Topic: Hash Table, String, Counting
Difficulty: Easy
Problem:
You are given a string
s consisting of lowercase English letters. Your task is to find the maximum difference
diff = a_1 - a_2 between the frequency of characters a_1 and a_2 in the string such that:•
a_1 has an odd frequency in the string.•
a_2 has an even frequency in the string.Return this maximum difference.
Example 1:
Input: s = "aaaaabbc"
Output: 3
Explanation:
• The character
'a' has an odd frequency of 5, and 'b' has an even frequency of 2.• The maximum difference is
5 - 2 = 3.Example 2:
Input: s = "abcabcab"
Output: 1
Explanation:
• The character
'a' has an odd frequency of 3, and 'c' has an even frequency of 2.• The maximum difference is
3 - 2 = 1.Constraints:
•
3 <= s.length <= 100•
s consists only of lowercase English letters.•
s contains at least one character with an odd frequency and one with an even frequency.2025-06-11
3445. Maximum Difference Between Even and Odd Frequency II
Topic: String, Sliding Window, Enumeration, Prefix Sum
Difficulty: Hard
Problem:
You are given a string
•
• Character
• Character
Return the maximum difference.
Note that
Example 1:
Input: s = "12233", k = 4
Output: -1
Explanation:
For the substring
Example 2:
Input: s = "1122211", k = 3
Output: 1
Explanation:
For the substring
Example 3:
Input: s = "110", k = 3
Output: -1
Constraints:
•
•
• The input is generated that at least one substring has a character with an even frequency and a character with an odd frequency.
•
3445. Maximum Difference Between Even and Odd Frequency II
Topic: String, Sliding Window, Enumeration, Prefix Sum
Difficulty: Hard
Problem:
You are given a string
s and an integer k. Your task is to find the maximum difference between the frequency of two characters, freq[a] - freq[b], in a substring subs of s, such that:•
subs has a size of at least k.• Character
a has an odd frequency in subs.• Character
b has an even frequency in subs.Return the maximum difference.
Note that
subs can contain more than 2 distinct characters.Example 1:
Input: s = "12233", k = 4
Output: -1
Explanation:
For the substring
"12233", the frequency of '1' is 1 and the frequency of '3' is 2. The difference is 1 - 2 = -1.Example 2:
Input: s = "1122211", k = 3
Output: 1
Explanation:
For the substring
"11222", the frequency of '2' is 3 and the frequency of '1' is 2. The difference is 3 - 2 = 1.Example 3:
Input: s = "110", k = 3
Output: -1
Constraints:
•
3 <= s.length <= 3 * 10^4•
s consists only of digits '0' to '4'.• The input is generated that at least one substring has a character with an even frequency and a character with an odd frequency.
•
1 <= k <= s.length2025-06-12
3423. Maximum Difference Between Adjacent Elements in a Circular Array
Topic: Array
Difficulty: Easy
Problem:
Given a circular array
Note: In a circular array, the first and last elements are adjacent.
Example 1:
Input: nums = 1,2,4
Output: 3
Explanation:
Because
Example 2:
Input: nums = -5,-10,-5
Output: 5
Explanation:
The adjacent elements
Constraints:
•
•
3423. Maximum Difference Between Adjacent Elements in a Circular Array
Topic: Array
Difficulty: Easy
Problem:
Given a circular array
nums, find the maximum absolute difference between adjacent elements.Note: In a circular array, the first and last elements are adjacent.
Example 1:
Input: nums = 1,2,4
Output: 3
Explanation:
Because
nums is circular, nums[0] and nums[2] are adjacent. They have the maximum absolute difference of |4 - 1| = 3.Example 2:
Input: nums = -5,-10,-5
Output: 5
Explanation:
The adjacent elements
nums[0] and nums[1] have the maximum absolute difference of |-5 - (-10)| = 5.Constraints:
•
2 <= nums.length <= 100•
-100 <= nums[i] <= 1002025-06-13
2616. Minimize the Maximum Difference of Pairs
Topic: Array, Binary Search, Greedy
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
Note that for a pair of elements at the index
Return the minimum maximum difference among all
Example 1:
Example 2:
Constraints:
•
•
•
2616. Minimize the Maximum Difference of Pairs
Topic: Array, Binary Search, Greedy
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
nums and an integer p. Find p pairs of indices of nums such that the maximum difference amongst all the pairs is minimized. Also, ensure no index appears more than once amongst the p pairs.Note that for a pair of elements at the index
i and j, the difference of this pair is |nums[i] - nums[j]|, where |x| represents the absolute value of x.Return the minimum maximum difference among all
p pairs. We define the maximum of an empty set to be zero.Example 1:
Input: nums = [10,1,2,7,1,3], p = 2
Output: 1
Explanation: The first pair is formed from the indices 1 and 4, and the second pair is formed from the indices 2 and 5.
The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1. Therefore, we return 1.
Example 2:
Input: nums = [4,2,1,2], p = 1
Output: 0
Explanation: Let the indices 1 and 3 form a pair. The difference of that pair is |2 - 2| = 0, which is the minimum we can attain.
Constraints:
•
1 <= nums.length <= 10^5•
0 <= nums[i] <= 10^9•
0 <= p <= (nums.length)/22025-06-14
2566. Maximum Difference by Remapping a Digit
Topic: Math, Greedy
Difficulty: Easy
Problem:
You are given an integer
Return the difference between the maximum and minimum values Bob can make by remapping exactly one digit in
Notes:
• When Bob remaps a digit d1 to another digit d2, Bob replaces all occurrences of
• Bob can remap a digit to itself, in which case
• Bob can remap different digits for obtaining minimum and maximum values respectively.
• The resulting number after remapping can contain leading zeroes.
Example 1:
Example 2:
Constraints:
•
2566. Maximum Difference by Remapping a Digit
Topic: Math, Greedy
Difficulty: Easy
Problem:
You are given an integer
num. You know that Bob will sneakily remap one of the 10 possible digits (0 to 9) to another digit.Return the difference between the maximum and minimum values Bob can make by remapping exactly one digit in
num.Notes:
• When Bob remaps a digit d1 to another digit d2, Bob replaces all occurrences of
d1 in num with d2.• Bob can remap a digit to itself, in which case
num does not change.• Bob can remap different digits for obtaining minimum and maximum values respectively.
• The resulting number after remapping can contain leading zeroes.
Example 1:
Input: num = 11891
Output: 99009
Explanation:
To achieve the maximum value, Bob can remap the digit 1 to the digit 9 to yield 99899.
To achieve the minimum value, Bob can remap the digit 1 to the digit 0, yielding 890.
The difference between these two numbers is 99009.
Example 2:
Input: num = 90
Output: 99
Explanation:
The maximum value that can be returned by the function is 99 (if 0 is replaced by 9) and the minimum value that can be returned by the function is 0 (if 9 is replaced by 0).
Thus, we return 99.
Constraints:
•
1 <= num <= 10^82025-06-15
1432. Max Difference You Can Get From Changing an Integer
Topic: Math, Greedy
Difficulty: Medium
Problem:
You are given an integer
• Pick a digit
• Pick another digit
• Replace all the occurrences of
Let
Return the max difference between
Note that neither
Example 1:
Example 2:
Constraints:
•
1432. Max Difference You Can Get From Changing an Integer
Topic: Math, Greedy
Difficulty: Medium
Problem:
You are given an integer
num. You will apply the following steps to num two separate times:• Pick a digit
x (0 <= x <= 9).• Pick another digit
y (0 <= y <= 9). Note y can be equal to x.• Replace all the occurrences of
x in the decimal representation of num by y.Let
a and b be the two results from applying the operation to num independently.Return the max difference between
a and b.Note that neither
a nor b may have any leading zeros, and must not be 0.Example 1:
Input: num = 555
Output: 888
Explanation: The first time pick x = 5 and y = 9 and store the new integer in a.
The second time pick x = 5 and y = 1 and store the new integer in b.
We have now a = 999 and b = 111 and max difference = 888
Example 2:
Input: num = 9
Output: 8
Explanation: The first time pick x = 9 and y = 9 and store the new integer in a.
The second time pick x = 9 and y = 1 and store the new integer in b.
We have now a = 9 and b = 1 and max difference = 8
Constraints:
•
1 <= num <= 10^82025-06-16
2016. Maximum Difference Between Increasing Elements
Topic: Array
Difficulty: Easy
Problem:
Given a 0-indexed integer array
Return the maximum difference. If no such
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
2016. Maximum Difference Between Increasing Elements
Topic: Array
Difficulty: Easy
Problem:
Given a 0-indexed integer array
nums of size n, find the maximum difference between nums[i] and nums[j] (i.e., nums[j] - nums[i]), such that 0 <= i < j < n and nums[i] < nums[j].Return the maximum difference. If no such
i and j exists, return -1.Example 1:
Input: nums = [7,1,5,4]
Output: 4
Explanation:
The maximum difference occurs with i = 1 and j = 2, nums[j] - nums[i] = 5 - 1 = 4.
Note that with i = 1 and j = 0, the difference nums[j] - nums[i] = 7 - 1 = 6, but i > j, so it is not valid.
Example 2:
Input: nums = [9,4,3,2]
Output: -1
Explanation:
There is no i and j such that i < j and nums[i] < nums[j].
Example 3:
Input: nums = [1,5,2,10]
Output: 9
Explanation:
The maximum difference occurs with i = 0 and j = 3, nums[j] - nums[i] = 10 - 1 = 9.
Constraints:
•
n == nums.length•
2 <= n <= 1000•
1 <= nums[i] <= 10^92025-06-17
3405. Count the Number of Arrays with K Matching Adjacent Elements
Topic: Math, Combinatorics
Difficulty: Hard
Problem:
You are given three integers
• Each element in
• Exactly
Return the number of good arrays that can be formed.
Since the answer may be very large, return it modulo
Example 1:
Input: n = 3, m = 2, k = 1
Output: 4
Explanation:
• There are 4 good arrays. They are
• Hence, the answer is 4.
Example 2:
Input: n = 4, m = 2, k = 2
Output: 6
Explanation:
• The good arrays are
• Hence, the answer is 6.
Example 3:
Input: n = 5, m = 2, k = 0
Output: 2
Explanation:
• The good arrays are
Constraints:
•
•
•
3405. Count the Number of Arrays with K Matching Adjacent Elements
Topic: Math, Combinatorics
Difficulty: Hard
Problem:
You are given three integers
n, m, k. A good array arr of size n is defined as follows:• Each element in
arr is in the inclusive range [1, m].• Exactly
k indices i (where 1 <= i < n) satisfy the condition arr[i - 1] == arr[i].Return the number of good arrays that can be formed.
Since the answer may be very large, return it modulo
10^9 + 7.Example 1:
Input: n = 3, m = 2, k = 1
Output: 4
Explanation:
• There are 4 good arrays. They are
[1, 1, 2], [1, 2, 2], [2, 1, 1] and [2, 2, 1].• Hence, the answer is 4.
Example 2:
Input: n = 4, m = 2, k = 2
Output: 6
Explanation:
• The good arrays are
[1, 1, 1, 2], [1, 1, 2, 2], [1, 2, 2, 2], [2, 1, 1, 1], [2, 2, 1, 1] and [2, 2, 2, 1].• Hence, the answer is 6.
Example 3:
Input: n = 5, m = 2, k = 0
Output: 2
Explanation:
• The good arrays are
[1, 2, 1, 2, 1] and [2, 1, 2, 1, 2]. Hence, the answer is 2.Constraints:
•
1 <= n <= 10^5•
1 <= m <= 10^5•
0 <= k <= n - 12025-06-18
2966. Divide Array Into Arrays With Max Difference
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
You are given an integer array
Divide the array
• The difference between any two elements in one array is less than or equal to
Return a 2D array containing the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.
Example 1:
Input: nums = 1,3,4,8,7,9,3,5,1, k = 2
Output: [1,1,3,3,4,5,7,8,9]
Explanation:
The difference between any two elements in each array is less than or equal to 2.
Example 2:
Input: nums = 2,4,2,2,5,2, k = 2
Output:
Explanation:
Different ways to divide
• [2,2,2,2,4,5] (and its permutations)
• [2,2,4,2,2,5] (and its permutations)
Because there are four 2s there will be an array with the elements 2 and 5 no matter how we divide it. since
Example 3:
Input: nums = 4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11, k = 14
Output: [2,2,12,4,8,5,5,9,7,7,8,5,5,9,10,11,12,2]
Explanation:
The difference between any two elements in each array is less than or equal to 14.
Constraints:
•
•
•
•
•
2966. Divide Array Into Arrays With Max Difference
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
You are given an integer array
nums of size n where n is a multiple of 3 and a positive integer k.Divide the array
nums into n / 3 arrays of size 3 satisfying the following condition:• The difference between any two elements in one array is less than or equal to
k.Return a 2D array containing the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.
Example 1:
Input: nums = 1,3,4,8,7,9,3,5,1, k = 2
Output: [1,1,3,3,4,5,7,8,9]
Explanation:
The difference between any two elements in each array is less than or equal to 2.
Example 2:
Input: nums = 2,4,2,2,5,2, k = 2
Output:
Explanation:
Different ways to divide
nums into 2 arrays of size 3 are:• [2,2,2,2,4,5] (and its permutations)
• [2,2,4,2,2,5] (and its permutations)
Because there are four 2s there will be an array with the elements 2 and 5 no matter how we divide it. since
5 - 2 = 3 > k, the condition is not satisfied and so there is no valid division.Example 3:
Input: nums = 4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11, k = 14
Output: [2,2,12,4,8,5,5,9,7,7,8,5,5,9,10,11,12,2]
Explanation:
The difference between any two elements in each array is less than or equal to 14.
Constraints:
•
n == nums.length•
1 <= n <= 10^5•
n is a multiple of 3•
1 <= nums[i] <= 10^5•
1 <= k <= 10^52025-06-19
2294. Partition Array Such That Maximum Difference Is K
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
You are given an integer array
Return the minimum number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is at most
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
2294. Partition Array Such That Maximum Difference Is K
Topic: Array, Greedy, Sorting
Difficulty: Medium
Problem:
You are given an integer array
nums and an integer k. You may partition nums into one or more subsequences such that each element in nums appears in exactly one of the subsequences.Return the minimum number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is at most
k.A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: nums = [3,6,1,2,5], k = 2
Output: 2
Explanation:
We can partition nums into the two subsequences [3,1,2] and [6,5].
The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2.
The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1.
Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed.
Example 2:
Input: nums = [1,2,3], k = 1
Output: 2
Explanation:
We can partition nums into the two subsequences [1,2] and [3].
The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1.
The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0.
Since two subsequences were created, we return 2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3].
Example 3:
Input: nums = [2,2,4,5], k = 0
Output: 3
Explanation:
We can partition nums into the three subsequences [2,2], [4], and [5].
The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0.
The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0.
The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0.
Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed.
Constraints:
•
1 <= nums.length <= 10^5•
0 <= nums[i] <= 10^5•
0 <= k <= 10^52025-06-20
3443. Maximum Manhattan Distance After K Changes
Topic: Hash Table, Math, String, Counting
Difficulty: Medium
Problem:
You are given a string
•
•
•
•
Initially, you are at the origin
Find the maximum Manhattan distance from the origin that can be achieved at any time while performing the movements in order.
The Manhattan Distance between two cells
Example 1:
Input: s = "NWSE", k = 1
Output: 3
Explanation:
Change
MovementPosition (x, y)Manhattan DistanceMaximums0 == 'N'(0, 1)0 + 1 = 11s1 == 'W'(-1, 1)1 + 1 = 22s2 == 'N'(-1, 2)1 + 2 = 33s3 == 'E'(0, 2)0 + 2 = 23
The maximum Manhattan distance from the origin that can be achieved is 3. Hence, 3 is the output.
Example 2:
Input: s = "NSWWEW", k = 3
Output: 6
Explanation:
Change
The maximum Manhattan distance from the origin that can be achieved is 6. Hence, 6 is the output.
Constraints:
•
•
•
3443. Maximum Manhattan Distance After K Changes
Topic: Hash Table, Math, String, Counting
Difficulty: Medium
Problem:
You are given a string
s consisting of the characters 'N', 'S', 'E', and 'W', where s[i] indicates movements in an infinite grid:•
'N' : Move north by 1 unit.•
'S' : Move south by 1 unit.•
'E' : Move east by 1 unit.•
'W' : Move west by 1 unit.Initially, you are at the origin
(0, 0). You can change at most k characters to any of the four directions.Find the maximum Manhattan distance from the origin that can be achieved at any time while performing the movements in order.
The Manhattan Distance between two cells
(x_i, y_i) and (x_j, y_j) is |x_i - x_j| + |y_i - y_j|.Example 1:
Input: s = "NWSE", k = 1
Output: 3
Explanation:
Change
s[2] from 'S' to 'N'. The string s becomes "NWNE".MovementPosition (x, y)Manhattan DistanceMaximums0 == 'N'(0, 1)0 + 1 = 11s1 == 'W'(-1, 1)1 + 1 = 22s2 == 'N'(-1, 2)1 + 2 = 33s3 == 'E'(0, 2)0 + 2 = 23
The maximum Manhattan distance from the origin that can be achieved is 3. Hence, 3 is the output.
Example 2:
Input: s = "NSWWEW", k = 3
Output: 6
Explanation:
Change
s[1] from 'S' to 'N', and s[4] from 'E' to 'W'. The string s becomes "NNWWWW".The maximum Manhattan distance from the origin that can be achieved is 6. Hence, 6 is the output.
Constraints:
•
1 <= s.length <= 10^5•
0 <= k <= s.length•
s consists of only 'N', 'S', 'E', and 'W'.2025-06-21
3085. Minimum Deletions to Make String K-Special
Topic: Hash Table, String, Greedy, Sorting, Counting
Difficulty: Medium
Problem:
You are given a string
We consider
Here,
Return the minimum number of characters you need to delete to make
Example 1:
Input: word = "aabcaba", k = 0
Output: 3
Explanation: We can make
Example 2:
Input: word = "dabdcbdcdcd", k = 2
Output: 2
Explanation: We can make
Example 3:
Input: word = "aaabaaa", k = 2
Output: 1
Explanation: We can make
Constraints:
•
•
•
3085. Minimum Deletions to Make String K-Special
Topic: Hash Table, String, Greedy, Sorting, Counting
Difficulty: Medium
Problem:
You are given a string
word and an integer k.We consider
word to be k-special if |freq(word[i]) - freq(word[j])| <= k for all indices i and j in the string.Here,
freq(x) denotes the frequency of the character x in word, and |y| denotes the absolute value of y.Return the minimum number of characters you need to delete to make
word k-special.Example 1:
Input: word = "aabcaba", k = 0
Output: 3
Explanation: We can make
word 0-special by deleting 2 occurrences of "a" and 1 occurrence of "c". Therefore, word becomes equal to "baba" where freq('a') == freq('b') == 2.Example 2:
Input: word = "dabdcbdcdcd", k = 2
Output: 2
Explanation: We can make
word 2-special by deleting 1 occurrence of "a" and 1 occurrence of "d". Therefore, word becomes equal to "bdcbdcdcd" where freq('b') == 2, freq('c') == 3, and freq('d') == 4.Example 3:
Input: word = "aaabaaa", k = 2
Output: 1
Explanation: We can make
word 2-special by deleting 1 occurrence of "b". Therefore, word becomes equal to "aaaaaa" where each letter's frequency is now uniformly 6.Constraints:
•
1 <= word.length <= 10^5•
0 <= k <= 10^5•
word consists only of lowercase English letters.2025-06-22
2138. Divide a String Into Groups of Size k
Topic: String, Simulation
Difficulty: Easy
Problem:
A string
• The first group consists of the first
• For the last group, if the string does not have
Note that the partition is done so that after removing the
Given the string
Example 1:
Example 2:
Constraints:
•
•
•
•
2138. Divide a String Into Groups of Size k
Topic: String, Simulation
Difficulty: Easy
Problem:
A string
s can be partitioned into groups of size k using the following procedure:• The first group consists of the first
k characters of the string, the second group consists of the next k characters of the string, and so on. Each element can be a part of exactly one group.• For the last group, if the string does not have
k characters remaining, a character fill is used to complete the group.Note that the partition is done so that after removing the
fill character from the last group (if it exists) and concatenating all the groups in order, the resultant string should be s.Given the string
s, the size of each group k and the character fill, return a string array denoting the composition of every group s has been divided into, using the above procedure.Example 1:
Input: s = "abcdefghi", k = 3, fill = "x"
Output: ["abc","def","ghi"]
Explanation:
The first 3 characters "abc" form the first group.
The next 3 characters "def" form the second group.
The last 3 characters "ghi" form the third group.
Since all groups can be completely filled by characters from the string, we do not need to use fill.
Thus, the groups formed are "abc", "def", and "ghi".
Example 2:
Input: s = "abcdefghij", k = 3, fill = "x"
Output: ["abc","def","ghi","jxx"]
Explanation:
Similar to the previous example, we are forming the first three groups "abc", "def", and "ghi".
For the last group, we can only use the character 'j' from the string. To complete this group, we add 'x' twice.
Thus, the 4 groups formed are "abc", "def", "ghi", and "jxx".
Constraints:
•
1 <= s.length <= 100•
s consists of lowercase English letters only.•
1 <= k <= 100•
fill is a lowercase English letter.2025-06-23
2081. Sum of k-Mirror Numbers
Topic: Math, Enumeration
Difficulty: Hard
Problem:
A k-mirror number is a positive integer without leading zeros that reads the same both forward and backward in base-10 as well as in base-k.
• For example,
• On the contrary,
Given the base
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2081. Sum of k-Mirror Numbers
Topic: Math, Enumeration
Difficulty: Hard
Problem:
A k-mirror number is a positive integer without leading zeros that reads the same both forward and backward in base-10 as well as in base-k.
• For example,
9 is a 2-mirror number. The representation of 9 in base-10 and base-2 are 9 and 1001 respectively, which read the same both forward and backward.• On the contrary,
4 is not a 2-mirror number. The representation of 4 in base-2 is 100, which does not read the same both forward and backward.Given the base
k and the number n, return the sum of the n smallest k-mirror numbers.Example 1:
Input: k = 2, n = 5
Output: 25
Explanation:
The 5 smallest 2-mirror numbers and their representations in base-2 are listed as follows:
base-10 base-2
1 1
3 11
5 101
7 111
9 1001
Their sum = 1 + 3 + 5 + 7 + 9 = 25.
Example 2:
Input: k = 3, n = 7
Output: 499
Explanation:
The 7 smallest 3-mirror numbers are and their representations in base-3 are listed as follows:
base-10 base-3
1 1
2 2
4 11
8 22
121 11111
151 12121
212 21212
Their sum = 1 + 2 + 4 + 8 + 121 + 151 + 212 = 499.
Example 3:
Input: k = 7, n = 17
Output: 20379000
Explanation: The 17 smallest 7-mirror numbers are:
1, 2, 3, 4, 5, 6, 8, 121, 171, 242, 292, 16561, 65656, 2137312, 4602064, 6597956, 6958596
Constraints:
•
2 <= k <= 9•
1 <= n <= 30