2855. Minimum Right Shifts to Sort the Array Easy
1/**
2 * [2855] Minimum Right Shifts to Sort the Array
3 *
4 * You are given a 0-indexed array nums of length n containing distinct positive integers. Return the minimum number of right shifts required to sort nums and -1 if this is not possible.
5 * A right shift is defined as shifting the element at index i to index (i + 1) % n, for all indices.
6 *
7 * <strong class="example">Example 1:
8 *
9 * Input: nums = [3,4,5,1,2]
10 * Output: 2
11 * Explanation:
12 * After the first right shift, nums = [2,3,4,5,1].
13 * After the second right shift, nums = [1,2,3,4,5].
14 * Now nums is sorted; therefore the answer is 2.
15 *
16 * <strong class="example">Example 2:
17 *
18 * Input: nums = [1,3,5]
19 * Output: 0
20 * Explanation: nums is already sorted therefore, the answer is 0.
21 * <strong class="example">Example 3:
22 *
23 * Input: nums = [2,1,4]
24 * Output: -1
25 * Explanation: It's impossible to sort the array using right shifts.
26 *
27 *
28 * Constraints:
29 *
30 * 1 <= nums.length <= 100
31 * 1 <= nums[i] <= 100
32 * nums contains distinct integers.
33 *
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/minimum-right-shifts-to-sort-the-array/
38// discuss: https://leetcode.com/problems/minimum-right-shifts-to-sort-the-array/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43 pub fn minimum_right_shifts(nums: Vec<i32>) -> i32 {
44 0
45 }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_2855() {
56 }
57}
58
Back
© 2025 bowen.ge All Rights Reserved.