2717. Semi-Ordered Permutation Easy

@problem@discussion
#Array#Simulation



1/**
2 * [2717] Semi-Ordered Permutation
3 *
4 * You are given a 0-indexed permutation of n integers nums.
5 * A permutation is called semi-ordered if the first number equals 1 and the last number equals n. You can perform the below operation as many times as you want until you make nums a semi-ordered permutation:
6 * 
7 * 	Pick two adjacent elements in nums, then swap them.
8 * 
9 * Return the minimum number of operations to make nums a semi-ordered permutation.
10 * A permutation is a sequence of integers from 1 to n of length n containing each number exactly once.
11 *  
12 * <strong class="example">Example 1:
13 * 
14 * Input: nums = [2,1,4,3]
15 * Output: 2
16 * Explanation: We can make the permutation semi-ordered using these sequence of operations: 
17 * 1 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3].
18 * 2 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4].
19 * It can be proved that there is no sequence of less than two operations that make nums a semi-ordered permutation. 
20 * 
21 * <strong class="example">Example 2:
22 * 
23 * Input: nums = [2,4,1,3]
24 * Output: 3
25 * Explanation: We can make the permutation semi-ordered using these sequence of operations:
26 * 1 - swap i = 1 and j = 2. The permutation becomes [2,1,4,3].
27 * 2 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3].
28 * 3 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4].
29 * It can be proved that there is no sequence of less than three operations that make nums a semi-ordered permutation.
30 * 
31 * <strong class="example">Example 3:
32 * 
33 * Input: nums = [1,3,4,2,5]
34 * Output: 0
35 * Explanation: The permutation is already a semi-ordered permutation.
36 * 
37 *  
38 * Constraints:
39 * 
40 * 	2 <= nums.length == n <= 50
41 * 	1 <= nums[i] <= 50
42 * 	nums is a permutation.
43 * 
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/semi-ordered-permutation/
48// discuss: https://leetcode.com/problems/semi-ordered-permutation/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53    pub fn semi_ordered_permutation(nums: Vec<i32>) -> i32 {
54        0
55    }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_2717() {
66    }
67}
68


Back
© 2025 bowen.ge All Rights Reserved.