From 4d869d3f71126fea92fd683c79d8145ab7fb8efe Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 10 Apr 2025 00:31:15 +0300 Subject: [PATCH] feat: update solutions to lc problem: No.3375 --- .../README.md | 23 +++++++++++++------ .../README_EN.md | 23 +++++++++++++------ .../Solution.js | 8 +++++++ .../Solution.ts | 10 +++----- 4 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.js diff --git a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README.md b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README.md index f1e73283c5c07..6450fffdd0f8e 100644 --- a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README.md +++ b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README.md @@ -176,16 +176,25 @@ func minOperations(nums []int, k int) int { ```ts function minOperations(nums: number[], k: number): number { - const s = new Set(); - let mi = Infinity; + const s = new Set([k]); for (const x of nums) { - if (x < k) { - return -1; - } + if (x < k) return -1; + s.add(x); + } + return s.size - 1; +} +``` + +#### JavaScript + +```js +function minOperations(nums, k) { + const s = new Set([k]); + for (const x of nums) { + if (x < k) return -1; s.add(x); - mi = Math.min(mi, x); } - return s.size - (mi === k ? 1 : 0); + return s.size - 1; } ``` diff --git a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README_EN.md b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README_EN.md index 2dd2f6dd1d718..3c00a12599575 100644 --- a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README_EN.md +++ b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README_EN.md @@ -174,16 +174,25 @@ func minOperations(nums []int, k int) int { ```ts function minOperations(nums: number[], k: number): number { - const s = new Set(); - let mi = Infinity; + const s = new Set([k]); for (const x of nums) { - if (x < k) { - return -1; - } + if (x < k) return -1; + s.add(x); + } + return s.size - 1; +} +``` + +#### JavaScript + +```js +function minOperations(nums, k) { + const s = new Set([k]); + for (const x of nums) { + if (x < k) return -1; s.add(x); - mi = Math.min(mi, x); } - return s.size - (mi === k ? 1 : 0); + return s.size - 1; } ``` diff --git a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.js b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.js new file mode 100644 index 0000000000000..e2ebea0fa1fe8 --- /dev/null +++ b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.js @@ -0,0 +1,8 @@ +function minOperations(nums, k) { + const s = new Set([k]); + for (const x of nums) { + if (x < k) return -1; + s.add(x); + } + return s.size - 1; +} diff --git a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.ts b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.ts index 652b303832721..4f22815234416 100644 --- a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.ts +++ b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.ts @@ -1,12 +1,8 @@ function minOperations(nums: number[], k: number): number { - const s = new Set(); - let mi = Infinity; + const s = new Set([k]); for (const x of nums) { - if (x < k) { - return -1; - } + if (x < k) return -1; s.add(x); - mi = Math.min(mi, x); } - return s.size - (mi === k ? 1 : 0); + return s.size - 1; }