Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9d01a95

Browse files
authored
1 parent 5b85006 commit 9d01a95

File tree

14 files changed

+760
-0
lines changed

14 files changed

+760
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [10031. 大于等于顺序前缀和的最小缺失整数](https://leetcode.cn/problems/smallest-missing-integer-greater-than-sequential-prefix-sum)
2+
3+
[English Version](/solution/10000-10099/10031.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;。</p>
10+
11+
<p>如果一个前缀&nbsp;<code>nums[0..i]</code>&nbsp;满足对于&nbsp;<code>1 &lt;= j &lt;= i</code>&nbsp;的所有元素都有&nbsp;<code>nums[j] = nums[j - 1] + 1</code>&nbsp;,那么我们称这个前缀是一个 <strong>顺序前缀</strong> 。特殊情况是,只包含&nbsp;<code>nums[0]</code>&nbsp;的前缀也是一个 <strong>顺序前缀</strong> 。</p>
12+
13+
<p>请你返回 <code>nums</code>&nbsp;中没有出现过的 <strong>最小</strong>&nbsp;整数&nbsp;<code>x</code>&nbsp;,满足&nbsp;<code>x</code>&nbsp;大于等于&nbsp;<strong>最长</strong> 顺序前缀的和。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong class="example">示例 1:</strong></p>
18+
19+
<pre>
20+
<b>输入:</b>nums = [1,2,3,2,5]
21+
<b>输出:</b>6
22+
<b>解释:</b>nums 的最长顺序前缀是 [1,2,3] ,和为 6 ,6 不在数组中,所以 6 是大于等于最长顺序前缀和的最小整数。
23+
</pre>
24+
25+
<p><strong class="example">示例 2:</strong></p>
26+
27+
<pre>
28+
<strong>输入:</strong>nums = [3,4,5,1,12,14,13]
29+
<b>输出:</b>15
30+
<b>解释:</b>nums 的最长顺序前缀是 [3,4,5] ,和为 12 ,12、13 和 14 都在数组中,但 15 不在,所以 15 是大于等于最长顺序前缀和的最小整数。
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
35+
<p><strong>提示:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
39+
<li><code>1 &lt;= nums[i] &lt;= 50</code></li>
40+
</ul>
41+
42+
## 解法
43+
44+
<!-- 这里可写通用的实现逻辑 -->
45+
46+
<!-- tabs:start -->
47+
48+
### **Python3**
49+
50+
<!-- 这里可写当前语言的特殊实现逻辑 -->
51+
52+
```python
53+
54+
```
55+
56+
### **Java**
57+
58+
<!-- 这里可写当前语言的特殊实现逻辑 -->
59+
60+
```java
61+
62+
```
63+
64+
### **C++**
65+
66+
```cpp
67+
68+
```
69+
70+
### **Go**
71+
72+
```go
73+
74+
```
75+
76+
### **...**
77+
78+
```
79+
80+
```
81+
82+
<!-- tabs:end -->
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# [10031. Smallest Missing Integer Greater Than Sequential Prefix Sum](https://leetcode.com/problems/smallest-missing-integer-greater-than-sequential-prefix-sum)
2+
3+
[中文文档](/solution/10000-10099/10031.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code>.</p>
8+
9+
<p>A prefix <code>nums[0..i]</code> is <strong>sequential</strong> if, for all <code>1 &lt;= j &lt;= i</code>, <code>nums[j] = nums[j - 1] + 1</code>. In particular, the prefix consisting only of <code>nums[0]</code> is <strong>sequential</strong>.</p>
10+
11+
<p>Return <em>the <strong>smallest</strong> integer</em> <code>x</code> <em>missing from</em> <code>nums</code> <em>such that</em> <code>x</code> <em>is greater than or equal to the sum of the <strong>longest</strong> sequential prefix.</em></p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [1,2,3,2,5]
18+
<strong>Output:</strong> 6
19+
<strong>Explanation:</strong> The longest sequential prefix of nums is [1,2,3] with a sum of 6. 6 is not in the array, therefore 6 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> nums = [3,4,5,1,12,14,13]
26+
<strong>Output:</strong> 15
27+
<strong>Explanation:</strong> The longest sequential prefix of nums is [3,4,5] with a sum of 12. 12, 13, and 14 belong to the array while 15 does not. Therefore 15 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
35+
<li><code>1 &lt;= nums[i] &lt;= 50</code></li>
36+
</ul>
37+
38+
## Solutions
39+
40+
<!-- tabs:start -->
41+
42+
### **Python3**
43+
44+
```python
45+
46+
```
47+
48+
### **Java**
49+
50+
```java
51+
52+
```
53+
54+
### **C++**
55+
56+
```cpp
57+
58+
```
59+
60+
### **Go**
61+
62+
```go
63+
64+
```
65+
66+
### **...**
67+
68+
```
69+
70+
```
71+
72+
<!-- tabs:end -->
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [10032. 使数组异或和等于 K 的最少操作次数](https://leetcode.cn/problems/minimum-number-of-operations-to-make-array-xor-equal-to-k)
2+
3+
[English Version](/solution/10000-10099/10032.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20XOR%20Equal%20to%20K/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;和一个正整数&nbsp;<code>k</code>&nbsp;。</p>
10+
11+
<p>你可以对数组执行以下操作 <strong>任意次</strong>&nbsp;:</p>
12+
13+
<ul>
14+
<li>选择数组里的 <strong>任意</strong>&nbsp;一个元素,并将它的&nbsp;<strong>二进制</strong>&nbsp;表示&nbsp;<strong>翻转</strong>&nbsp;一个数位,翻转数位表示将&nbsp;<code>0</code> 变成&nbsp;<code>1</code>&nbsp;或者将 <code>1</code>&nbsp;变成 <code>0</code>&nbsp;。</li>
15+
</ul>
16+
17+
<p>你的目标是让数组里 <strong>所有</strong>&nbsp;元素的按位异或和得到 <code>k</code>&nbsp;,请你返回达成这一目标的 <strong>最少&nbsp;</strong>操作次数。</p>
18+
19+
<p><strong>注意</strong>,你也可以将一个数的前导 0 翻转。比方说,数字&nbsp;<code>(101)<sub>2</sub></code>&nbsp;翻转第四个数位,得到&nbsp;<code>(1101)<sub>2</sub></code>&nbsp;。</p>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong class="example">示例 1:</strong></p>
24+
25+
<pre>
26+
<b>输入:</b>nums = [2,1,3,4], k = 1
27+
<b>输出:</b>2
28+
<b>解释:</b>我们可以执行以下操作:
29+
- 选择下标为 2 的元素,也就是 3 == (011)<sub>2</sub>&nbsp;,我们翻转第一个数位得到 (010)<sub>2</sub> == 2 。数组变为 [2,1,2,4] 。
30+
- 选择下标为 0 的元素,也就是 2 == (010)<sub>2</sub> ,我们翻转第三个数位得到 (110)<sub>2</sub> == 6 。数组变为 [6,1,2,4] 。
31+
最终数组的所有元素异或和为 (6 XOR 1 XOR 2 XOR 4) == 1 == k 。
32+
无法用少于 2 次操作得到异或和等于 k 。
33+
</pre>
34+
35+
<p><strong class="example">示例 2:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong>nums = [2,0,2,0], k = 0
39+
<b>输出:</b>0
40+
<strong>解释:</strong>数组所有元素的异或和为 (2 XOR 0 XOR 2 XOR 0) == 0 == k 。所以不需要进行任何操作。
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
49+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
50+
<li><code>0 &lt;= k &lt;= 10<sup>6</sup></code></li>
51+
</ul>
52+
53+
## 解法
54+
55+
<!-- 这里可写通用的实现逻辑 -->
56+
57+
<!-- tabs:start -->
58+
59+
### **Python3**
60+
61+
<!-- 这里可写当前语言的特殊实现逻辑 -->
62+
63+
```python
64+
65+
```
66+
67+
### **Java**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```java
72+
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
79+
```
80+
81+
### **Go**
82+
83+
```go
84+
85+
```
86+
87+
### **...**
88+
89+
```
90+
91+
```
92+
93+
<!-- tabs:end -->
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [10032. Minimum Number of Operations to Make Array XOR Equal to K](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-xor-equal-to-k)
2+
3+
[中文文档](/solution/10000-10099/10032.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20XOR%20Equal%20to%20K/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p>
8+
9+
<p>You can apply the following operation on the array <strong>any</strong> number of times:</p>
10+
11+
<ul>
12+
<li>Choose <strong>any</strong> element of the array and <strong>flip</strong> a bit in its <strong>binary</strong> representation. Flipping a bit means changing a <code>0</code> to <code>1</code> or vice versa.</li>
13+
</ul>
14+
15+
<p>Return <em>the <strong>minimum</strong> number of operations required to make the bitwise </em><code>XOR</code><em> of <strong>all</strong> elements of the final array equal to </em><code>k</code>.</p>
16+
17+
<p><strong>Note</strong> that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> nums = [2,1,3,4], k = 1
24+
<strong>Output:</strong> 2
25+
<strong>Explanation:</strong> We can do the following operations:
26+
- Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4].
27+
- Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4].
28+
The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k.
29+
It can be shown that we cannot make the XOR equal to k in less than 2 operations.
30+
</pre>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> nums = [2,0,2,0], k = 0
36+
<strong>Output:</strong> 0
37+
<strong>Explanation:</strong> The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
45+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
46+
<li><code>0 &lt;= k &lt;= 10<sup>6</sup></code></li>
47+
</ul>
48+
49+
## Solutions
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
```python
56+
57+
```
58+
59+
### **Java**
60+
61+
```java
62+
63+
```
64+
65+
### **C++**
66+
67+
```cpp
68+
69+
```
70+
71+
### **Go**
72+
73+
```go
74+
75+
```
76+
77+
### **...**
78+
79+
```
80+
81+
```
82+
83+
<!-- tabs:end -->

0 commit comments

Comments
 (0)