Skip to content

Commit d587296

Browse files
committed
feat: add solutions to lc problem: No.0330
No.0330.Patching Array
1 parent 67905ed commit d587296

File tree

8 files changed

+257
-3
lines changed

8 files changed

+257
-3
lines changed

solution/0300-0399/0330.Patching Array/README.md

+104-1
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,125 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:贪心**
57+
58+
我们假设数字 $x$ 是最小的不能表示的正整数,那么 $[1,..x-1]$ 的这些数都是可以表示的。为了能表示数字 $x$,我们需要添加一个小于等于 $x$ 的数:
59+
60+
- 如果添加的数等于 $x$,由于 $[1,..x-1]$ 的数都可以表示,添加 $x$ 后,区间 $[1,..2x-1]$ 内的数都可以表示,最小的不能表示的正整数变成了 $2x$。
61+
- 如果添加的数小于 $x$,不妨设为 $x'$,由于 $[1,..x-1]$ 的数都可以表示,添加 $x'$ 后,区间 $[1,..x+x'-1]$ 内的数都可以表示,最小的不能表示的正整数变成了 $x+x' \lt 2x$。
62+
63+
因此,我们应该贪心地添加数字 $x$,这样可以覆盖的区间更大。
64+
65+
我们用一个变量 $x$ 记录当前不能表示的最小正整数,初始化为 $1$,此时 $[1,..x-1]$ 是空的,表示当前没有任何数可以被覆盖;用一个变量 $i$ 记录当前遍历到的数组下标。
66+
67+
循环进行以下操作:
68+
69+
- 如果 $i$ 在数组范围内且 $nums[i] \le x$,说明当前数字可以被覆盖,因此将 $x$ 的值加上 $nums[i]$,并将 $i$ 的值加 $1$。
70+
- 否则,说明 $x$ 没有被覆盖,因此需要在数组中补充一个数 $x$,然后 $x$ 更新为 $2x$。
71+
- 重复上述操作,直到 $x$ 的值大于 $n$。
72+
73+
最终答案即为补充的数的数量。
74+
75+
时间复杂度 $O(m + \log n)$,其中 $m$ 为数组 $nums$ 的长度。空间复杂度 $O(1)$。
76+
5677
<!-- tabs:start -->
5778

5879
### **Python3**
5980

6081
<!-- 这里可写当前语言的特殊实现逻辑 -->
6182

6283
```python
63-
84+
class Solution:
85+
def minPatches(self, nums: List[int], n: int) -> int:
86+
x = 1
87+
ans = i = 0
88+
while x <= n:
89+
if i < len(nums) and nums[i] <= x:
90+
x += nums[i]
91+
i += 1
92+
else:
93+
ans += 1
94+
x <<= 1
95+
return ans
6496
```
6597

6698
### **Java**
6799

68100
<!-- 这里可写当前语言的特殊实现逻辑 -->
69101

70102
```java
103+
class Solution {
104+
public int minPatches(int[] nums, int n) {
105+
long x = 1;
106+
int ans = 0;
107+
for (int i = 0; x <= n;) {
108+
if (i < nums.length && nums[i] <= x) {
109+
x += nums[i++];
110+
} else {
111+
++ans;
112+
x <<= 1;
113+
}
114+
}
115+
return ans;
116+
}
117+
}
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
class Solution {
124+
public:
125+
int minPatches(vector<int>& nums, int n) {
126+
long long x = 1;
127+
int ans = 0;
128+
for (int i = 0; x <= n;) {
129+
if (i < nums.size() && nums[i] <= x) {
130+
x += nums[i++];
131+
} else {
132+
++ans;
133+
x <<= 1;
134+
}
135+
}
136+
return ans;
137+
}
138+
};
139+
```
140+
141+
### **Go**
142+
143+
```go
144+
func minPatches(nums []int, n int) (ans int) {
145+
x := 1
146+
for i := 0; x <= n; {
147+
if i < len(nums) && nums[i] <= x {
148+
x += nums[i]
149+
i++
150+
} else {
151+
ans++
152+
x <<= 1
153+
}
154+
}
155+
return
156+
}
157+
```
71158

159+
### **TypeScript**
160+
161+
```ts
162+
function minPatches(nums: number[], n: number): number {
163+
let x = 1;
164+
let ans = 0;
165+
for (let i = 0; x <= n; ) {
166+
if (i < nums.length && nums[i] <= x) {
167+
x += nums[i++];
168+
} else {
169+
++ans;
170+
x *= 2;
171+
}
172+
}
173+
return ans;
174+
}
72175
```
73176

74177
### **...**

solution/0300-0399/0330.Patching Array/README_EN.md

+83-1
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,95 @@ Explanation: The two patches can be [2, 4].
5353
### **Python3**
5454

5555
```python
56-
56+
class Solution:
57+
def minPatches(self, nums: List[int], n: int) -> int:
58+
x = 1
59+
ans = i = 0
60+
while x <= n:
61+
if i < len(nums) and nums[i] <= x:
62+
x += nums[i]
63+
i += 1
64+
else:
65+
ans += 1
66+
x <<= 1
67+
return ans
5768
```
5869

5970
### **Java**
6071

6172
```java
73+
class Solution {
74+
public int minPatches(int[] nums, int n) {
75+
long x = 1;
76+
int ans = 0;
77+
for (int i = 0; x <= n;) {
78+
if (i < nums.length && nums[i] <= x) {
79+
x += nums[i++];
80+
} else {
81+
++ans;
82+
x <<= 1;
83+
}
84+
}
85+
return ans;
86+
}
87+
}
88+
```
89+
90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
int minPatches(vector<int>& nums, int n) {
96+
long long x = 1;
97+
int ans = 0;
98+
for (int i = 0; x <= n;) {
99+
if (i < nums.size() && nums[i] <= x) {
100+
x += nums[i++];
101+
} else {
102+
++ans;
103+
x <<= 1;
104+
}
105+
}
106+
return ans;
107+
}
108+
};
109+
```
110+
111+
### **Go**
112+
113+
```go
114+
func minPatches(nums []int, n int) (ans int) {
115+
x := 1
116+
for i := 0; x <= n; {
117+
if i < len(nums) && nums[i] <= x {
118+
x += nums[i]
119+
i++
120+
} else {
121+
ans++
122+
x <<= 1
123+
}
124+
}
125+
return
126+
}
127+
```
62128

129+
### **TypeScript**
130+
131+
```ts
132+
function minPatches(nums: number[], n: number): number {
133+
let x = 1;
134+
let ans = 0;
135+
for (let i = 0; x <= n; ) {
136+
if (i < nums.length && nums[i] <= x) {
137+
x += nums[i++];
138+
} else {
139+
++ans;
140+
x *= 2;
141+
}
142+
}
143+
return ans;
144+
}
63145
```
64146

65147
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int minPatches(vector<int>& nums, int n) {
4+
long long x = 1;
5+
int ans = 0;
6+
for (int i = 0; x <= n;) {
7+
if (i < nums.size() && nums[i] <= x) {
8+
x += nums[i++];
9+
} else {
10+
++ans;
11+
x <<= 1;
12+
}
13+
}
14+
return ans;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func minPatches(nums []int, n int) (ans int) {
2+
x := 1
3+
for i := 0; x <= n; {
4+
if i < len(nums) && nums[i] <= x {
5+
x += nums[i]
6+
i++
7+
} else {
8+
ans++
9+
x <<= 1
10+
}
11+
}
12+
return
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int minPatches(int[] nums, int n) {
3+
long x = 1;
4+
int ans = 0;
5+
for (int i = 0; x <= n;) {
6+
if (i < nums.length && nums[i] <= x) {
7+
x += nums[i++];
8+
} else {
9+
++ans;
10+
x <<= 1;
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def minPatches(self, nums: List[int], n: int) -> int:
3+
x = 1
4+
ans = i = 0
5+
while x <= n:
6+
if i < len(nums) and nums[i] <= x:
7+
x += nums[i]
8+
i += 1
9+
else:
10+
ans += 1
11+
x <<= 1
12+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function minPatches(nums: number[], n: number): number {
2+
let x = 1;
3+
let ans = 0;
4+
for (let i = 0; x <= n; ) {
5+
if (i < nums.length && nums[i] <= x) {
6+
x += nums[i++];
7+
} else {
8+
++ans;
9+
x *= 2;
10+
}
11+
}
12+
return ans;
13+
}

solution/1000-1099/1073.Adding Two Negabinary Numbers/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
我们遍历两个数组,从最低位开始,记两个数组当前位的数字为 $a$ 和 $b$,进位为 $c$,三个数相加的结果为 $x$。
5959

6060
- 先将进位 $c$ 置为 $0$。
61-
- 如果 $x \geq 2$,那么将 $x$ 减去 $2$,并向高位进位 $-1$。即逢 $2$ 进负 $1$。
61+
- 如果 $x \geq 2$,由于 $(-2)^{i} + (-2)^{i} = -(-2)^{i+1}$,所以我们可以将 $x$ 减去 $2$,并向高位进位 $-1$。
6262
- 如果 $x = -1$,由于 $-(-2)^{i} = (-2)^{i} + (-2)^{i+1}$,所以我们可以将 $x$ 置为 $1$,并向高位进位 $1$。
6363

6464
然后,我们将 $x$ 加入到答案数组中,然后继续处理下一位。

0 commit comments

Comments
 (0)