From fe8ae008f4fd4402fb4a5bb0240e3f9f8dc1a640 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 18 Jun 2024 12:49:55 +0800 Subject: [PATCH] feat: update solutions to lc problems: No.2678~2683 --- .../2678.Number of Senior Citizens/README.md | 47 +------------- .../README_EN.md | 47 +------------- .../Solution.rs | 16 ++--- .../Solution.ts | 9 +-- .../Solution2.rs | 9 --- .../Solution2.ts | 3 - .../2600-2699/2679.Sum in a Matrix/README.md | 58 +++-------------- .../2679.Sum in a Matrix/README_EN.md | 58 +++-------------- .../2679.Sum in a Matrix/Solution.rs | 24 +++---- .../2679.Sum in a Matrix/Solution2.rs | 19 ------ .../2681.Power of Heroes/README_EN.md | 18 +++++- .../README_EN.md | 10 ++- .../2683.Neighboring Bitwise XOR/README.md | 24 +------ .../2683.Neighboring Bitwise XOR/README_EN.md | 45 +++++++------- .../2683.Neighboring Bitwise XOR/Solution.ts | 6 +- .../2683.Neighboring Bitwise XOR/Solution2.ts | 3 - .../2716.Minimize String Length/README.md | 37 +---------- .../2716.Minimize String Length/README_EN.md | 37 +---------- .../2716.Minimize String Length/Solution.rs | 11 +--- .../2716.Minimize String Length/Solution2.rs | 13 ---- .../2717.Semi-Ordered Permutation/README.md | 62 +++---------------- .../README_EN.md | 62 +++---------------- .../2717.Semi-Ordered Permutation/Solution.rs | 23 +++---- .../Solution2.rs | 24 ------- 24 files changed, 121 insertions(+), 544 deletions(-) delete mode 100644 solution/2600-2699/2678.Number of Senior Citizens/Solution2.rs delete mode 100644 solution/2600-2699/2678.Number of Senior Citizens/Solution2.ts delete mode 100644 solution/2600-2699/2679.Sum in a Matrix/Solution2.rs delete mode 100644 solution/2600-2699/2683.Neighboring Bitwise XOR/Solution2.ts delete mode 100644 solution/2700-2799/2716.Minimize String Length/Solution2.rs delete mode 100644 solution/2700-2799/2717.Semi-Ordered Permutation/Solution2.rs diff --git a/solution/2600-2699/2678.Number of Senior Citizens/README.md b/solution/2600-2699/2678.Number of Senior Citizens/README.md index af82776b9af1c..59a7f81d40878 100644 --- a/solution/2600-2699/2678.Number of Senior Citizens/README.md +++ b/solution/2600-2699/2678.Number of Senior Citizens/README.md @@ -135,52 +135,7 @@ func countSeniors(details []string) (ans int) { ```ts function countSeniors(details: string[]): number { - let ans = 0; - for (const x of details) { - const age = parseInt(x.slice(11, 13)); - if (age > 60) { - ++ans; - } - } - return ans; -} -``` - -#### Rust - -```rust -impl Solution { - pub fn count_seniors(details: Vec) -> i32 { - let mut ans = 0; - - for s in details.iter() { - if let Ok(age) = s[11..13].parse::() { - if age > 60 { - ans += 1; - } - } - } - - ans - } -} -``` - - - - - - - -### 方法二 - - - -#### TypeScript - -```ts -function countSeniors(details: string[]): number { - return details.filter(v => parseInt(v.slice(11, 13)) > 60).length; + return details.filter(x => +x.slice(11, 13) > 60).length; } ``` diff --git a/solution/2600-2699/2678.Number of Senior Citizens/README_EN.md b/solution/2600-2699/2678.Number of Senior Citizens/README_EN.md index 4c0f22a62122b..00eed620e35fd 100644 --- a/solution/2600-2699/2678.Number of Senior Citizens/README_EN.md +++ b/solution/2600-2699/2678.Number of Senior Citizens/README_EN.md @@ -133,52 +133,7 @@ func countSeniors(details []string) (ans int) { ```ts function countSeniors(details: string[]): number { - let ans = 0; - for (const x of details) { - const age = parseInt(x.slice(11, 13)); - if (age > 60) { - ++ans; - } - } - return ans; -} -``` - -#### Rust - -```rust -impl Solution { - pub fn count_seniors(details: Vec) -> i32 { - let mut ans = 0; - - for s in details.iter() { - if let Ok(age) = s[11..13].parse::() { - if age > 60 { - ans += 1; - } - } - } - - ans - } -} -``` - - - - - - - -### Solution 2 - - - -#### TypeScript - -```ts -function countSeniors(details: string[]): number { - return details.filter(v => parseInt(v.slice(11, 13)) > 60).length; + return details.filter(x => +x.slice(11, 13) > 60).length; } ``` diff --git a/solution/2600-2699/2678.Number of Senior Citizens/Solution.rs b/solution/2600-2699/2678.Number of Senior Citizens/Solution.rs index 31713a2db248c..867e6ab362591 100644 --- a/solution/2600-2699/2678.Number of Senior Citizens/Solution.rs +++ b/solution/2600-2699/2678.Number of Senior Citizens/Solution.rs @@ -1,15 +1,9 @@ impl Solution { pub fn count_seniors(details: Vec) -> i32 { - let mut ans = 0; - - for s in details.iter() { - if let Ok(age) = s[11..13].parse::() { - if age > 60 { - ans += 1; - } - } - } - - ans + details + .iter() + .filter_map(|s| s[11..13].parse::().ok()) + .filter(|&age| age > 60) + .count() as i32 } } diff --git a/solution/2600-2699/2678.Number of Senior Citizens/Solution.ts b/solution/2600-2699/2678.Number of Senior Citizens/Solution.ts index 6c332106fc752..f8b578d0a56f8 100644 --- a/solution/2600-2699/2678.Number of Senior Citizens/Solution.ts +++ b/solution/2600-2699/2678.Number of Senior Citizens/Solution.ts @@ -1,10 +1,3 @@ function countSeniors(details: string[]): number { - let ans = 0; - for (const x of details) { - const age = parseInt(x.slice(11, 13)); - if (age > 60) { - ++ans; - } - } - return ans; + return details.filter(x => +x.slice(11, 13) > 60).length; } diff --git a/solution/2600-2699/2678.Number of Senior Citizens/Solution2.rs b/solution/2600-2699/2678.Number of Senior Citizens/Solution2.rs deleted file mode 100644 index 867e6ab362591..0000000000000 --- a/solution/2600-2699/2678.Number of Senior Citizens/Solution2.rs +++ /dev/null @@ -1,9 +0,0 @@ -impl Solution { - pub fn count_seniors(details: Vec) -> i32 { - details - .iter() - .filter_map(|s| s[11..13].parse::().ok()) - .filter(|&age| age > 60) - .count() as i32 - } -} diff --git a/solution/2600-2699/2678.Number of Senior Citizens/Solution2.ts b/solution/2600-2699/2678.Number of Senior Citizens/Solution2.ts deleted file mode 100644 index 1c269b1903ca6..0000000000000 --- a/solution/2600-2699/2678.Number of Senior Citizens/Solution2.ts +++ /dev/null @@ -1,3 +0,0 @@ -function countSeniors(details: string[]): number { - return details.filter(v => parseInt(v.slice(11, 13)) > 60).length; -} diff --git a/solution/2600-2699/2679.Sum in a Matrix/README.md b/solution/2600-2699/2679.Sum in a Matrix/README.md index 714b643c973f6..6c3f9fff12eba 100644 --- a/solution/2600-2699/2679.Sum in a Matrix/README.md +++ b/solution/2600-2699/2679.Sum in a Matrix/README.md @@ -168,22 +168,18 @@ function matrixSum(nums: number[][]): number { ```rust impl Solution { - pub fn matrix_sum(nums: Vec>) -> i32 { - let mut nums = nums; - for row in nums.iter_mut() { + pub fn matrix_sum(mut nums: Vec>) -> i32 { + for row in &mut nums { row.sort(); } - let transposed: Vec> = (0..nums[0].len()) - .map(|i| { - nums.iter() - .map(|row| row[i]) - .collect() - }) - .collect(); - - transposed - .iter() - .map(|row| row.iter().max().unwrap()) + (0..nums[0].len()) + .map(|col| + nums + .iter() + .map(|row| row[col]) + .max() + .unwrap() + ) .sum() } } @@ -193,38 +189,4 @@ impl Solution { - - -### 方法二 - - - -#### Rust - -```rust -impl Solution { - pub fn matrix_sum(nums: Vec>) -> i32 { - let mut nums = nums.clone(); - for row in nums.iter_mut() { - row.sort(); - } - - let mut ans = 0; - for j in 0..nums[0].len() { - let mut mx = 0; - for row in &nums { - mx = mx.max(row[j]); - } - ans += mx; - } - - ans - } -} -``` - - - - - diff --git a/solution/2600-2699/2679.Sum in a Matrix/README_EN.md b/solution/2600-2699/2679.Sum in a Matrix/README_EN.md index 1ed73ffc21fb4..de2ba66d7a560 100644 --- a/solution/2600-2699/2679.Sum in a Matrix/README_EN.md +++ b/solution/2600-2699/2679.Sum in a Matrix/README_EN.md @@ -159,22 +159,18 @@ function matrixSum(nums: number[][]): number { ```rust impl Solution { - pub fn matrix_sum(nums: Vec>) -> i32 { - let mut nums = nums; - for row in nums.iter_mut() { + pub fn matrix_sum(mut nums: Vec>) -> i32 { + for row in &mut nums { row.sort(); } - let transposed: Vec> = (0..nums[0].len()) - .map(|i| { - nums.iter() - .map(|row| row[i]) - .collect() - }) - .collect(); - - transposed - .iter() - .map(|row| row.iter().max().unwrap()) + (0..nums[0].len()) + .map(|col| + nums + .iter() + .map(|row| row[col]) + .max() + .unwrap() + ) .sum() } } @@ -184,38 +180,4 @@ impl Solution { - - -### Solution 2 - - - -#### Rust - -```rust -impl Solution { - pub fn matrix_sum(nums: Vec>) -> i32 { - let mut nums = nums.clone(); - for row in nums.iter_mut() { - row.sort(); - } - - let mut ans = 0; - for j in 0..nums[0].len() { - let mut mx = 0; - for row in &nums { - mx = mx.max(row[j]); - } - ans += mx; - } - - ans - } -} -``` - - - - - diff --git a/solution/2600-2699/2679.Sum in a Matrix/Solution.rs b/solution/2600-2699/2679.Sum in a Matrix/Solution.rs index 4f3f48bdbc350..8cdc9052996eb 100644 --- a/solution/2600-2699/2679.Sum in a Matrix/Solution.rs +++ b/solution/2600-2699/2679.Sum in a Matrix/Solution.rs @@ -1,20 +1,16 @@ impl Solution { - pub fn matrix_sum(nums: Vec>) -> i32 { - let mut nums = nums; - for row in nums.iter_mut() { + pub fn matrix_sum(mut nums: Vec>) -> i32 { + for row in &mut nums { row.sort(); } - let transposed: Vec> = (0..nums[0].len()) - .map(|i| { - nums.iter() - .map(|row| row[i]) - .collect() - }) - .collect(); - - transposed - .iter() - .map(|row| row.iter().max().unwrap()) + (0..nums[0].len()) + .map(|col| + nums + .iter() + .map(|row| row[col]) + .max() + .unwrap() + ) .sum() } } diff --git a/solution/2600-2699/2679.Sum in a Matrix/Solution2.rs b/solution/2600-2699/2679.Sum in a Matrix/Solution2.rs deleted file mode 100644 index 93212861342c3..0000000000000 --- a/solution/2600-2699/2679.Sum in a Matrix/Solution2.rs +++ /dev/null @@ -1,19 +0,0 @@ -impl Solution { - pub fn matrix_sum(nums: Vec>) -> i32 { - let mut nums = nums.clone(); - for row in nums.iter_mut() { - row.sort(); - } - - let mut ans = 0; - for j in 0..nums[0].len() { - let mut mx = 0; - for row in &nums { - mx = mx.max(row[j]); - } - ans += mx; - } - - ans - } -} diff --git a/solution/2600-2699/2681.Power of Heroes/README_EN.md b/solution/2600-2699/2681.Power of Heroes/README_EN.md index 72ee227a6625f..8fdeb7d06b635 100644 --- a/solution/2600-2699/2681.Power of Heroes/README_EN.md +++ b/solution/2600-2699/2681.Power of Heroes/README_EN.md @@ -70,7 +70,23 @@ The sum of powers of all groups is 8 + 1 + 64 + 4 + 32 + 16 + 16 = 141. -### Solution 1 +### Solution 1: Sorting + Mathematics + +We notice that the problem involves the maximum and minimum values of a subsequence, and the order of elements in the array does not affect the final result. Therefore, we can sort the array first. + +Next, we enumerate each element as the minimum value of the subsequence. Let's denote each element of the array as $a_1, a_2, \cdots, a_n$. The contribution of the subsequence with $a_i$ as the minimum value is: + +$$ +a_i \times (a_{i}^{2} + a_{i+1}^2 + 2 \times a_{i+2}^2 + 4 \times a_{i+3}^2 + \cdots + 2^{n-i-1} \times a_n^2) +$$ + +We notice that each $a_i$ will be multiplied by $a_i^2$, which we can directly add to the answer. For the remaining part, we can maintain it with a variable $p$, initially set to $0$. + +Then, we enumerate $a_i$ from right to left. Each time, we add $a_i \times p$ to the answer, and then set $p = p \times 2 + a_i^2$. + +After enumerating all $a_i$, return the answer. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array. diff --git a/solution/2600-2699/2682.Find the Losers of the Circular Game/README_EN.md b/solution/2600-2699/2682.Find the Losers of the Circular Game/README_EN.md index 383cb0fedb0c7..9a4f7a59e7584 100644 --- a/solution/2600-2699/2682.Find the Losers of the Circular Game/README_EN.md +++ b/solution/2600-2699/2682.Find the Losers of the Circular Game/README_EN.md @@ -76,7 +76,15 @@ tags: -### Solution 1 +### Solution 1: Simulation + +We use an array `vis` to record whether each friend has received the ball, initially, all friends have not received the ball. Then, we simulate the game process according to the rules described in the problem statement until a friend receives the ball for the second time. + +In the simulation process, we use two variables $i$ and $p$ to represent the current friend holding the ball and the current passing step length, respectively. Initially, $i=0, p=1$, indicating the first friend receives the ball. Each time the ball is passed, we update $i$ to $(i+p \times k) \bmod n$, representing the next friend's number to receive the ball, and then update $p$ to $p+1$, representing the step length for the next pass. The game ends when a friend receives the ball for the second time. + +Finally, we iterate through the array `vis` and add the numbers of friends who have not received the ball to the answer array. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of friends. diff --git a/solution/2600-2699/2683.Neighboring Bitwise XOR/README.md b/solution/2600-2699/2683.Neighboring Bitwise XOR/README.md index cb5152971b48a..304d374be5a98 100644 --- a/solution/2600-2699/2683.Neighboring Bitwise XOR/README.md +++ b/solution/2600-2699/2683.Neighboring Bitwise XOR/README.md @@ -156,29 +156,7 @@ func doesValidArrayExist(derived []int) bool { ```ts function doesValidArrayExist(derived: number[]): boolean { - let s = 0; - for (const x of derived) { - s ^= x; - } - return s === 0; -} -``` - - - - - - - -### 方法二 - - - -#### TypeScript - -```ts -function doesValidArrayExist(derived: number[]): boolean { - return derived.reduce((acc, x) => acc ^ x, 0) === 0; + return derived.reduce((acc, x) => acc ^ x) === 0; } ``` diff --git a/solution/2600-2699/2683.Neighboring Bitwise XOR/README_EN.md b/solution/2600-2699/2683.Neighboring Bitwise XOR/README_EN.md index 313aef5710a93..9c28e18331126 100644 --- a/solution/2600-2699/2683.Neighboring Bitwise XOR/README_EN.md +++ b/solution/2600-2699/2683.Neighboring Bitwise XOR/README_EN.md @@ -81,7 +81,26 @@ derived[1] = original[1] ⊕ original[0] = 1 -### Solution 1 +### Solution 1: Bit Manipulation + +Let's assume the original binary array is $a$, and the derived array is $b$. Then, we have: + +$$ +b_0 = a_0 \oplus a_1 \\ +b_1 = a_1 \oplus a_2 \\ +\cdots \\ +b_{n-1} = a_{n-1} \oplus a_0 +$$ + +Since the XOR operation is commutative and associative, we get: + +$$ +b_0 \oplus b_1 \oplus \cdots \oplus b_{n-1} = (a_0 \oplus a_1) \oplus (a_1 \oplus a_2) \oplus \cdots \oplus (a_{n-1} \oplus a_0) = 0 +$$ + +Therefore, as long as the XOR sum of all elements in the derived array is $0$, there must exist an original binary array that meets the requirements. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. @@ -138,29 +157,7 @@ func doesValidArrayExist(derived []int) bool { ```ts function doesValidArrayExist(derived: number[]): boolean { - let s = 0; - for (const x of derived) { - s ^= x; - } - return s === 0; -} -``` - - - - - - - -### Solution 2 - - - -#### TypeScript - -```ts -function doesValidArrayExist(derived: number[]): boolean { - return derived.reduce((acc, x) => acc ^ x, 0) === 0; + return derived.reduce((acc, x) => acc ^ x) === 0; } ``` diff --git a/solution/2600-2699/2683.Neighboring Bitwise XOR/Solution.ts b/solution/2600-2699/2683.Neighboring Bitwise XOR/Solution.ts index a6b7445c28f15..00d828dfa2b00 100644 --- a/solution/2600-2699/2683.Neighboring Bitwise XOR/Solution.ts +++ b/solution/2600-2699/2683.Neighboring Bitwise XOR/Solution.ts @@ -1,7 +1,3 @@ function doesValidArrayExist(derived: number[]): boolean { - let s = 0; - for (const x of derived) { - s ^= x; - } - return s === 0; + return derived.reduce((acc, x) => acc ^ x) === 0; } diff --git a/solution/2600-2699/2683.Neighboring Bitwise XOR/Solution2.ts b/solution/2600-2699/2683.Neighboring Bitwise XOR/Solution2.ts deleted file mode 100644 index 0211597d5403f..0000000000000 --- a/solution/2600-2699/2683.Neighboring Bitwise XOR/Solution2.ts +++ /dev/null @@ -1,3 +0,0 @@ -function doesValidArrayExist(derived: number[]): boolean { - return derived.reduce((acc, x) => acc ^ x, 0) === 0; -} diff --git a/solution/2700-2799/2716.Minimize String Length/README.md b/solution/2700-2799/2716.Minimize String Length/README.md index e42a1aebad986..86079bba90735 100644 --- a/solution/2700-2799/2716.Minimize String Length/README.md +++ b/solution/2700-2799/2716.Minimize String Length/README.md @@ -132,46 +132,13 @@ function minimizedStringLength(s: string): number { #### Rust -```rust -use std::collections::HashMap; - -impl Solution { - pub fn minimized_string_length(s: String) -> i32 { - let mut hash = HashMap::new(); - - for c in s.chars() { - hash.insert(c, true); - } - - hash.len() as i32 - } -} -``` - - - - - - - -### 方法二 - - - -#### Rust - ```rust use std::collections::HashSet; impl Solution { pub fn minimized_string_length(s: String) -> i32 { - let mut set = HashSet::new(); - - for c in s.chars() { - set.insert(c); - } - - set.len() as i32 + let ss: HashSet = s.chars().collect(); + ss.len() as i32 } } ``` diff --git a/solution/2700-2799/2716.Minimize String Length/README_EN.md b/solution/2700-2799/2716.Minimize String Length/README_EN.md index add6435b9779f..2bfbe16465d73 100644 --- a/solution/2700-2799/2716.Minimize String Length/README_EN.md +++ b/solution/2700-2799/2716.Minimize String Length/README_EN.md @@ -133,46 +133,13 @@ function minimizedStringLength(s: string): number { #### Rust -```rust -use std::collections::HashMap; - -impl Solution { - pub fn minimized_string_length(s: String) -> i32 { - let mut hash = HashMap::new(); - - for c in s.chars() { - hash.insert(c, true); - } - - hash.len() as i32 - } -} -``` - - - - - - - -### Solution 2 - - - -#### Rust - ```rust use std::collections::HashSet; impl Solution { pub fn minimized_string_length(s: String) -> i32 { - let mut set = HashSet::new(); - - for c in s.chars() { - set.insert(c); - } - - set.len() as i32 + let ss: HashSet = s.chars().collect(); + ss.len() as i32 } } ``` diff --git a/solution/2700-2799/2716.Minimize String Length/Solution.rs b/solution/2700-2799/2716.Minimize String Length/Solution.rs index afcc3dc816b51..7be19a758b840 100644 --- a/solution/2700-2799/2716.Minimize String Length/Solution.rs +++ b/solution/2700-2799/2716.Minimize String Length/Solution.rs @@ -1,13 +1,8 @@ -use std::collections::HashMap; +use std::collections::HashSet; impl Solution { pub fn minimized_string_length(s: String) -> i32 { - let mut hash = HashMap::new(); - - for c in s.chars() { - hash.insert(c, true); - } - - hash.len() as i32 + let ss: HashSet = s.chars().collect(); + ss.len() as i32 } } diff --git a/solution/2700-2799/2716.Minimize String Length/Solution2.rs b/solution/2700-2799/2716.Minimize String Length/Solution2.rs deleted file mode 100644 index 642a8a0079ebb..0000000000000 --- a/solution/2700-2799/2716.Minimize String Length/Solution2.rs +++ /dev/null @@ -1,13 +0,0 @@ -use std::collections::HashSet; - -impl Solution { - pub fn minimized_string_length(s: String) -> i32 { - let mut set = HashSet::new(); - - for c in s.chars() { - set.insert(c); - } - - set.len() as i32 - } -} diff --git a/solution/2700-2799/2717.Semi-Ordered Permutation/README.md b/solution/2700-2799/2717.Semi-Ordered Permutation/README.md index f5f0573110661..900207f8cb85e 100644 --- a/solution/2700-2799/2717.Semi-Ordered Permutation/README.md +++ b/solution/2700-2799/2717.Semi-Ordered Permutation/README.md @@ -177,64 +177,20 @@ function semiOrderedPermutation(nums: number[]): number { ```rust impl Solution { pub fn semi_ordered_permutation(nums: Vec) -> i32 { - let mut i = 0; - let mut j = 0; - let mut n = nums.len(); + let n = nums.len(); + let (mut i, mut j) = (0, 0); - for idx in 0..n { - if nums[idx] == 1 { - i = idx; + for k in 0..n { + if nums[k] == 1 { + i = k; } - if nums[idx] == (n as i32) { - j = idx; + if nums[k] == (n as i32) { + j = k; } } - let mut ans = i - 1 + n - j; - if i > j { - ans = i - 1 + n - j - 1; - } - - ans as i32 - } -} -``` - - - - - - - -### 方法二 - - - -#### Rust - -```rust -impl Solution { - pub fn semi_ordered_permutation(nums: Vec) -> i32 { - let n = nums.len(); - let i = nums - .iter() - .enumerate() - .find(|&(_, &v)| v == 1) - .map(|(i, _)| i) - .unwrap(); - let j = nums - .iter() - .enumerate() - .find(|&(_, &v)| v == (n as i32)) - .map(|(i, _)| i) - .unwrap(); - - let mut ans = i - 1 + n - j; - if i > j { - ans = i - 1 + n - j - 1; - } - - ans as i32 + let k = if i < j { 1 } else { 2 }; + (i + n - j - k) as i32 } } ``` diff --git a/solution/2700-2799/2717.Semi-Ordered Permutation/README_EN.md b/solution/2700-2799/2717.Semi-Ordered Permutation/README_EN.md index bcfad92f83932..ed957b97ed86f 100644 --- a/solution/2700-2799/2717.Semi-Ordered Permutation/README_EN.md +++ b/solution/2700-2799/2717.Semi-Ordered Permutation/README_EN.md @@ -175,64 +175,20 @@ function semiOrderedPermutation(nums: number[]): number { ```rust impl Solution { pub fn semi_ordered_permutation(nums: Vec) -> i32 { - let mut i = 0; - let mut j = 0; - let mut n = nums.len(); + let n = nums.len(); + let (mut i, mut j) = (0, 0); - for idx in 0..n { - if nums[idx] == 1 { - i = idx; + for k in 0..n { + if nums[k] == 1 { + i = k; } - if nums[idx] == (n as i32) { - j = idx; + if nums[k] == (n as i32) { + j = k; } } - let mut ans = i - 1 + n - j; - if i > j { - ans = i - 1 + n - j - 1; - } - - ans as i32 - } -} -``` - - - - - - - -### Solution 2 - - - -#### Rust - -```rust -impl Solution { - pub fn semi_ordered_permutation(nums: Vec) -> i32 { - let n = nums.len(); - let i = nums - .iter() - .enumerate() - .find(|&(_, &v)| v == 1) - .map(|(i, _)| i) - .unwrap(); - let j = nums - .iter() - .enumerate() - .find(|&(_, &v)| v == (n as i32)) - .map(|(i, _)| i) - .unwrap(); - - let mut ans = i - 1 + n - j; - if i > j { - ans = i - 1 + n - j - 1; - } - - ans as i32 + let k = if i < j { 1 } else { 2 }; + (i + n - j - k) as i32 } } ``` diff --git a/solution/2700-2799/2717.Semi-Ordered Permutation/Solution.rs b/solution/2700-2799/2717.Semi-Ordered Permutation/Solution.rs index c3cc4915d6a81..03d1ccba29ee8 100644 --- a/solution/2700-2799/2717.Semi-Ordered Permutation/Solution.rs +++ b/solution/2700-2799/2717.Semi-Ordered Permutation/Solution.rs @@ -1,23 +1,18 @@ impl Solution { pub fn semi_ordered_permutation(nums: Vec) -> i32 { - let mut i = 0; - let mut j = 0; - let mut n = nums.len(); + let n = nums.len(); + let (mut i, mut j) = (0, 0); - for idx in 0..n { - if nums[idx] == 1 { - i = idx; + for k in 0..n { + if nums[k] == 1 { + i = k; } - if nums[idx] == (n as i32) { - j = idx; + if nums[k] == (n as i32) { + j = k; } } - let mut ans = i - 1 + n - j; - if i > j { - ans = i - 1 + n - j - 1; - } - - ans as i32 + let k = if i < j { 1 } else { 2 }; + (i + n - j - k) as i32 } } diff --git a/solution/2700-2799/2717.Semi-Ordered Permutation/Solution2.rs b/solution/2700-2799/2717.Semi-Ordered Permutation/Solution2.rs deleted file mode 100644 index 2518893fa692a..0000000000000 --- a/solution/2700-2799/2717.Semi-Ordered Permutation/Solution2.rs +++ /dev/null @@ -1,24 +0,0 @@ -impl Solution { - pub fn semi_ordered_permutation(nums: Vec) -> i32 { - let n = nums.len(); - let i = nums - .iter() - .enumerate() - .find(|&(_, &v)| v == 1) - .map(|(i, _)| i) - .unwrap(); - let j = nums - .iter() - .enumerate() - .find(|&(_, &v)| v == (n as i32)) - .map(|(i, _)| i) - .unwrap(); - - let mut ans = i - 1 + n - j; - if i > j { - ans = i - 1 + n - j - 1; - } - - ans as i32 - } -}