Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 40399af

Browse files
solves #2717: Semi-Ordered Permutation in java
1 parent cdf5978 commit 40399af

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

‎README.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@
828828
| 2706 | [Buy Two Chocolates](https://leetcode.com/problems/buy-two-chocolates) | [![Java](assets/java.png)](src/BuyTwoChocolates.java) | |
829829
| 2710 | [Remove Trailing Zeros From a String](https://leetcode.com/problems/remove-trailing-zeros-from-a-string) | [![Java](assets/java.png)](src/RemoveTrailingZerosFromAString.java) | |
830830
| 2716 | [Minimize String Length](https://leetcode.com/problems/minimize-string-length) | [![Java](assets/java.png)](src/MinimizeStringLength.java) | |
831-
| 2717 | [Semi-Ordered Permutation](https://leetcode.com/problems/semi-ordered-permutation) | | |
831+
| 2717 | [Semi-Ordered Permutation](https://leetcode.com/problems/semi-ordered-permutation) | [![Java](assets/java.png)](src/SemiOrderedPermutation.java) | |
832832
| 2728 | [Count Houses in a Circular Street](https://leetcode.com/problems/count-houses-in-a-circular-street) | | |
833833
| 2729 | [Check if The Number is Fascinating](https://leetcode.com/problems/check-if-the-number-is-fascinating) | | |
834834
| 2733 | [Neither Minimum nor Maximum](https://leetcode.com/problems/neither-minimum-nor-maximum) | | |

‎src/SemiOrderedPermutation.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// https://leetcode.com/problems/semi-ordered-permutation
2+
// T: O(N)
3+
// S: O(1)
4+
5+
import java.util.Arrays;
6+
7+
public class SemiOrderedPermutation {
8+
public int semiOrderedPermutation(int[] nums) {
9+
final int startIndex = findIndexOf(nums, 1);
10+
final int endIndex = findIndexOf(nums, nums.length);
11+
final int endDrift = nums.length - endIndex - 1;
12+
13+
if (endIndex < startIndex) {
14+
return startIndex + endDrift - 1;
15+
}
16+
return startIndex + endDrift;
17+
}
18+
19+
private int findIndexOf(int[] array, int element) {
20+
for (int i = 0 ; i < array.length ; i++) {
21+
if (array[i] == element) return i;
22+
}
23+
return -1;
24+
}
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /