Skip to content

Commit 446f006

Browse files
add 2855
1 parent 6321f13 commit 446f006

File tree

3 files changed

+63
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+63
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
| 2869 | [Minimum Operations to Collect Elements](https://leetcode.com/problems/minimum-operations-to-collect-elements/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2869.java) | | Easy |
1111
| 2864 | [Maximum Odd Binary Number](https://leetcode.com/problems/maximum-odd-binary-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2864.java) | | Easy |Greedy
1212
| 2859 | [Sum of Values at Indices With K Set Bits](https://leetcode.com/problems/sum-of-values-at-indices-with-k-set-bits/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2859.java) | | Easy |
13+
| 2855 | [Minimum Right Shifts to Sort the Array](https://leetcode.com/problems/minimum-right-shifts-to-sort-the-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2855.java) | | Easy |
1314
| 2839 | [Check if Strings Can be Made Equal With Operations I](https://leetcode.com/problems/check-if-strings-can-be-made-equal-with-operations-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2839.java) | | Easy |
1415
| 2833 | [Furthest Point From Origin](https://leetcode.com/problems/furthest-point-from-origin/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2833.java) | | Easy |
1516
| 2828 | [Check if a String Is an Acronym of Words](https://leetcode.com/problems/check-if-a-string-is-an-acronym-of-words/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2828.java) | | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _2855 {
7+
public static class Solution1 {
8+
public int minimumRightShifts(List<Integer> nums) {
9+
int shifts = 0;
10+
do {
11+
if (sorted(nums)) {
12+
return shifts;
13+
}
14+
nums = shiftByOne(nums);
15+
shifts++;
16+
} while (shifts < nums.size());
17+
return -1;
18+
}
19+
20+
private List<Integer> shiftByOne(List<Integer> list) {
21+
List<Integer> shifted = new ArrayList<>();
22+
shifted.add(list.get(list.size() - 1));
23+
for (int i = 0; i < list.size() - 1; i++) {
24+
shifted.add(list.get(i));
25+
}
26+
return shifted;
27+
}
28+
29+
private boolean sorted(List<Integer> nums) {
30+
for (int i = 0; i < nums.size() - 1; i++) {
31+
if (nums.get(i) >= nums.get(i + 1)) {
32+
return false;
33+
}
34+
}
35+
return true;
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2855;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
public class _2855Test {
12+
private static _2855.Solution1 solution1;
13+
14+
@BeforeEach
15+
public void setup() {
16+
solution1 = new _2855.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(2, solution1.minimumRightShifts(Arrays.asList(3, 4, 5, 1, 2)));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)