Skip to content

[pull] main from doocs:main #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion solution/0300-0399/0383.Ransom Note/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func canConstruct(ransomNote string, magazine string) bool {

```ts
function canConstruct(ransomNote: string, magazine: string): boolean {
const cnt = new Array(26).fill(0);
const cnt: number[] = Array(26).fill(0);
for (const c of magazine) {
++cnt[c.charCodeAt(0) - 97];
}
Expand Down
2 changes: 1 addition & 1 deletion solution/0300-0399/0383.Ransom Note/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func canConstruct(ransomNote string, magazine string) bool {

```ts
function canConstruct(ransomNote: string, magazine: string): boolean {
const cnt = new Array(26).fill(0);
const cnt: number[] = Array(26).fill(0);
for (const c of magazine) {
++cnt[c.charCodeAt(0) - 97];
}
Expand Down
2 changes: 1 addition & 1 deletion solution/0300-0399/0383.Ransom Note/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function canConstruct(ransomNote: string, magazine: string): boolean {
const cnt = new Array(26).fill(0);
const cnt: number[] = Array(26).fill(0);
for (const c of magazine) {
++cnt[c.charCodeAt(0) - 97];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# [10031. 大于等于顺序前缀和的最小缺失整数](https://leetcode.cn/problems/smallest-missing-integer-greater-than-sequential-prefix-sum)

[English Version](/solution/10000-10099/10031.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;。</p>

<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>

<p>请你返回 <code>nums</code>&nbsp;中没有出现过的 <strong>最小</strong>&nbsp;整数&nbsp;<code>x</code>&nbsp;,满足&nbsp;<code>x</code>&nbsp;大于等于&nbsp;<strong>最长</strong> 顺序前缀的和。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<pre>
<b>输入:</b>nums = [1,2,3,2,5]
<b>输出:</b>6
<b>解释:</b>nums 的最长顺序前缀是 [1,2,3] ,和为 6 ,6 不在数组中,所以 6 是大于等于最长顺序前缀和的最小整数。
</pre>

<p><strong class="example">示例 2:</strong></p>

<pre>
<strong>输入:</strong>nums = [3,4,5,1,12,14,13]
<b>输出:</b>15
<b>解释:</b>nums 的最长顺序前缀是 [3,4,5] ,和为 12 ,12、13 和 14 都在数组中,但 15 不在,所以 15 是大于等于最长顺序前缀和的最小整数。
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
<li><code>1 &lt;= nums[i] &lt;= 50</code></li>
</ul>

## 解法

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

<!-- tabs:start -->

### **Python3**

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

```python
class Solution:
def missingInteger(self, nums: List[int]) -> int:
s, n = nums[0], len(nums)
j = 1
while j < len(nums) and nums[j] == nums[j - 1] + 1:
s += nums[j]
j += 1
vis = set(nums)
for x in count(s):
if x not in vis:
return x
```

### **Java**

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

```java
class Solution {
public int missingInteger(int[] nums) {
int s = nums[0], j = 1;
while (j < nums.length && nums[j] == nums[j - 1] + 1) {
s += nums[j++];
}
boolean[] vis = new boolean[51];
for (int x : nums) {
vis[x] = true;
}
for (int x = s;; ++x) {
if (x >= vis.length || !vis[x]) {
return x;
}
}
}
}
```

### **C++**

```cpp
class Solution {
public:
int missingInteger(vector<int>& nums) {
int s = nums[0], j = 1;
while (j < nums.size() && nums[j] == nums[j - 1] + 1) {
s += nums[j++];
}
bool vis[51]{};
for (int x : nums) {
vis[x] = true;
}
for (int x = s;; ++x) {
if (x >= 51 || !vis[x]) {
return x;
}
}
}
};
```

### **Go**

```go
func missingInteger(nums []int) int {
s, j := nums[0], 1
for j < len(nums) && nums[j] == nums[j-1]+1 {
s, j = s+nums[j], j+1
}
vis := [51]bool{}
for _, x := range nums {
vis[x] = true
}
for x := s; ; x++ {
if x >= len(vis) || !vis[x] {
return x
}
}
}
```

### **TypeScript**

```ts
function missingInteger(nums: number[]): number {
let [s, j] = [nums[0], 1];
const n = nums.length;
while (j < n && nums[j] === nums[j - 1] + 1) {
s += nums[j++];
}
const vis: boolean[] = Array(51).fill(false);
for (const x of nums) {
vis[x] = true;
}
for (let x = s; ; ++x) {
if (x >= vis.length || !vis[x]) {
return x;
}
}
}
```

### **...**

```

```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# [10031. Smallest Missing Integer Greater Than Sequential Prefix Sum](https://leetcode.com/problems/smallest-missing-integer-greater-than-sequential-prefix-sum)

[中文文档](/solution/10000-10099/10031.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README.md)

## Description

<p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code>.</p>

<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>

<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>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,2,5]
<strong>Output:</strong> 6
<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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,4,5,1,12,14,13]
<strong>Output:</strong> 15
<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.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
<li><code>1 &lt;= nums[i] &lt;= 50</code></li>
</ul>

## Solutions

<!-- tabs:start -->

### **Python3**

```python
class Solution:
def missingInteger(self, nums: List[int]) -> int:
s, n = nums[0], len(nums)
j = 1
while j < len(nums) and nums[j] == nums[j - 1] + 1:
s += nums[j]
j += 1
vis = set(nums)
for x in count(s):
if x not in vis:
return x
```

### **Java**

```java
class Solution {
public int missingInteger(int[] nums) {
int s = nums[0], j = 1;
while (j < nums.length && nums[j] == nums[j - 1] + 1) {
s += nums[j++];
}
boolean[] vis = new boolean[51];
for (int x : nums) {
vis[x] = true;
}
for (int x = s;; ++x) {
if (x >= vis.length || !vis[x]) {
return x;
}
}
}
}
```

### **C++**

```cpp
class Solution {
public:
int missingInteger(vector<int>& nums) {
int s = nums[0], j = 1;
while (j < nums.size() && nums[j] == nums[j - 1] + 1) {
s += nums[j++];
}
bool vis[51]{};
for (int x : nums) {
vis[x] = true;
}
for (int x = s;; ++x) {
if (x >= 51 || !vis[x]) {
return x;
}
}
}
};
```

### **Go**

```go
func missingInteger(nums []int) int {
s, j := nums[0], 1
for j < len(nums) && nums[j] == nums[j-1]+1 {
s, j = s+nums[j], j+1
}
vis := [51]bool{}
for _, x := range nums {
vis[x] = true
}
for x := s; ; x++ {
if x >= len(vis) || !vis[x] {
return x
}
}
}
```

### **TypeScript**

```ts
function missingInteger(nums: number[]): number {
let [s, j] = [nums[0], 1];
const n = nums.length;
while (j < n && nums[j] === nums[j - 1] + 1) {
s += nums[j++];
}
const vis: boolean[] = Array(51).fill(false);
for (const x of nums) {
vis[x] = true;
}
for (let x = s; ; ++x) {
if (x >= vis.length || !vis[x]) {
return x;
}
}
}
```

### **...**

```

```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int missingInteger(vector<int>& nums) {
int s = nums[0], j = 1;
while (j < nums.size() && nums[j] == nums[j - 1] + 1) {
s += nums[j++];
}
bool vis[51]{};
for (int x : nums) {
vis[x] = true;
}
for (int x = s;; ++x) {
if (x >= 51 || !vis[x]) {
return x;
}
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
func missingInteger(nums []int) int {
s, j := nums[0], 1
for j < len(nums) && nums[j] == nums[j-1]+1 {
s, j = s+nums[j], j+1
}
vis := [51]bool{}
for _, x := range nums {
vis[x] = true
}
for x := s; ; x++ {
if x >= len(vis) || !vis[x] {
return x
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int missingInteger(int[] nums) {
int s = nums[0], j = 1;
while (j < nums.length && nums[j] == nums[j - 1] + 1) {
s += nums[j++];
}
boolean[] vis = new boolean[51];
for (int x : nums) {
vis[x] = true;
}
for (int x = s;; ++x) {
if (x >= vis.length || !vis[x]) {
return x;
}
}
}
}
Loading