20241211 finished.

This commit is contained in:
jackfiled 2024-12-11 12:09:18 +08:00
parent 3bcc0caba4
commit f711d3f4d6
2 changed files with 47 additions and 0 deletions

View File

@ -362,3 +362,5 @@ mod p782_transform_to_chessboard;
mod p1812_determine_color_of_a_chessboard_square;
mod p935_knight_dialer;
mod p2717_semi_ordered_permutation;

View File

@ -0,0 +1,45 @@
/**
* [2717] Semi-Ordered Permutation
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn semi_ordered_permutation(nums: Vec<i32>) -> i32 {
let n = nums.len();
let one_pos = nums
.iter()
.enumerate()
.find_map(|(i, v)| if *v == 1 { Some(i) } else { None })
.unwrap();
let n_pos = nums
.iter()
.enumerate()
.find_map(|(i, v)| if *v == n as i32 { Some(i) } else { None })
.unwrap();
let result = if one_pos < n_pos {
(n - 1 - n_pos) + (one_pos - 0)
} else {
(n - 1 - n_pos) + (one_pos - 0) - 1
} as i32;
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2717() {
assert_eq!(2, Solution::semi_ordered_permutation(vec![2, 1, 4, 3]));
assert_eq!(3, Solution::semi_ordered_permutation(vec![2, 4, 1, 3]));
assert_eq!(0, Solution::semi_ordered_permutation(vec![1, 3, 4, 2, 5]));
}
}