330. Patching Array
Difficulty: Hard
Topics: Array, Greedy
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 <= 10001 <= nums[i] <= 104-
numsis sorted in ascending order. 1 <= n <= 231 - 1- Only one valid answer exists.
Solution:
We need to ensure that every number in the range [1, n] can be formed by the sum of some elements in the given array. We can use a greedy algorithm to determine the minimum number of patches required.
Solution Explanation:
-
Key Insight:
- Start with the smallest missing number
misswhich starts at 1. - Iterate through the array
nums. If the current number innumsis less than or equal tomiss, thenmisscan be covered by adding this number. Otherwise, we need to patchmissto the array, effectively doubling the range that can be covered.
- Start with the smallest missing number
-
Algorithm:
- Initialize
miss = 1andpatches = 0. - Loop through the array while
miss <= n:- If the current number in
numscan covermiss, move to the next number innums. - If the current number is greater than
miss, patchmissand double the range covered by settingmiss = miss * 2.
- If the current number in
- Continue until the entire range
[1, n]is covered.
- Initialize
-
Complexity:
- The time complexity is O(m + log n), where
mis the size of the arraynums.
- The time complexity is O(m + log n), where
Let's implement this solution in PHP: 330. Patching Array
<?php
/**
* @param String $n
* @return String
*/
function minPatches($nums, $n) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example usage:
// Example 1
$nums1 = [1, 3];
$n1 = 6;
echo minPatches($nums1, $n1); // Output: 1
// Example 2
$nums2 = [1, 5, 10];
$n2 = 20;
echo minPatches($nums2, $n2); // Output: 2
// Example 3
$nums3 = [1, 2, 2];
$n3 = 5;
echo minPatches($nums3, $n3); // Output: 0
?>
Explanation:
-
Initial Setup: We initialize
missas 1, meaning we want to ensure we can generate the number 1. -
While Loop: We loop until
missexceedsn(the maximum number we need to cover). In each iteration:- If the current number in
numscan help covermiss, we add it tomissand move to the next number. - If not, we "patch" the array by effectively adding
missto it (without changing the array itself) and then updatemissto cover the new range.
- If the current number in
- Output: The total number of patches required is returned.
This algorithm ensures that we cover the entire range [1, n] with the minimum number of patches.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)