Skip to content

Commit 80259e4

Browse files
authored
Create Minimum Right Shifts to Sort the Array.java
1 parent 76f32c6 commit 80259e4

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int minimumRightShifts(List<Integer> nums) {
3+
int mismatchIdx = -1;
4+
int count = 0;
5+
for (int i = 1; i < nums.size(); i++) {
6+
if (nums.get(i - 1) > nums.get(i)) {
7+
mismatchIdx = i;
8+
count++;
9+
}
10+
}
11+
if (count > 1) {
12+
return -1;
13+
}
14+
if (count == 0) {
15+
return 0;
16+
}
17+
return nums.get(nums.size() - 1) > nums.get(0) ? -1 : nums.size() - mismatchIdx;
18+
}
19+
}

0 commit comments

Comments
 (0)