File tree 2 files changed +26
-1
lines changed
2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 828
828
| 2706 | [Buy Two Chocolates](https://leetcode.com/problems/buy-two-chocolates) | [](src/BuyTwoChocolates.java) | |
829
829
| 2710 | [Remove Trailing Zeros From a String](https://leetcode.com/problems/remove-trailing-zeros-from-a-string) | [](src/RemoveTrailingZerosFromAString.java) | |
830
830
| 2716 | [Minimize String Length](https://leetcode.com/problems/minimize-string-length) | [](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) | [](src/SemiOrderedPermutation.java) | |
832
832
| 2728 | [Count Houses in a Circular Street](https://leetcode.com/problems/count-houses-in-a-circular-street) | | |
833
833
| 2729 | [Check if The Number is Fascinating](https://leetcode.com/problems/check-if-the-number-is-fascinating) | | |
834
834
| 2733 | [Neither Minimum nor Maximum](https://leetcode.com/problems/neither-minimum-nor-maximum) | | |
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments