Skip to content

Commit e95696b

Browse files
authored
Merge pull request #1948 from Saipradyumnagoud/main
Added 330-patching-array
2 parents 55a9358 + 548f5e6 commit e95696b

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
id: patching-array
3+
title: Patching Array
4+
level: hard
5+
sidebar_label: Patching Array
6+
tags:
7+
- Greedy
8+
- Array
9+
- Binary Search
10+
- Java
11+
description: "This document provides solutions for the Patching Array problem."
12+
---
13+
14+
## Problem Statement
15+
16+
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.
17+
18+
Return the minimum number of patches required.
19+
20+
**Example:**
21+
22+
Example 1:
23+
24+
Input: `nums = [1,3]`, `n = 6`
25+
26+
Output: `1`
27+
28+
Explanation:
29+
30+
Combinations of nums are `[1]`, `[3]`, `[1,3]`, which form possible sums of: `1, 3, 4`.
31+
Now if we add/patch `2` to nums, the combinations are: `[1]`, `[2]`, `[3]`, `[1,3]`, `[2,3]`, `[1,2,3]`.
32+
Possible sums are `1, 2, 3, 4, 5, 6`, which now covers the range `[1, 6]`.
33+
So we only need `1` patch.
34+
35+
Example 2:
36+
37+
Input: `nums = [1,5,10]`, `n = 20`
38+
39+
Output: `2`
40+
41+
Explanation:
42+
43+
The two patches can be `[2, 4]`.
44+
45+
Example 3:
46+
47+
Input: `nums = [1,2,2]`, `n = 5`
48+
49+
Output: `0`
50+
51+
Explanation:
52+
53+
In this case, the array `nums` already covers all sums up to `5`, so no patches are needed.
54+
55+
**Constraints:**
56+
57+
- `1 <= nums.length <= 1000`
58+
- `1 <= nums[i] <= 10^4`
59+
- `nums` is sorted in ascending order.
60+
- `1 <= n <= 2 * 10^9 - 1`
61+
62+
## Solutions
63+
64+
### Approach
65+
66+
The problem can be efficiently solved using a greedy approach:
67+
68+
1. **Initialization:**
69+
- Initialize `miss` to `1`, which represents the smallest number that cannot be formed with the current elements in `nums`.
70+
- Initialize `result` to `0` to count the number of patches needed.
71+
- Initialize `i` to `0` to iterate through `nums`.
72+
73+
2. **Iterate Until Covering `n`:**
74+
- While `miss` is less than or equal to `n`, check if the current number `miss` can be formed by adding elements from `nums`.
75+
- If `nums[i]` is less than or equal to `miss`, add `nums[i]` to `miss` and move to the next element (`i++`).
76+
- If `nums[i]` is greater than `miss` or `i` exceeds the length of `nums`, it means `miss` cannot be formed with the current elements. Therefore, add `miss` itself to `nums` to extend the range of possible sums and increment `result`.
77+
78+
3. **Return Result:**
79+
- Once the loop ends and `miss` is greater than `n`, return `result`, which is the minimum number of patches required to cover all sums up to `n`.
80+
81+
### Java
82+
83+
```java
84+
class Solution {
85+
public int minPatches(int[] nums, int n) {
86+
long miss = 1;
87+
int result = 0;
88+
int i = 0;
89+
90+
while (miss <= n) {
91+
if (i < nums.length && nums[i] <= miss) {
92+
miss += nums[i];
93+
i++;
94+
} else {
95+
miss += miss;
96+
result++;
97+
}
98+
}
99+
100+
return result;
101+
}
102+
```
103+
### Python
104+
105+
```Python
106+
from typing import List
107+
108+
class Solution:
109+
def minPatches(self, nums: List[int], n: int) -> int:
110+
miss = 1
111+
result = 0
112+
i = 0
113+
114+
while miss <= n:
115+
if i < len(nums) and nums[i] <= miss:
116+
miss += nums[i]
117+
i += 1
118+
else:
119+
miss += miss
120+
result += 1
121+
122+
return result
123+
```
124+
125+

0 commit comments

Comments
 (0)