-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat: add solutions to lc problems: No.2855~2858 #1639
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
164 changes: 164 additions & 0 deletions
solution/2800-2899/2855.Minimum Right Shifts to Sort the Array/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
# [2855. 使数组成为递增数组的最少右移次数](https://leetcode.cn/problems/minimum-right-shifts-to-sort-the-array) | ||
|
||
[English Version](/solution/2800-2899/2855.Minimum%20Right%20Shifts%20to%20Sort%20the%20Array/README_EN.md) | ||
|
||
## 题目描述 | ||
|
||
<!-- 这里写题目描述 --> | ||
|
||
<p>给你一个长度为 <code>n</code> 下标从 <strong>0</strong> 开始的数组 <code>nums</code> ,数组中的元素为 <strong>互不相同</strong> 的正整数。请你返回让 <code>nums</code> 成为递增数组的 <strong>最少右移</strong> 次数,如果无法得到递增数组,返回 <code>-1</code> 。</p> | ||
|
||
<p>一次 <strong>右移</strong> 指的是同时对所有下标进行操作,将下标为 <code>i</code> 的元素移动到下标 <code>(i + 1) % n</code> 处。</p> | ||
|
||
<p> </p> | ||
|
||
<p><strong class="example">示例 1:</strong></p> | ||
|
||
<pre> | ||
<b>输入:</b>nums = [3,4,5,1,2] | ||
<b>输出:</b>2 | ||
<b>解释:</b> | ||
第一次右移后,nums = [2,3,4,5,1] 。 | ||
第二次右移后,nums = [1,2,3,4,5] 。 | ||
现在 nums 是递增数组了,所以答案为 2 。 | ||
</pre> | ||
|
||
<p><strong class="example">示例 2:</strong></p> | ||
|
||
<pre> | ||
<b>输入:</b>nums = [1,3,5] | ||
<b>输出:</b>0 | ||
<b>解释:</b>nums 已经是递增数组了,所以答案为 0 。</pre> | ||
|
||
<p><strong class="example">示例 3:</strong></p> | ||
|
||
<pre> | ||
<b>输入:</b>nums = [2,1,4] | ||
<b>输出:</b>-1 | ||
<b>解释:</b>无法将数组变为递增数组。 | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 100</code></li> | ||
<li><code>1 <= nums[i] <= 100</code></li> | ||
<li><code>nums</code> 中的整数互不相同。</li> | ||
</ul> | ||
|
||
## 解法 | ||
|
||
<!-- 这里可写通用的实现逻辑 --> | ||
|
||
**方法一:直接遍历** | ||
|
||
我们先用一个指针 $i$ 从左到右遍历数组 $nums,ドル找出一段连续的递增序列,直到 $i$ 到达数组末尾或者 $nums[i - 1] \gt nums[i]$。接下来我们用另一个指针 $k$ 从 $i + 1$ 开始遍历数组 $nums,ドル找出一段连续的递增序列,直到 $k$ 到达数组末尾或者 $nums[k - 1] \gt nums[k]$ 且 $nums[k] \gt nums[0]$。如果 $k$ 到达数组末尾,说明数组已经是递增的,返回 $n - i$;否则返回 $-1$。 | ||
|
||
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 是数组 $nums$ 的长度。 | ||
|
||
<!-- tabs:start --> | ||
|
||
### **Python3** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```python | ||
class Solution: | ||
def minimumRightShifts(self, nums: List[int]) -> int: | ||
n = len(nums) | ||
i = 1 | ||
while i < n and nums[i - 1] < nums[i]: | ||
i += 1 | ||
k = i + 1 | ||
while k < n and nums[k - 1] < nums[k] < nums[0]: | ||
k += 1 | ||
return -1 if k < n else n - i | ||
``` | ||
|
||
### **Java** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```java | ||
class Solution { | ||
public int minimumRightShifts(List<Integer> nums) { | ||
int n = nums.size(); | ||
int i = 1; | ||
while (i < n && nums.get(i - 1) < nums.get(i)) { | ||
++i; | ||
} | ||
int k = i + 1; | ||
while (k < n && nums.get(k - 1) < nums.get(k) && nums.get(k) < nums.get(0)) { | ||
++k; | ||
} | ||
return k < n ? -1 : n - i; | ||
} | ||
} | ||
``` | ||
|
||
### **C++** | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int minimumRightShifts(vector<int>& nums) { | ||
int n = nums.size(); | ||
int i = 1; | ||
while (i < n && nums[i - 1] < nums[i]) { | ||
++i; | ||
} | ||
int k = i + 1; | ||
while (k < n && nums[k - 1] < nums[k] && nums[k] < nums[0]) { | ||
++k; | ||
} | ||
return k < n ? -1 : n - i; | ||
} | ||
}; | ||
``` | ||
|
||
### **Go** | ||
|
||
```go | ||
func minimumRightShifts(nums []int) int { | ||
n := len(nums) | ||
i := 1 | ||
for i < n && nums[i-1] < nums[i] { | ||
i++ | ||
} | ||
k := i + 1 | ||
for k < n && nums[k-1] < nums[k] && nums[k] < nums[0] { | ||
k++ | ||
} | ||
if k < n { | ||
return -1 | ||
} | ||
return n - i | ||
} | ||
``` | ||
|
||
### **TypeScript** | ||
|
||
```ts | ||
function minimumRightShifts(nums: number[]): number { | ||
const n = nums.length; | ||
let i = 1; | ||
while (i < n && nums[i - 1] < nums[i]) { | ||
++i; | ||
} | ||
let k = i + 1; | ||
while (k < n && nums[k - 1] < nums[k] && nums[k] < nums[0]) { | ||
++k; | ||
} | ||
return k < n ? -1 : n - i; | ||
} | ||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
|
||
``` | ||
|
||
<!-- tabs:end --> |
148 changes: 148 additions & 0 deletions
solution/2800-2899/2855.Minimum Right Shifts to Sort the Array/README_EN.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# [2855. Minimum Right Shifts to Sort the Array](https://leetcode.com/problems/minimum-right-shifts-to-sort-the-array) | ||
|
||
[中文文档](/solution/2800-2899/2855.Minimum%20Right%20Shifts%20to%20Sort%20the%20Array/README.md) | ||
|
||
## Description | ||
|
||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of length <code>n</code> containing <strong>distinct</strong> positive integers. Return <em>the <strong>minimum</strong> number of <strong>right shifts</strong> required to sort </em><code>nums</code><em> and </em><code>-1</code><em> if this is not possible.</em></p> | ||
|
||
<p>A <strong>right shift</strong> is defined as shifting the element at index <code>i</code> to index <code>(i + 1) % n</code>, for all indices.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [3,4,5,1,2] | ||
<strong>Output:</strong> 2 | ||
<strong>Explanation:</strong> | ||
After the first right shift, nums = [2,3,4,5,1]. | ||
After the second right shift, nums = [1,2,3,4,5]. | ||
Now nums is sorted; therefore the answer is 2. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [1,3,5] | ||
<strong>Output:</strong> 0 | ||
<strong>Explanation:</strong> nums is already sorted therefore, the answer is 0.</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [2,1,4] | ||
<strong>Output:</strong> -1 | ||
<strong>Explanation:</strong> It's impossible to sort the array using right shifts. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 100</code></li> | ||
<li><code>1 <= nums[i] <= 100</code></li> | ||
<li><code>nums</code> contains distinct integers.</li> | ||
</ul> | ||
|
||
## Solutions | ||
|
||
<!-- tabs:start --> | ||
|
||
### **Python3** | ||
|
||
```python | ||
class Solution: | ||
def minimumRightShifts(self, nums: List[int]) -> int: | ||
n = len(nums) | ||
i = 1 | ||
while i < n and nums[i - 1] < nums[i]: | ||
i += 1 | ||
k = i + 1 | ||
while k < n and nums[k - 1] < nums[k] < nums[0]: | ||
k += 1 | ||
return -1 if k < n else n - i | ||
``` | ||
|
||
### **Java** | ||
|
||
```java | ||
class Solution { | ||
public int minimumRightShifts(List<Integer> nums) { | ||
int n = nums.size(); | ||
int i = 1; | ||
while (i < n && nums.get(i - 1) < nums.get(i)) { | ||
++i; | ||
} | ||
int k = i + 1; | ||
while (k < n && nums.get(k - 1) < nums.get(k) && nums.get(k) < nums.get(0)) { | ||
++k; | ||
} | ||
return k < n ? -1 : n - i; | ||
} | ||
} | ||
``` | ||
|
||
### **C++** | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int minimumRightShifts(vector<int>& nums) { | ||
int n = nums.size(); | ||
int i = 1; | ||
while (i < n && nums[i - 1] < nums[i]) { | ||
++i; | ||
} | ||
int k = i + 1; | ||
while (k < n && nums[k - 1] < nums[k] && nums[k] < nums[0]) { | ||
++k; | ||
} | ||
return k < n ? -1 : n - i; | ||
} | ||
}; | ||
``` | ||
|
||
### **Go** | ||
|
||
```go | ||
func minimumRightShifts(nums []int) int { | ||
n := len(nums) | ||
i := 1 | ||
for i < n && nums[i-1] < nums[i] { | ||
i++ | ||
} | ||
k := i + 1 | ||
for k < n && nums[k-1] < nums[k] && nums[k] < nums[0] { | ||
k++ | ||
} | ||
if k < n { | ||
return -1 | ||
} | ||
return n - i | ||
} | ||
``` | ||
|
||
### **TypeScript** | ||
|
||
```ts | ||
function minimumRightShifts(nums: number[]): number { | ||
const n = nums.length; | ||
let i = 1; | ||
while (i < n && nums[i - 1] < nums[i]) { | ||
++i; | ||
} | ||
let k = i + 1; | ||
while (k < n && nums[k - 1] < nums[k] && nums[k] < nums[0]) { | ||
++k; | ||
} | ||
return k < n ? -1 : n - i; | ||
} | ||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
|
||
``` | ||
|
||
<!-- tabs:end --> |
15 changes: 15 additions & 0 deletions
solution/2800-2899/2855.Minimum Right Shifts to Sort the Array/Solution.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public: | ||
int minimumRightShifts(vector<int>& nums) { | ||
int n = nums.size(); | ||
int i = 1; | ||
while (i < n && nums[i - 1] < nums[i]) { | ||
++i; | ||
} | ||
int k = i + 1; | ||
while (k < n && nums[k - 1] < nums[k] && nums[k] < nums[0]) { | ||
++k; | ||
} | ||
return k < n ? -1 : n - i; | ||
} | ||
}; |
15 changes: 15 additions & 0 deletions
solution/2800-2899/2855.Minimum Right Shifts to Sort the Array/Solution.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
func minimumRightShifts(nums []int) int { | ||
n := len(nums) | ||
i := 1 | ||
for i < n && nums[i-1] < nums[i] { | ||
i++ | ||
} | ||
k := i + 1 | ||
for k < n && nums[k-1] < nums[k] && nums[k] < nums[0] { | ||
k++ | ||
} | ||
if k < n { | ||
return -1 | ||
} | ||
return n - i | ||
} |
14 changes: 14 additions & 0 deletions
solution/2800-2899/2855.Minimum Right Shifts to Sort the Array/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution { | ||
public int minimumRightShifts(List<Integer> nums) { | ||
int n = nums.size(); | ||
int i = 1; | ||
while (i < n && nums.get(i - 1) < nums.get(i)) { | ||
++i; | ||
} | ||
int k = i + 1; | ||
while (k < n && nums.get(k - 1) < nums.get(k) && nums.get(k) < nums.get(0)) { | ||
++k; | ||
} | ||
return k < n ? -1 : n - i; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
solution/2800-2899/2855.Minimum Right Shifts to Sort the Array/Solution.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution: | ||
def minimumRightShifts(self, nums: List[int]) -> int: | ||
n = len(nums) | ||
i = 1 | ||
while i < n and nums[i - 1] < nums[i]: | ||
i += 1 | ||
k = i + 1 | ||
while k < n and nums[k - 1] < nums[k] < nums[0]: | ||
k += 1 | ||
return -1 if k < n else n - i |
12 changes: 12 additions & 0 deletions
solution/2800-2899/2855.Minimum Right Shifts to Sort the Array/Solution.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function minimumRightShifts(nums: number[]): number { | ||
const n = nums.length; | ||
let i = 1; | ||
while (i < n && nums[i - 1] < nums[i]) { | ||
++i; | ||
} | ||
let k = i + 1; | ||
while (k < n && nums[k - 1] < nums[k] && nums[k] < nums[0]) { | ||
++k; | ||
} | ||
return k < n ? -1 : n - i; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.