2024-06-07
648. Replace Words
Topic: Array, Hash Table, String, Trie
Difficulty: Medium
Problem:
In English, we have a concept called root, which can be followed by some other word to form another longer word - let's call this word derivative. For example, when the root
Given a
Return the
Example 1:
Example 2:
Constraints:
•
•
•
•
•
• The number of words in
• The length of each word in
• Every two consecutive words in
•
648. Replace Words
Topic: Array, Hash Table, String, Trie
Difficulty: Medium
Problem:
In English, we have a concept called root, which can be followed by some other word to form another longer word - let's call this word derivative. For example, when the root
"help" is followed by the word "ful", we can form a derivative "helpful".Given a
dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the derivatives in the sentence with the root forming it. If a derivative can be replaced by more than one root, replace it with the root that has the shortest length.Return the
sentence after the replacement.Example 1:
Input: dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
Output: "the cat was rat by the bat"
Example 2:
Input: dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
Output: "a a b c"
Constraints:
•
1 <= dictionary.length <= 1000•
1 <= dictionary[i].length <= 100•
dictionary[i] consists of only lower-case letters.•
1 <= sentence.length <= 10^6•
sentence consists of only lower-case letters and spaces.• The number of words in
sentence is in the range [1, 1000]• The length of each word in
sentence is in the range [1, 1000]• Every two consecutive words in
sentence will be separated by exactly one space.•
sentence does not have leading or trailing spaces.2024-06-08
523. Continuous Subarray Sum
Topic: Array, Hash Table, Math, Prefix Sum
Difficulty: Medium
Problem:
Given an integer array nums and an integer k, return
A good subarray is a subarray where:
• its length is at least two, and
• the sum of the elements of the subarray is a multiple of
Note that:
• A subarray is a contiguous part of the array.
• An integer
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
523. Continuous Subarray Sum
Topic: Array, Hash Table, Math, Prefix Sum
Difficulty: Medium
Problem:
Given an integer array nums and an integer k, return
true if nums has a good subarray or false otherwise.A good subarray is a subarray where:
• its length is at least two, and
• the sum of the elements of the subarray is a multiple of
k.Note that:
• A subarray is a contiguous part of the array.
• An integer
x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k.Example 1:
Input: nums = [23,2,4,6,7], k = 6
Output: true
Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
Example 2:
Input: nums = [23,2,6,4,7], k = 6
Output: true
Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
Example 3:
Input: nums = [23,2,6,4,7], k = 13
Output: false
Constraints:
•
1 <= nums.length <= 10^5•
0 <= nums[i] <= 10^9•
0 <= sum(nums[i]) <= 2^31 - 1•
1 <= k <= 2^31 - 12024-06-09
974. Subarray Sums Divisible by K
Topic: Array, Hash Table, Prefix Sum
Difficulty: Medium
Problem:
Given an integer array
A subarray is a contiguous part of an array.
Example 1:
Example 2:
Constraints:
•
•
•
974. Subarray Sums Divisible by K
Topic: Array, Hash Table, Prefix Sum
Difficulty: Medium
Problem:
Given an integer array
nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k.A subarray is a contiguous part of an array.
Example 1:
Input: nums = [4,5,0,-2,-3,1], k = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by k = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Example 2:
Input: nums = [5], k = 9
Output: 0
Constraints:
•
1 <= nums.length <= 3 * 10^4•
-10^4 <= nums[i] <= 10^4•
2 <= k <= 10^42024-06-10
1051. Height Checker
Topic: Array, Sorting, Counting Sort
Difficulty: Easy
Problem:
A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array
You are given an integer array
Return the number of indices where
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1051. Height Checker
Topic: Array, Sorting, Counting Sort
Difficulty: Easy
Problem:
A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array
expected where expected[i] is the expected height of the i^th student in line.You are given an integer array
heights representing the current order that the students are standing in. Each heights[i] is the height of the i^th student in line (0-indexed).Return the number of indices where
heights[i] != expected[i].Example 1:
Input: heights = [1,1,4,2,1,3]
Output: 3
Explanation:
heights: [1,1,4,2,1,3]
expected: [1,1,1,2,3,4]
Indices 2, 4, and 5 do not match.
Example 2:
Input: heights = [5,1,2,3,4]
Output: 5
Explanation:
heights: [5,1,2,3,4]
expected: [1,2,3,4,5]
All indices do not match.
Example 3:
Input: heights = [1,2,3,4,5]
Output: 0
Explanation:
heights: [1,2,3,4,5]
expected: [1,2,3,4,5]
All indices match.
Constraints:
•
1 <= heights.length <= 100•
1 <= heights[i] <= 1002024-06-11
1122. Relative Sort Array
Topic: Array, Hash Table, Sorting, Counting Sort
Difficulty: Easy
Problem:
Given two arrays
Sort the elements of
Example 1:
Example 2:
Constraints:
•
•
• All the elements of
• Each
1122. Relative Sort Array
Topic: Array, Hash Table, Sorting, Counting Sort
Difficulty: Easy
Problem:
Given two arrays
arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.Sort the elements of
arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that do not appear in arr2 should be placed at the end of arr1 in ascending order.Example 1:
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
Output: [2,2,2,1,4,3,3,9,6,7,19]
Example 2:
Input: arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6]
Output: [22,28,8,6,17,44]
Constraints:
•
1 <= arr1.length, arr2.length <= 1000•
0 <= arr1[i], arr2[i] <= 1000• All the elements of
arr2 are distinct.• Each
arr2[i] is in arr1.2024-06-12
75. Sort Colors
Topic: Array, Two Pointers, Sorting
Difficulty: Medium
Problem:
Given an array
We will use the integers
You must solve this problem without using the library's sort function.
Example 1:
Example 2:
Constraints:
•
•
•
Follow up: Could you come up with a one-pass algorithm using only constant extra space?
75. Sort Colors
Topic: Array, Two Pointers, Sorting
Difficulty: Medium
Problem:
Given an array
nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.We will use the integers
0, 1, and 2 to represent the color red, white, and blue, respectively.You must solve this problem without using the library's sort function.
Example 1:
Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Example 2:
Input: nums = [2,0,1]
Output: [0,1,2]
Constraints:
•
n == nums.length•
1 <= n <= 300•
nums[i] is either 0, 1, or 2.Follow up: Could you come up with a one-pass algorithm using only constant extra space?
2024-06-13
2037. Minimum Number of Moves to Seat Everyone
Topic: Array, Greedy, Sorting
Difficulty: Easy
Problem:
There are
You may perform the following move any number of times:
• Increase or decrease the position of the
Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat.
Note that there may be multiple seats or students in the same position at the beginning.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
2037. Minimum Number of Moves to Seat Everyone
Topic: Array, Greedy, Sorting
Difficulty: Easy
Problem:
There are
n seats and n students in a room. You are given an array seats of length n, where seats[i] is the position of the i^th seat. You are also given the array students of length n, where students[j] is the position of the j^th student.You may perform the following move any number of times:
• Increase or decrease the position of the
i^th student by 1 (i.e., moving the i^th student from position x to x + 1 or x - 1)Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat.
Note that there may be multiple seats or students in the same position at the beginning.
Example 1:
Input: seats = [3,1,5], students = [2,7,4]
Output: 4
Explanation: The students are moved as follows:
- The first student is moved from from position 2 to position 1 using 1 move.
- The second student is moved from from position 7 to position 5 using 2 moves.
- The third student is moved from from position 4 to position 3 using 1 move.
In total, 1 + 2 + 1 = 4 moves were used.
Example 2:
Input: seats = [4,1,5,9], students = [1,3,2,6]
Output: 7
Explanation: The students are moved as follows:
- The first student is not moved.
- The second student is moved from from position 3 to position 4 using 1 move.
- The third student is moved from from position 2 to position 5 using 3 moves.
- The fourth student is moved from from position 6 to position 9 using 3 moves.
In total, 0 + 1 + 3 + 3 = 7 moves were used.
Example 3:
Input: seats = [2,2,6,6], students = [1,3,2,6]
Output: 4
Explanation: Note that there are two seats at position 2 and two seats at position 6.
The students are moved as follows:
- The first student is moved from from position 1 to position 2 using 1 move.
- The second student is moved from from position 3 to position 6 using 3 moves.
- The third student is not moved.
- The fourth student is not moved.
In total, 1 + 3 + 0 + 0 = 4 moves were used.
Constraints:
•
n == seats.length == students.length•
1 <= n <= 100•
1 <= seats[i], students[j] <= 1002024-06-14
945. Minimum Increment to Make Array Unique
Topic: Array, Greedy, Sorting, Counting
Difficulty: Medium
Problem:
You are given an integer array
Return the minimum number of moves to make every value in
The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Example 2:
Constraints:
•
•
945. Minimum Increment to Make Array Unique
Topic: Array, Greedy, Sorting, Counting
Difficulty: Medium
Problem:
You are given an integer array
nums. In one move, you can pick an index i where 0 <= i < nums.length and increment nums[i] by 1.Return the minimum number of moves to make every value in
nums unique.The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Input: nums = [1,2,2]
Output: 1
Explanation: After 1 move, the array could be [1, 2, 3].
Example 2:
Input: nums = [3,2,1,2,1,7]
Output: 6
Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7].
It can be shown with 5 or less moves that it is impossible for the array to have all unique values.
Constraints:
•
1 <= nums.length <= 10^5•
0 <= nums[i] <= 10^52024-06-15
502. IPO
Topic: Array, Greedy, Sorting, Heap (Priority Queue)
Difficulty: Hard
Problem:
Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the IPO. Since it has limited resources, it can only finish at most
You are given
Initially, you have
Pick a list of at most
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Example 2:
Constraints:
•
•
•
•
•
•
•
502. IPO
Topic: Array, Greedy, Sorting, Heap (Priority Queue)
Difficulty: Hard
Problem:
Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the IPO. Since it has limited resources, it can only finish at most
k distinct projects before the IPO. Help LeetCode design the best way to maximize its total capital after finishing at most k distinct projects.You are given
n projects where the i^th project has a pure profit profits[i] and a minimum capital of capital[i] is needed to start it.Initially, you have
w capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital.Pick a list of at most
k distinct projects from given projects to maximize your final capital, and return the final maximized capital.The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
Output: 4
Explanation: Since your initial capital is 0, you can only start the project indexed 0.
After finishing it you will obtain profit 1 and your capital becomes 1.
With capital 1, you can either start the project indexed 1 or the project indexed 2.
Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital.
Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4.
Example 2:
Input: k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
Output: 6
Constraints:
•
1 <= k <= 10^5•
0 <= w <= 10^9•
n == profits.length•
n == capital.length•
1 <= n <= 10^5•
0 <= profits[i] <= 10^4•
0 <= capital[i] <= 10^92024-06-16
330. Patching Array
Topic: Array, Greedy
Difficulty: Hard
Problem:
Given a sorted integer array
Return the minimum number of patches required.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
330. Patching Array
Topic: Array, Greedy
Difficulty: Hard
Problem:
Given a sorted integer array
nums and an integer n, add/patch elements to the array such that any number in the range [1, n] inclusive can be formed by the sum of some elements in the array.Return the minimum number of patches required.
Example 1:
Input: nums = [1,3], n = 6
Output: 1
Explanation:
Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.
Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3].
Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6].
So we only need 1 patch.
Example 2:
Input: nums = [1,5,10], n = 20
Output: 2
Explanation: The two patches can be [2, 4].
Example 3:
Input: nums = [1,2,2], n = 5
Output: 0
Constraints:
•
1 <= nums.length <= 1000•
1 <= nums[i] <= 10^4•
nums is sorted in ascending order.•
1 <= n <= 2^31 - 12024-06-17
633. Sum of Square Numbers
Topic: Math, Two Pointers, Binary Search
Difficulty: Medium
Problem:
Given a non-negative integer
Example 1:
Example 2:
Constraints:
•
633. Sum of Square Numbers
Topic: Math, Two Pointers, Binary Search
Difficulty: Medium
Problem:
Given a non-negative integer
c, decide whether there're two integers a and b such that a^2 + b^2 = c.Example 1:
Input: c = 5
Output: true
Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: c = 3
Output: false
Constraints:
•
0 <= c <= 2^31 - 12024-06-18
826. Most Profit Assigning Work
Topic: Array, Two Pointers, Binary Search, Greedy, Sorting
Difficulty: Medium
Problem:
You have
•
•
Every worker can be assigned at most one job, but one job can be completed multiple times.
• For example, if three workers attempt the same job that pays
Return the maximum profit we can achieve after assigning the workers to the jobs.
Example 1:
Example 2:
Constraints:
•
•
•
•
•
826. Most Profit Assigning Work
Topic: Array, Two Pointers, Binary Search, Greedy, Sorting
Difficulty: Medium
Problem:
You have
n jobs and m workers. You are given three arrays: difficulty, profit, and worker where:•
difficulty[i] and profit[i] are the difficulty and the profit of the i^th job, and•
worker[j] is the ability of j^th worker (i.e., the j^th worker can only complete a job with difficulty at most worker[j]).Every worker can be assigned at most one job, but one job can be completed multiple times.
• For example, if three workers attempt the same job that pays
$1, then the total profit will be $3. If a worker cannot complete any job, their profit is $0.Return the maximum profit we can achieve after assigning the workers to the jobs.
Example 1:
Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
Output: 100
Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get a profit of [20,20,30,30] separately.
Example 2:
Input: difficulty = [85,47,57], profit = [24,66,99], worker = [40,25,25]
Output: 0
Constraints:
•
n == difficulty.length•
n == profit.length•
m == worker.length•
1 <= n, m <= 10^4•
1 <= difficulty[i], profit[i], worker[i] <= 10^52024-06-19
1482. Minimum Number of Days to Make m Bouquets
Topic: Array, Binary Search
Difficulty: Medium
Problem:
You are given an integer array
You want to make
The garden consists of
Return the minimum number of days you need to wait to be able to make
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
1482. Minimum Number of Days to Make m Bouquets
Topic: Array, Binary Search
Difficulty: Medium
Problem:
You are given an integer array
bloomDay, an integer m and an integer k.You want to make
m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.The garden consists of
n flowers, the i^th flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.Return the minimum number of days you need to wait to be able to make
m bouquets from the garden. If it is impossible to make m bouquets return -1.Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here is the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Constraints:
•
bloomDay.length == n•
1 <= n <= 10^5•
1 <= bloomDay[i] <= 10^9•
1 <= m <= 10^6•
1 <= k <= n2024-06-20
1552. Magnetic Force Between Two Balls
Topic: Array, Binary Search, Sorting
Difficulty: Medium
Problem:
In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has
Rick stated that magnetic force between two different balls at positions
Given the integer array
Example 1:
Image: https://assets.leetcode.com/uploads/2020/08/11/q3v1.jpg
Example 2:
Constraints:
•
•
•
• All integers in
•
1552. Magnetic Force Between Two Balls
Topic: Array, Binary Search, Sorting
Difficulty: Medium
Problem:
In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has
n empty baskets, the i^th basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.Rick stated that magnetic force between two different balls at positions
x and y is |x - y|.Given the integer array
position and the integer m. Return the required force.Example 1:
Image: https://assets.leetcode.com/uploads/2020/08/11/q3v1.jpg
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.
Constraints:
•
n == position.length•
2 <= n <= 10^5•
1 <= position[i] <= 10^9• All integers in
position are distinct.•
2 <= m <= position.length2024-06-21
1052. Grumpy Bookstore Owner
Topic: Array, Sliding Window
Difficulty: Medium
Problem:
There is a bookstore owner that has a store open for
On some minutes, the bookstore owner is grumpy. You are given a binary array grumpy where
When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise, they are satisfied.
The bookstore owner knows a secret technique to keep themselves not grumpy for
Return the maximum number of customers that can be satisfied throughout the day.
Example 1:
Example 2:
Constraints:
•
•
•
•
1052. Grumpy Bookstore Owner
Topic: Array, Sliding Window
Difficulty: Medium
Problem:
There is a bookstore owner that has a store open for
n minutes. Every minute, some number of customers enter the store. You are given an integer array customers of length n where customers[i] is the number of the customer that enters the store at the start of the i^th minute and all those customers leave after the end of that minute.On some minutes, the bookstore owner is grumpy. You are given a binary array grumpy where
grumpy[i] is 1 if the bookstore owner is grumpy during the i^th minute, and is 0 otherwise.When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise, they are satisfied.
The bookstore owner knows a secret technique to keep themselves not grumpy for
minutes consecutive minutes, but can only use it once.Return the maximum number of customers that can be satisfied throughout the day.
Example 1:
Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3
Output: 16
Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes.
The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.
Example 2:
Input: customers = [1], grumpy = [0], minutes = 1
Output: 1
Constraints:
•
n == customers.length == grumpy.length•
1 <= minutes <= n <= 2 * 10^4•
0 <= customers[i] <= 1000•
grumpy[i] is either 0 or 1.2024-06-22
1248. Count Number of Nice Subarrays
Topic: Array, Hash Table, Math, Sliding Window
Difficulty: Medium
Problem:
Given an array of integers
Return the number of nice sub-arrays.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1248. Count Number of Nice Subarrays
Topic: Array, Hash Table, Math, Sliding Window
Difficulty: Medium
Problem:
Given an array of integers
nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There are no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
•
1 <= nums.length <= 50000•
1 <= nums[i] <= 10^5•
1 <= k <= nums.length2024-06-23
1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
Topic: Array, Queue, Sliding Window, Heap (Priority Queue), Ordered Set, Monotonic Queue
Difficulty: Medium
Problem:
Given an array of integers
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
Topic: Array, Queue, Sliding Window, Heap (Priority Queue), Ordered Set, Monotonic Queue
Difficulty: Medium
Problem:
Given an array of integers
nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.Example 1:
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.
Example 2:
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
Example 3:
Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^9•
0 <= limit <= 10^92024-06-24
995. Minimum Number of K Consecutive Bit Flips
Topic: Array, Bit Manipulation, Queue, Sliding Window, Prefix Sum
Difficulty: Hard
Problem:
You are given a binary array
A k-bit flip is choosing a subarray of length
Return the minimum number of k-bit flips required so that there is no
A subarray is a contiguous part of an array.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
995. Minimum Number of K Consecutive Bit Flips
Topic: Array, Bit Manipulation, Queue, Sliding Window, Prefix Sum
Difficulty: Hard
Problem:
You are given a binary array
nums and an integer k.A k-bit flip is choosing a subarray of length
k from nums and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0.Return the minimum number of k-bit flips required so that there is no
0 in the array. If it is not possible, return -1.A subarray is a contiguous part of an array.
Example 1:
Input: nums = [0,1,0], k = 1
Output: 2
Explanation: Flip nums[0], then flip nums[2].
Example 2:
Input: nums = [1,1,0], k = 2
Output: -1
Explanation: No matter how we flip subarrays of size 2, we cannot make the array become [1,1,1].
Example 3:
Input: nums = [0,0,0,1,0,1,1,0], k = 3
Output: 3
Explanation:
Flip nums[0],nums[1],nums[2]: nums becomes [1,1,1,1,0,1,1,0]
Flip nums[4],nums[5],nums[6]: nums becomes [1,1,1,1,1,0,0,0]
Flip nums[5],nums[6],nums[7]: nums becomes [1,1,1,1,1,1,1,1]
Constraints:
•
1 <= nums.length <= 10^5•
1 <= k <= nums.length2024-06-25
1038. Binary Search Tree to Greater Sum Tree
Topic: Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Medium
Problem:
Given the
As a reminder, a binary search tree is a tree that satisfies these constraints:
• The left subtree of a node contains only nodes with keys less than the node's key.
• The right subtree of a node contains only nodes with keys greater than the node's key.
• Both the left and right subtrees must also be binary search trees.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/05/02/tree.png
Example 2:
Constraints:
• The number of nodes in the tree is in the range
•
• All the values in the tree are unique.
Note: This question is the same as 538: <https://leetcode.com/problems/convert-bst-to-greater-tree/>
1038. Binary Search Tree to Greater Sum Tree
Topic: Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.As a reminder, a binary search tree is a tree that satisfies these constraints:
• The left subtree of a node contains only nodes with keys less than the node's key.
• The right subtree of a node contains only nodes with keys greater than the node's key.
• Both the left and right subtrees must also be binary search trees.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/05/02/tree.png
Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
Example 2:
Input: root = [0,null,1]
Output: [1,null,1]
Constraints:
• The number of nodes in the tree is in the range
[1, 100].•
0 <= Node.val <= 100• All the values in the tree are unique.
Note: This question is the same as 538: <https://leetcode.com/problems/convert-bst-to-greater-tree/>
2024-06-26
1382. Balance a Binary Search Tree
Topic: Divide and Conquer, Greedy, Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Medium
Problem:
Given the
A binary search tree is balanced if the depth of the two subtrees of every node never differs by more than
Example 1:
Image: https://assets.leetcode.com/uploads/2021/08/10/balance1-tree.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/08/10/balanced2-tree.jpg
Constraints:
• The number of nodes in the tree is in the range
•
1382. Balance a Binary Search Tree
Topic: Divide and Conquer, Greedy, Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary search tree, return a balanced binary search tree with the same node values. If there is more than one answer, return any of them.A binary search tree is balanced if the depth of the two subtrees of every node never differs by more than
1.Example 1:
Image: https://assets.leetcode.com/uploads/2021/08/10/balance1-tree.jpg
Input: root = [1,null,2,null,3,null,4,null,null]
Output: [2,1,3,null,null,null,4]
Explanation: This is not the only correct answer, [3,1,4,null,2] is also correct.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/08/10/balanced2-tree.jpg
Input: root = [2,1,3]
Output: [2,1,3]
Constraints:
• The number of nodes in the tree is in the range
[1, 10^4].•
1 <= Node.val <= 10^5