From 715e4a62f2b3f81a46ff485da0bd3fa295af4d25 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 8 Apr 2025 06:09:21 +0800 Subject: [PATCH 1/2] feat: update solution to lc problem: No.3394 --- .../README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/README.md b/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/README.md index 2a81bc3f1b9ec..22457b69a235f 100644 --- a/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/README.md +++ b/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/README.md @@ -122,10 +122,9 @@ class Solution { public int minimumOperations(int[] nums) { Set s = new HashSet<>(); for (int i = nums.length - 1; i >= 0; --i) { - if (s.contains(nums[i])) { + if (!s.add(nums[i])) { return i / 3 + 1; } - s.add(nums[i]); } return 0; } From 1e9b18d1236cd382cb525302093799249120ace2 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 8 Apr 2025 06:11:55 +0800 Subject: [PATCH 2/2] Add Rust solution and optimize Java solution. --- .../README.md | 18 ++++++++++++++++ .../README_EN.md | 21 +++++++++++++++++-- .../Solution.java | 3 +-- .../Solution.rs | 13 ++++++++++++ 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.rs diff --git a/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/README.md b/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/README.md index 22457b69a235f..028509821ac18 100644 --- a/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/README.md +++ b/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/README.md @@ -179,6 +179,24 @@ function minimumOperations(nums: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashSet; + +impl Solution { + pub fn minimum_operations(nums: Vec) -> i32 { + let mut s = HashSet::new(); + for i in (0..nums.len()).rev() { + if !s.insert(nums[i]) { + return (i / 3) as i32 + 1; + } + } + 0 + } +} +``` + diff --git a/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/README_EN.md b/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/README_EN.md index 0bb441f86095b..f4290fac124df 100644 --- a/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/README_EN.md +++ b/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/README_EN.md @@ -118,10 +118,9 @@ class Solution { public int minimumOperations(int[] nums) { Set s = new HashSet<>(); for (int i = nums.length - 1; i >= 0; --i) { - if (s.contains(nums[i])) { + if (!s.add(nums[i])) { return i / 3 + 1; } - s.add(nums[i]); } return 0; } @@ -176,6 +175,24 @@ function minimumOperations(nums: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashSet; + +impl Solution { + pub fn minimum_operations(nums: Vec) -> i32 { + let mut s = HashSet::new(); + for i in (0..nums.len()).rev() { + if !s.insert(nums[i]) { + return (i / 3) as i32 + 1; + } + } + 0 + } +} +``` + diff --git a/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.java b/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.java index 7867529603680..39ad4381c00b7 100644 --- a/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.java +++ b/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.java @@ -2,10 +2,9 @@ class Solution { public int minimumOperations(int[] nums) { Set s = new HashSet<>(); for (int i = nums.length - 1; i >= 0; --i) { - if (s.contains(nums[i])) { + if (!s.add(nums[i])) { return i / 3 + 1; } - s.add(nums[i]); } return 0; } diff --git a/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.rs b/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.rs new file mode 100644 index 0000000000000..f342802788c2d --- /dev/null +++ b/solution/3300-3399/3396.Minimum Number of Operations to Make Elements in Array Distinct/Solution.rs @@ -0,0 +1,13 @@ +use std::collections::HashSet; + +impl Solution { + pub fn minimum_operations(nums: Vec) -> i32 { + let mut s = HashSet::new(); + for i in (0..nums.len()).rev() { + if !s.insert(nums[i]) { + return (i / 3) as i32 + 1; + } + } + 0 + } +} \ No newline at end of file