Skip to content

Commit 34eead1

Browse files
authored
Merge pull request #3611 from Ishitamukherjee2004/new-branch
Solution of 2717 (LC No.) is added
2 parents 169d3ef + ac7ed41 commit 34eead1

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
---
2+
id: semi-ordered-permutation
3+
title: Semi-Ordered Permutation
4+
sidebar_label: 2717-Semi_ordered-Permutation
5+
tags:
6+
- Array
7+
- Simulation
8+
9+
description: "This is a solution to the 2717."
10+
---
11+
12+
## Problem Description
13+
You are given a 0-indexed permutation of `n` integers `nums`.
14+
15+
A permutation is called semi-ordered if the first number equals `1` and the last number equals `n`. You can perform the below operation as many times as you want until you make `nums` a semi-ordered permutation:
16+
17+
Pick two adjacent elements in `nums`, then swap them.
18+
Return the minimum number of operations to make `nums` a semi-ordered permutation.
19+
20+
A permutation is a sequence of integers from `1` to `n` of length `n` containing each number exactly once.
21+
22+
23+
### Example
24+
25+
**Example 1:**
26+
27+
28+
```
29+
Input: nums = [2,1,4,3]
30+
Output: 2
31+
Explanation: We can make the permutation semi-ordered using these sequence of operations:
32+
1 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3].
33+
2 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4].
34+
It can be proved that there is no sequence of less than two operations that make nums a semi-ordered permutation.
35+
```
36+
**Example 2:**
37+
```
38+
Input: nums = [2,4,1,3]
39+
Output: 3
40+
Explanation: We can make the permutation semi-ordered using these sequence of operations:
41+
1 - swap i = 1 and j = 2. The permutation becomes [2,1,4,3].
42+
2 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3].
43+
3 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4].
44+
It can be proved that there is no sequence of less than three operations that make nums a semi-ordered permutation.
45+
```
46+
### Constraints
47+
48+
- `2 <= nums.length == n <= 50`
49+
50+
## Solution Approach
51+
52+
### Intuition:
53+
54+
To efficiently determine the Semi-Ordered Permutation
55+
## Solution Implementation
56+
57+
### Code In Different Languages:
58+
59+
<Tabs>
60+
<TabItem value="JavaScript" label="JavaScript" default>
61+
<SolutionAuthor name="@Ishitamukherjee2004"/>
62+
```javascript
63+
class Solution {
64+
semiOrderedPermutation(nums) {
65+
let n = nums.length;
66+
let left = 0, right = n - 1;
67+
let operations = 0;
68+
while (left < right) {
69+
if (nums[left] === left + 1) {
70+
left++;
71+
} else if (nums[right] === n - right) {
72+
right--;
73+
} else {
74+
[nums[left], nums[right]] = [nums[right], nums[left]];
75+
operations++;
76+
}
77+
}
78+
return operations;
79+
}
80+
}
81+
82+
```
83+
84+
</TabItem>
85+
<TabItem value="TypeScript" label="TypeScript">
86+
<SolutionAuthor name="@Ishitamukherjee2004"/>
87+
```typescript
88+
class Solution {
89+
semiOrderedPermutation(nums: number[]): number {
90+
let n = nums.length;
91+
let left = 0, right = n - 1;
92+
let operations = 0;
93+
while (left < right) {
94+
if (nums[left] === left + 1) {
95+
left++;
96+
} else if (nums[right] === n - right) {
97+
right--;
98+
} else {
99+
[nums[left], nums[right]] = [nums[right], nums[left]];
100+
operations++;
101+
}
102+
}
103+
return operations;
104+
}
105+
}
106+
107+
```
108+
109+
</TabItem>
110+
<TabItem value="Python" label="Python">
111+
<SolutionAuthor name="@Ishitamukherjee2004"/>
112+
```python
113+
class Solution:
114+
def semiOrderedPermutation(self, nums: List[int]) -> int:
115+
n = len(nums)
116+
left, right = 0, n - 1
117+
operations = 0
118+
while left < right:
119+
if nums[left] == left + 1:
120+
left += 1
121+
elif nums[right] == n - right:
122+
right -= 1
123+
else:
124+
nums[left], nums[right] = nums[right], nums[left]
125+
operations += 1
126+
return operations
127+
128+
```
129+
130+
</TabItem>
131+
<TabItem value="Java" label="Java">
132+
<SolutionAuthor name="@Ishitamukherjee2004"/>
133+
```java
134+
135+
public class Solution {
136+
public int semiOrderedPermutation(int[] nums) {
137+
int n = nums.length;
138+
int left = 0, right = n - 1;
139+
int operations = 0;
140+
while (left < right) {
141+
if (nums[left] == left + 1) {
142+
left++;
143+
} else if (nums[right] == n - right) {
144+
right--;
145+
} else {
146+
int temp = nums[left];
147+
nums[left] = nums[right];
148+
nums[right] = temp;
149+
operations++;
150+
}
151+
}
152+
return operations;
153+
}
154+
}
155+
156+
157+
```
158+
159+
</TabItem>
160+
<TabItem value="C++" label="C++">
161+
<SolutionAuthor name="@Ishitamukherjee2004"/>
162+
```cpp
163+
class Solution {
164+
public:
165+
int semiOrderedPermutation(vector<int>& nums) {
166+
int n = nums.size();
167+
int left = 0, right = n - 1;
168+
int operations = 0;
169+
while (left < right) {
170+
if (nums[left] == left + 1) {
171+
left++;
172+
} else if (nums[right] == n - right) {
173+
right--;
174+
} else {
175+
swap(nums[left], nums[right]);
176+
operations++;
177+
}
178+
}
179+
180+
return operations;
181+
}
182+
183+
};
184+
```
185+
</TabItem>
186+
</Tabs>
187+
188+
#### Complexity Analysis
189+
190+
- Time Complexity: $$O(n)$$
191+
- Space Complexity: $$O(1)$$
192+
- The time complexity is $$O(n)$$ where n is the length of the input array nums. This is because the method iterates through the array once, performing a constant amount of work for each element.
193+
- The space complexity is $$O(1)$$ because we are not using any extra space.

0 commit comments

Comments
 (0)