2024-07-31
1105. Filling Bookcase Shelves
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are given an array
We want to place these books in order onto bookcase shelves that have a total width
We choose some of the books to place on this shelf such that the sum of their thickness is less than or equal to
Note that at each step of the above process, the order of the books we place is the same order as the given sequence of books.
• For example, if we have an ordered list of
Return the minimum possible height that the total bookshelf can be after placing shelves in this manner.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/06/24/shelves.png
Example 2:
Constraints:
•
•
•
1105. Filling Bookcase Shelves
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are given an array
books where books[i] = [thickness_i, height_i] indicates the thickness and height of the i^th book. You are also given an integer shelfWidth.We want to place these books in order onto bookcase shelves that have a total width
shelfWidth.We choose some of the books to place on this shelf such that the sum of their thickness is less than or equal to
shelfWidth, then build another level of the shelf of the bookcase so that the total height of the bookcase has increased by the maximum height of the books we just put down. We repeat this process until there are no more books to place.Note that at each step of the above process, the order of the books we place is the same order as the given sequence of books.
• For example, if we have an ordered list of
5 books, we might place the first and second book onto the first shelf, the third book on the second shelf, and the fourth and fifth book on the last shelf.Return the minimum possible height that the total bookshelf can be after placing shelves in this manner.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/06/24/shelves.png
Input: books = [[1,1],[2,3],[2,3],[1,1],[1,1],[1,1],[1,2]], shelfWidth = 4
Output: 6
Explanation:
The sum of the heights of the 3 shelves is 1 + 3 + 2 = 6.
Notice that book number 2 does not have to be on the first shelf.
Example 2:
Input: books = [[1,3],[2,4],[3,2]], shelfWidth = 6
Output: 4
Constraints:
•
1 <= books.length <= 1000•
1 <= thickness_i <= shelfWidth <= 1000•
1 <= height_i <= 10002024-08-01
2678. Number of Senior Citizens
Topic: Array, String
Difficulty: Easy
Problem:
You are given a 0-indexed array of strings
• The first ten characters consist of the phone number of passengers.
• The next character denotes the gender of the person.
• The following two characters are used to indicate the age of the person.
• The last two characters determine the seat allotted to that person.
Return the number of passengers who are strictly more than 60 years old.
Example 1:
Example 2:
Constraints:
•
•
•
•
• The phone numbers and seat numbers of the passengers are distinct.
2678. Number of Senior Citizens
Topic: Array, String
Difficulty: Easy
Problem:
You are given a 0-indexed array of strings
details. Each element of details provides information about a given passenger compressed into a string of length 15. The system is such that:• The first ten characters consist of the phone number of passengers.
• The next character denotes the gender of the person.
• The following two characters are used to indicate the age of the person.
• The last two characters determine the seat allotted to that person.
Return the number of passengers who are strictly more than 60 years old.
Example 1:
Input: details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
Output: 2
Explanation: The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
Example 2:
Input: details = ["1313579440F2036","2921522980M5644"]
Output: 0
Explanation: None of the passengers are older than 60.
Constraints:
•
1 <= details.length <= 100•
details[i].length == 15•
details[i] consists of digits from '0' to '9'.•
details[i][10] is either 'M' or 'F' or 'O'.• The phone numbers and seat numbers of the passengers are distinct.
2024-08-02
2134. Minimum Swaps to Group All 1's Together II
Topic: Array, Sliding Window
Difficulty: Medium
Problem:
A swap is defined as taking two distinct positions in an array and swapping the values in them.
A circular array is defined as an array where we consider the first element and the last element to be adjacent.
Given a binary circular array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2134. Minimum Swaps to Group All 1's Together II
Topic: Array, Sliding Window
Difficulty: Medium
Problem:
A swap is defined as taking two distinct positions in an array and swapping the values in them.
A circular array is defined as an array where we consider the first element and the last element to be adjacent.
Given a binary circular array
nums, return the minimum number of swaps required to group all 1's present in the array together at any location.Example 1:
Input: nums = [0,1,0,1,1,0,0]
Output: 1
Explanation: Here are a few of the ways to group all the 1's together:
[0,0,1,1,1,0,0] using 1 swap.
[0,1,1,1,0,0,0] using 1 swap.
[1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array).
There is no way to group all 1's together with 0 swaps.
Thus, the minimum number of swaps required is 1.
Example 2:
Input: nums = [0,1,1,1,0,0,1,1,0]
Output: 2
Explanation: Here are a few of the ways to group all the 1's together:
[1,1,1,0,0,0,0,1,1] using 2 swaps (using the circular property of the array).
[1,1,1,1,1,0,0,0,0] using 2 swaps.
There is no way to group all 1's together with 0 or 1 swaps.
Thus, the minimum number of swaps required is 2.
Example 3:
Input: nums = [1,1,0,0,1]
Output: 0
Explanation: All the 1's are already grouped together due to the circular property of the array.
Thus, the minimum number of swaps required is 0.
Constraints:
•
1 <= nums.length <= 10^5•
nums[i] is either 0 or 1.2024-08-03
1460. Make Two Arrays Equal by Reversing Subarrays
Topic: Array, Hash Table, Sorting
Difficulty: Easy
Problem:
You are given two integer arrays of equal length
Return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
1460. Make Two Arrays Equal by Reversing Subarrays
Topic: Array, Hash Table, Sorting
Difficulty: Easy
Problem:
You are given two integer arrays of equal length
target and arr. In one step, you can select any non-empty subarray of arr and reverse it. You are allowed to make any number of steps.Return
true if you can make arr equal to target or false otherwise.Example 1:
Input: target = [1,2,3,4], arr = [2,4,1,3]
Output: true
Explanation: You can follow the next steps to convert arr to target:
1- Reverse subarray [2,4,1], arr becomes [1,4,2,3]
2- Reverse subarray [4,2], arr becomes [1,2,4,3]
3- Reverse subarray [4,3], arr becomes [1,2,3,4]
There are multiple ways to convert arr to target, this is not the only way to do so.
Example 2:
Input: target = [7], arr = [7]
Output: true
Explanation: arr is equal to target without any reverses.
Example 3:
Input: target = [3,7,9], arr = [3,7,11]
Output: false
Explanation: arr does not have value 9 and it can never be converted to target.
Constraints:
•
target.length == arr.length•
1 <= target.length <= 1000•
1 <= target[i] <= 1000•
1 <= arr[i] <= 10002024-08-04
1508. Range Sum of Sorted Subarray Sums
Topic: Array, Two Pointers, Binary Search, Sorting
Difficulty: Medium
Problem:
You are given the array
Return the sum of the numbers from index
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
1508. Range Sum of Sorted Subarray Sums
Topic: Array, Two Pointers, Binary Search, Sorting
Difficulty: Medium
Problem:
You are given the array
nums consisting of n positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of n * (n + 1) / 2 numbers.Return the sum of the numbers from index
left to index right (indexed from 1), inclusive, in the new array. Since the answer can be a huge number return it modulo 10^9 + 7.Example 1:
Input: nums = [1,2,3,4], n = 4, left = 1, right = 5
Output: 13
Explanation: All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13.
Example 2:
Input: nums = [1,2,3,4], n = 4, left = 3, right = 4
Output: 6
Explanation: The given array is the same as example 1. We have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 3 to ri = 4 is 3 + 3 = 6.
Example 3:
Input: nums = [1,2,3,4], n = 4, left = 1, right = 10
Output: 50
Constraints:
•
n == nums.length•
1 <= nums.length <= 1000•
1 <= nums[i] <= 100•
1 <= left <= right <= n * (n + 1) / 22024-08-05
2053. Kth Distinct String in an Array
Topic: Array, Hash Table, String, Counting
Difficulty: Easy
Problem:
A distinct string is a string that is present only once in an array.
Given an array of strings
Note that the strings are considered in the order in which they appear in the array.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
2053. Kth Distinct String in an Array
Topic: Array, Hash Table, String, Counting
Difficulty: Easy
Problem:
A distinct string is a string that is present only once in an array.
Given an array of strings
arr, and an integer k, return the k^th distinct string present in arr. If there are fewer than k distinct strings, return an empty string "".Note that the strings are considered in the order in which they appear in the array.
Example 1:
Input: arr = ["d","b","c","b","c","a"], k = 2
Output: "a"
Explanation:
The only distinct strings in arr are "d" and "a".
"d" appears 1^st, so it is the 1^st distinct string.
"a" appears 2^nd, so it is the 2^nd distinct string.
Since k == 2, "a" is returned.
Example 2:
Input: arr = ["aaa","aa","a"], k = 1
Output: "aaa"
Explanation:
All strings in arr are distinct, so the 1^st string "aaa" is returned.
Example 3:
Input: arr = ["a","b","a"], k = 3
Output: ""
Explanation:
The only distinct string is "b". Since there are fewer than 3 distinct strings, we return an empty string "".
Constraints:
•
1 <= k <= arr.length <= 1000•
1 <= arr[i].length <= 5•
arr[i] consists of lowercase English letters.2024-08-06
3016. Minimum Number of Pushes to Type Word II
Topic: Hash Table, String, Greedy, Sorting, Counting
Difficulty: Medium
Problem:
You are given a string
Telephone keypads have keys mapped with distinct collections of lowercase English letters, which can be used to form words by pushing them. For example, the key
It is allowed to remap the keys numbered
Return the minimum number of pushes needed to type
An example mapping of letters to keys on a telephone keypad is given below. Note that
Image: https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png
Example 1:
Image: https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png
Constraints:
•
•
3016. Minimum Number of Pushes to Type Word II
Topic: Hash Table, String, Greedy, Sorting, Counting
Difficulty: Medium
Problem:
You are given a string
word containing lowercase English letters.Telephone keypads have keys mapped with distinct collections of lowercase English letters, which can be used to form words by pushing them. For example, the key
2 is mapped with ["a","b","c"], we need to push the key one time to type "a", two times to type "b", and three times to type "c" .It is allowed to remap the keys numbered
2 to 9 to distinct collections of letters. The keys can be remapped to any amount of letters, but each letter must be mapped to exactly one key. You need to find the minimum number of times the keys will be pushed to type the string word.Return the minimum number of pushes needed to type
word after remapping the keys.An example mapping of letters to keys on a telephone keypad is given below. Note that
1, *, #, and 0 do not map to any letters.Image: https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png
Example 1:
Image: https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png
Input: word = "abcde"
Output: 5
Explanation: The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
Total cost is 1 + 1 + 1 + 1 + 1 = 5.
It can be shown that no other mapping can provide a lower cost.
Example 2:
Image: https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png
Input: word = "xyzxyzxyzxyz"
Output: 12
Explanation: The remapped keypad given in the image provides the minimum cost.
"x" -> one push on key 2
"y" -> one push on key 3
"z" -> one push on key 4
Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12
It can be shown that no other mapping can provide a lower cost.
Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters.
Example 3:
Image: https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png
Input: word = "aabbccddeeffgghhiiiiii"
Output: 24
Explanation: The remapped keypad given in the image provides the minimum cost.
"a" -> one push on key 2
"b" -> one push on key 3
"c" -> one push on key 4
"d" -> one push on key 5
"e" -> one push on key 6
"f" -> one push on key 7
"g" -> one push on key 8
"h" -> two pushes on key 9
"i" -> one push on key 9
Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24.
It can be shown that no other mapping can provide a lower cost.
Constraints:
•
1 <= word.length <= 10^5•
word consists of lowercase English letters.2024-08-07
273. Integer to English Words
Topic: Math, String, Recursion
Difficulty: Hard
Problem:
Convert a non-negative integer
Example 1:
Example 2:
Example 3:
Constraints:
•
273. Integer to English Words
Topic: Math, String, Recursion
Difficulty: Hard
Problem:
Convert a non-negative integer
num to its English words representation.Example 1:
Input: num = 123
Output: "One Hundred Twenty Three"
Example 2:
Input: num = 12345
Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: num = 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Constraints:
•
0 <= num <= 2^31 - 12024-08-08
885. Spiral Matrix III
Topic: Array, Matrix, Simulation
Difficulty: Medium
Problem:
You start at the cell
You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid's boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all
Return an array of coordinates representing the positions of the grid in the order you visited them.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/24/example_1.png
Example 2:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/24/example_2.png
Constraints:
•
•
•
885. Spiral Matrix III
Topic: Array, Matrix, Simulation
Difficulty: Medium
Problem:
You start at the cell
(rStart, cStart) of an rows x cols grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid's boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all
rows * cols spaces of the grid.Return an array of coordinates representing the positions of the grid in the order you visited them.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/24/example_1.png
Input: rows = 1, cols = 4, rStart = 0, cStart = 0
Output: [[0,0],[0,1],[0,2],[0,3]]
Example 2:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/24/example_2.png
Input: rows = 5, cols = 6, rStart = 1, cStart = 4
Output: [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
Constraints:
•
1 <= rows, cols <= 100•
0 <= rStart < rows•
0 <= cStart < cols2024-08-09
840. Magic Squares In Grid
Topic: Array, Hash Table, Math, Matrix
Difficulty: Medium
Problem:
A
Given a
Note: while a magic square can only contain numbers from 1 to 9,
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/11/magic_main.jpg
Example 2:
Constraints:
•
•
•
•
840. Magic Squares In Grid
Topic: Array, Hash Table, Math, Matrix
Difficulty: Medium
Problem:
A
3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.Given a
row x col grid of integers, how many 3 x 3 contiguous magic square subgrids are there?Note: while a magic square can only contain numbers from 1 to 9,
grid may contain numbers up to 15.Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/11/magic_main.jpg
Input: grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]
Output: 1
Explanation:
The following subgrid is a 3 x 3 magic square:
Image: [https://assets.leetcode.com/uploads/2020/09/11/magic_valid.jpg](https://assets.leetcode.com/uploads/2020/09/11/magic_valid.jpg)
while this one is not:
Image: [https://assets.leetcode.com/uploads/2020/09/11/magic_invalid.jpg](https://assets.leetcode.com/uploads/2020/09/11/magic_invalid.jpg)
In total, there is only one magic square inside the given grid.
Example 2:
Input: grid = [[8]]
Output: 0
Constraints:
•
row == grid.length•
col == grid[i].length•
1 <= row, col <= 10•
0 <= grid[i][j] <= 152024-08-10
959. Regions Cut By Slashes
Topic: Array, Hash Table, Depth-First Search, Breadth-First Search, Union Find, Matrix
Difficulty: Medium
Problem:
An
Given the grid
Note that backslash characters are escaped, so a
Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/15/1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/15/2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2018/12/15/4.png
Constraints:
•
•
•
959. Regions Cut By Slashes
Topic: Array, Hash Table, Depth-First Search, Breadth-First Search, Union Find, Matrix
Difficulty: Medium
Problem:
An
n x n grid is composed of 1 x 1 squares where each 1 x 1 square consists of a '/', '\', or blank space ' '. These characters divide the square into contiguous regions.Given the grid
grid represented as a string array, return the number of regions.Note that backslash characters are escaped, so a
'\' is represented as '\\'.Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/15/1.png
Input: grid = [" /","/ "]
Output: 2
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/15/2.png
Input: grid = [" /"," "]
Output: 1
Example 3:
Image: https://assets.leetcode.com/uploads/2018/12/15/4.png
Input: grid = ["/\\","\\/"]
Output: 5
Explanation: Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.
Constraints:
•
n == grid.length == grid[i].length•
1 <= n <= 30•
grid[i][j] is either '/', '\', or ' '.2024-08-11
1568. Minimum Number of Days to Disconnect Island
Topic: Array, Depth-First Search, Breadth-First Search, Matrix, Strongly Connected Component
Difficulty: Hard
Problem:
You are given an
The grid is said to be connected if we have exactly one island, otherwise is said disconnected.
In one day, we are allowed to change any single land cell
Return the minimum number of days to disconnect the grid.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/24/land1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/12/24/land2.jpg
Constraints:
•
•
•
•
1568. Minimum Number of Days to Disconnect Island
Topic: Array, Depth-First Search, Breadth-First Search, Matrix, Strongly Connected Component
Difficulty: Hard
Problem:
You are given an
m x n binary grid grid where 1 represents land and 0 represents water. An island is a maximal 4-directionally (horizontal or vertical) connected group of 1's.The grid is said to be connected if we have exactly one island, otherwise is said disconnected.
In one day, we are allowed to change any single land cell
(1) into a water cell (0).Return the minimum number of days to disconnect the grid.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/12/24/land1.jpg
Input: grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]]
Output: 2
Explanation: We need at least 2 days to get a disconnected grid.
Change land grid[1][1] and grid[0][2] to water and get 2 disconnected island.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/12/24/land2.jpg
Input: grid = [[1,1]]
Output: 2
Explanation: Grid of full water is also disconnected ([[1,1]] -> [[0,0]]), 0 islands.
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m, n <= 30•
grid[i][j] is either 0 or 1.2024-08-12
703. Kth Largest Element in a Stream
Topic: Tree, Design, Binary Search Tree, Heap (Priority Queue), Binary Tree, Data Stream
Difficulty: Easy
Problem:
Design a class to find the
Implement
•
•
Example 1:
Constraints:
•
•
•
•
• At most
• It is guaranteed that there will be at least
703. Kth Largest Element in a Stream
Topic: Tree, Design, Binary Search Tree, Heap (Priority Queue), Binary Tree, Data Stream
Difficulty: Easy
Problem:
Design a class to find the
k^th largest element in a stream. Note that it is the k^th largest element in the sorted order, not the k^th distinct element.Implement
KthLargest class:•
KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums.•
int add(int val) Appends the integer val to the stream and returns the element representing the k^th largest element in the stream.Example 1:
Input
["KthLargest", "add", "add", "add", "add", "add"]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
Output
[null, 4, 5, 5, 8, 8]
Explanation
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // return 4
kthLargest.add(5); // return 5
kthLargest.add(10); // return 5
kthLargest.add(9); // return 8
kthLargest.add(4); // return 8
Constraints:
•
1 <= k <= 10^4•
0 <= nums.length <= 10^4•
-10^4 <= nums[i] <= 10^4•
-10^4 <= val <= 10^4• At most
10^4 calls will be made to add.• It is guaranteed that there will be at least
k elements in the array when you search for the k^th element.2024-08-13
40. Combination Sum II
Topic: Array, Backtracking
Difficulty: Medium
Problem:
Given a collection of candidate numbers (
Each number in
Note: The solution set must not contain duplicate combinations.
Example 1:
Example 2:
Constraints:
•
•
•
40. Combination Sum II
Topic: Array, Backtracking
Difficulty: Medium
Problem:
Given a collection of candidate numbers (
candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.Each number in
candidates may only be used once in the combination.Note: The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [10,1,2,7,6,1,5], target = 8
Output:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
Example 2:
Input: candidates = [2,5,2,1,2], target = 5
Output:
[
[1,2,2],
[5]
]
Constraints:
•
1 <= candidates.length <= 100•
1 <= candidates[i] <= 50•
1 <= target <= 302024-08-14
719. Find K-th Smallest Pair Distance
Topic: Array, Two Pointers, Binary Search, Sorting
Difficulty: Hard
Problem:
The distance of a pair of integers
Given an integer array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
719. Find K-th Smallest Pair Distance
Topic: Array, Two Pointers, Binary Search, Sorting
Difficulty: Hard
Problem:
The distance of a pair of integers
a and b is defined as the absolute difference between a and b.Given an integer array
nums and an integer k, return the k^th smallest distance among all the pairs nums[i] and nums[j] where 0 <= i < j < nums.length.Example 1:
Input: nums = [1,3,1], k = 1
Output: 0
Explanation: Here are all the pairs:
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
Then the 1^st smallest distance pair is (1,1), and its distance is 0.
Example 2:
Input: nums = [1,1,1], k = 2
Output: 0
Example 3:
Input: nums = [1,6,1], k = 3
Output: 5
Constraints:
•
n == nums.length•
2 <= n <= 10^4•
0 <= nums[i] <= 10^6•
1 <= k <= n * (n - 1) / 22024-08-15
860. Lemonade Change
Topic: Array, Greedy
Difficulty: Easy
Problem:
At a lemonade stand, each lemonade costs
Note that you do not have any change in hand at first.
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
860. Lemonade Change
Topic: Array, Greedy
Difficulty: Easy
Problem:
At a lemonade stand, each lemonade costs
$5. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5.Note that you do not have any change in hand at first.
Given an integer array
bills where bills[i] is the bill the i^th customer pays, return true if you can provide every customer with the correct change, or false otherwise.Example 1:
Input: bills = [5,5,5,10,20]
Output: true
Explanation:
From the first 3 customers, we collect three $5 bills in order.
From the fourth customer, we collect a $10 bill and give back a $5.
From the fifth customer, we give a $10 bill and a $5 bill.
Since all customers got correct change, we output true.
Example 2:
Input: bills = [5,5,10,10,20]
Output: false
Explanation:
From the first two customers in order, we collect two $5 bills.
For the next two customers in order, we collect a $10 bill and give back a $5 bill.
For the last customer, we can not give the change of $15 back because we only have two $10 bills.
Since not every customer received the correct change, the answer is false.
Constraints:
•
1 <= bills.length <= 10^5•
bills[i] is either 5, 10, or 20.2024-08-17
1937. Maximum Number of Points with Cost
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are given an
To gain points, you must pick one cell in each row. Picking the cell at coordinates
However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows
Return the maximum number of points you can achieve.
•
•
Example 1:
Image: https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png
Constraints:
•
•
•
•
•
1937. Maximum Number of Points with Cost
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You are given an
m x n integer matrix points (0-indexed). Starting with 0 points, you want to maximize the number of points you can get from the matrix.To gain points, you must pick one cell in each row. Picking the cell at coordinates
(r, c) will add points[r][c] to your score.However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows
r and r + 1 (where 0 <= r < m - 1), picking cells at coordinates (r, c_1) and (r + 1, c_2) will subtract abs(c_1 - c_2) from your score.Return the maximum number of points you can achieve.
abs(x) is defined as:•
x for x >= 0.•
-x for x < 0.Example 1:
Image: https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png
Input: points = [[1,2,3],[1,5,1],[3,1,1]]
Output: 9
Explanation:
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png
Input: points = [[1,5],[2,3],[4,2]]
Output: 11
Explanation:
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
Constraints:
•
m == points.length•
n == points[r].length•
1 <= m, n <= 10^5•
1 <= m * n <= 10^5•
0 <= points[r][c] <= 10^52024-08-18
264. Ugly Number II
Topic: Hash Table, Math, Dynamic Programming, Heap (Priority Queue)
Difficulty: Medium
Problem:
An ugly number is a positive integer whose prime factors are limited to
Given an integer
Example 1:
Example 2:
Constraints:
•
264. Ugly Number II
Topic: Hash Table, Math, Dynamic Programming, Heap (Priority Queue)
Difficulty: Medium
Problem:
An ugly number is a positive integer whose prime factors are limited to
2, 3, and 5.Given an integer
n, return the n^th ugly number.Example 1:
Input: n = 10
Output: 12
Explanation: [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
Example 2:
Input: n = 1
Output: 1
Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
Constraints:
•
1 <= n <= 16902024-08-19
650. 2 Keys Keyboard
Topic: Math, Dynamic Programming
Difficulty: Medium
Problem:
There is only one character
• Copy All: You can copy all the characters present on the screen (a partial copy is not allowed).
• Paste: You can paste the characters which are copied last time.
Given an integer
Example 1:
Example 2:
Constraints:
•
650. 2 Keys Keyboard
Topic: Math, Dynamic Programming
Difficulty: Medium
Problem:
There is only one character
'A' on the screen of a notepad. You can perform one of two operations on this notepad for each step:• Copy All: You can copy all the characters present on the screen (a partial copy is not allowed).
• Paste: You can paste the characters which are copied last time.
Given an integer
n, return the minimum number of operations to get the character 'A' exactly n times on the screen.Example 1:
Input: n = 3
Output: 3
Explanation: Initially, we have one character 'A'.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get 'AA'.
In step 3, we use Paste operation to get 'AAA'.
Example 2:
Input: n = 1
Output: 0
Constraints:
•
1 <= n <= 10002024-08-20
1140. Stone Game II
Topic: Array, Math, Dynamic Programming, Prefix Sum, Game Theory
Difficulty: Medium
Problem:
Alice and Bob continue their games with piles of stones. There are a number of piles arranged in a row, and each pile has a positive integer number of stones
Alice and Bob take turns, with Alice starting first. Initially,
On each player's turn, that player can take all the stones in the first
The game continues until all the stones have been taken.
Assuming Alice and Bob play optimally, return the maximum number of stones Alice can get.
Example 1:
Example 2:
Constraints:
•
•
1140. Stone Game II
Topic: Array, Math, Dynamic Programming, Prefix Sum, Game Theory
Difficulty: Medium
Problem:
Alice and Bob continue their games with piles of stones. There are a number of piles arranged in a row, and each pile has a positive integer number of stones
piles[i]. The objective of the game is to end with the most stones. Alice and Bob take turns, with Alice starting first. Initially,
M = 1.On each player's turn, that player can take all the stones in the first
X remaining piles, where 1 <= X <= 2M. Then, we set M = max(M, X).The game continues until all the stones have been taken.
Assuming Alice and Bob play optimally, return the maximum number of stones Alice can get.
Example 1:
Input: piles = [2,7,9,4,4]
Output: 10
Explanation: If Alice takes one pile at the beginning, Bob takes two piles, then Alice takes 2 piles again. Alice can get 2 + 4 + 4 = 10 piles in total. If Alice takes two piles at the beginning, then Bob can take all three piles left. In this case, Alice get 2 + 7 = 9 piles in total. So we return 10 since it's larger.
Example 2:
Input: piles = [1,2,3,4,5,100]
Output: 104
Constraints:
•
1 <= piles.length <= 100•
1 <= piles[i] <= 10^42024-08-21
664. Strange Printer
Topic: String, Dynamic Programming
Difficulty: Hard
Problem:
There is a strange printer with the following two special properties:
• The printer can only print a sequence of the same character each time.
• At each turn, the printer can print new characters starting from and ending at any place and will cover the original existing characters.
Given a string
Example 1:
Example 2:
Constraints:
•
•
664. Strange Printer
Topic: String, Dynamic Programming
Difficulty: Hard
Problem:
There is a strange printer with the following two special properties:
• The printer can only print a sequence of the same character each time.
• At each turn, the printer can print new characters starting from and ending at any place and will cover the original existing characters.
Given a string
s, return the minimum number of turns the printer needed to print it.Example 1:
Input: s = "aaabbb"
Output: 2
Explanation: Print "aaa" first and then print "bbb".
Example 2:
Input: s = "aba"
Output: 2
Explanation: Print "aaa" first and then print "b" from the second place of the string, which will cover the existing character 'a'.
Constraints:
•
1 <= s.length <= 100•
s consists of lowercase English letters.