diff --git a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README.md b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README.md index 5bbd629058f59..4489d067bfff1 100644 --- a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README.md +++ b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README.md @@ -240,6 +240,34 @@ function minChanges(nums: number[], k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn min_changes(nums: Vec, k: i32) -> i32 { + let n = nums.len(); + let mut d = vec![0; (k + 2) as usize]; + for i in 0..n / 2 { + let x = nums[i].min(nums[n - i - 1]); + let y = nums[i].max(nums[n - i - 1]); + d[0] += 1; + d[(y - x) as usize] -= 1; + d[(y - x + 1) as usize] += 1; + let idx = (y.max(k - x) + 1) as usize; + d[idx] -= 1; + d[idx] += 2; + } + let mut ans = n as i32; + let mut s = 0; + for x in d { + s += x; + ans = ans.min(s); + } + ans + } +} +``` + diff --git a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README_EN.md b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README_EN.md index 242423dcb6b62..66e84e72e6dff 100644 --- a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README_EN.md +++ b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README_EN.md @@ -238,6 +238,34 @@ function minChanges(nums: number[], k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn min_changes(nums: Vec, k: i32) -> i32 { + let n = nums.len(); + let mut d = vec![0; (k + 2) as usize]; + for i in 0..n / 2 { + let x = nums[i].min(nums[n - i - 1]); + let y = nums[i].max(nums[n - i - 1]); + d[0] += 1; + d[(y - x) as usize] -= 1; + d[(y - x + 1) as usize] += 1; + let idx = (y.max(k - x) + 1) as usize; + d[idx] -= 1; + d[idx] += 2; + } + let mut ans = n as i32; + let mut s = 0; + for x in d { + s += x; + ans = ans.min(s); + } + ans + } +} +``` + diff --git a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.rs b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.rs new file mode 100644 index 0000000000000..3efe91dcd7230 --- /dev/null +++ b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/Solution.rs @@ -0,0 +1,23 @@ +impl Solution { + pub fn min_changes(nums: Vec, k: i32) -> i32 { + let n = nums.len(); + let mut d = vec![0; (k + 2) as usize]; + for i in 0..n / 2 { + let x = nums[i].min(nums[n - i - 1]); + let y = nums[i].max(nums[n - i - 1]); + d[0] += 1; + d[(y - x) as usize] -= 1; + d[(y - x + 1) as usize] += 1; + let idx = (y.max(k - x) + 1) as usize; + d[idx] -= 1; + d[idx] += 2; + } + let mut ans = n as i32; + let mut s = 0; + for x in d { + s += x; + ans = ans.min(s); + } + ans + } +} diff --git a/solution/3200-3299/3225.Maximum Score From Grid Operations/images/rectangles.png b/solution/3200-3299/3225.Maximum Score From Grid Operations/images/rectangles.png deleted file mode 100644 index 6adb9f06edbad..0000000000000 Binary files a/solution/3200-3299/3225.Maximum Score From Grid Operations/images/rectangles.png and /dev/null differ diff --git a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README.md b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README.md index 04d6d0850a1dc..6eb95a6201682 100644 --- a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README.md +++ b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README.md @@ -140,6 +140,30 @@ function bitCount(i: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn min_changes(n: i32, k: i32) -> i32 { + if (n & k) != k { + -1 + } else { + (n ^ k).count_ones() as i32 + } + } +} +``` + +#### C# + +```cs +public class Solution { + public int MinChanges(int n, int k) { + return (n & k) != k ? -1 : BitOperations.PopCount((uint)(n ^ k)); + } +} +``` + diff --git a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README_EN.md b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README_EN.md index 891c5e63f9968..808d59dad5eff 100644 --- a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README_EN.md +++ b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README_EN.md @@ -137,6 +137,30 @@ function bitCount(i: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn min_changes(n: i32, k: i32) -> i32 { + if (n & k) != k { + -1 + } else { + (n ^ k).count_ones() as i32 + } + } +} +``` + +#### C# + +```cs +public class Solution { + public int MinChanges(int n, int k) { + return (n & k) != k ? -1 : BitOperations.PopCount((uint)(n ^ k)); + } +} +``` + diff --git a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.cs b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.cs new file mode 100644 index 0000000000000..ea2911fa339ed --- /dev/null +++ b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.cs @@ -0,0 +1,5 @@ +public class Solution { + public int MinChanges(int n, int k) { + return (n & k) != k ? -1 : BitOperations.PopCount((uint)(n ^ k)); + } +} diff --git a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.rs b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.rs new file mode 100644 index 0000000000000..e377c164b294f --- /dev/null +++ b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.rs @@ -0,0 +1,9 @@ +impl Solution { + pub fn min_changes(n: i32, k: i32) -> i32 { + if (n & k) != k { + -1 + } else { + (n ^ k).count_ones() as i32 + } + } +}