Skip to content

Commit 57b09ee

Browse files
committed
Add solution #3375
1 parent 0eafd86 commit 57b09ee

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,224 LeetCode solutions in JavaScript
1+
# 1,225 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1220,6 +1220,7 @@
12201220
3223|[Minimum Length of String After Operations](./solutions/3223-minimum-length-of-string-after-operations.js)|Medium|
12211221
3306|[Count of Substrings Containing Every Vowel and K Consonants II](./solutions/3306-count-of-substrings-containing-every-vowel-and-k-consonants-ii.js)|Medium|
12221222
3356|[Zero Array Transformation II](./solutions/3356-zero-array-transformation-ii.js)|Medium|
1223+
3375|[Minimum Operations to Make Array Values Equal to K](./solutions/3375-minimum-operations-to-make-array-values-equal-to-k.js)|Easy|
12231224
3392|[Count Subarrays of Length Three With a Condition](./solutions/3392-count-subarrays-of-length-three-with-a-condition.js)|Easy|
12241225
3394|[Check if Grid can be Cut into Sections](./solutions/3394-check-if-grid-can-be-cut-into-sections.js)|Medium|
12251226
3396|[Minimum Number of Operations to Make Elements in Array Distinct](./solutions/3396-minimum-number-of-operations-to-make-elements-in-array-distinct.js)|Easy|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 3375. Minimum Operations to Make Array Values Equal to K
3+
* https://leetcode.com/problems/minimum-operations-to-make-array-values-equal-to-k/
4+
* Difficulty: Easy
5+
*
6+
* You are given an integer array nums and an integer k.
7+
*
8+
* An integer h is called valid if all values in the array that are strictly greater than
9+
* h are identical.
10+
*
11+
* For example, if nums = [10, 8, 10, 8], a valid integer is h = 9 because all nums[i] > 9
12+
* are equal to 10, but 5 is not a valid integer.
13+
*
14+
* You are allowed to perform the following operation on nums:
15+
* - Select an integer h that is valid for the current values in nums.
16+
* - For each index i where nums[i] > h, set nums[i] to h.
17+
*
18+
* Return the minimum number of operations required to make every element in nums equal to k.
19+
* If it is impossible to make all elements equal to k, return -1.
20+
*/
21+
22+
/**
23+
* @param {number[]} nums
24+
* @param {number} k
25+
* @return {number}
26+
*/
27+
var minOperations = function(nums, k) {
28+
if (nums.some(x => x < k)) return -1;
29+
30+
const uniqueAbove = [...new Set(nums.filter(x => x > k))].sort((a, b) => b - a);
31+
if (uniqueAbove.length === 0) return 0;
32+
33+
let result = 0;
34+
let current = uniqueAbove.slice();
35+
36+
while (current.length > 0) {
37+
const threshold = current[0] - 1;
38+
current = current.filter(x => x <= threshold);
39+
result++;
40+
if (current.every(x => x === k)) break;
41+
}
42+
43+
return result;
44+
};

0 commit comments

Comments
 (0)