aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-04-08 20:39:58 -0600
committerJack Sangdahl <[email protected]>2025-04-08 20:39:58 -0600
commit792ebab379f6802b0d63c67eae2e8772066ffe28 (patch)
tree95f4a832adaa2fc36c680baa2daa4bf6eb59ceae
parent66e44e00b92d3959ec640525055d652c3d3d7756 (diff)
downloadleetcode-792ebab379f6802b0d63c67eae2e8772066ffe28.tar.gz
leetcode-792ebab379f6802b0d63c67eae2e8772066ffe28.zip
3375. minimum operations to make array values equal to k
-rw-r--r--min_ops_arr_eq_k.c35
-rw-r--r--min_ops_arr_eq_k.py37
2 files changed, 72 insertions, 0 deletions
diff --git a/min_ops_arr_eq_k.c b/min_ops_arr_eq_k.c
new file mode 100644
index 0000000..19bc647
--- /dev/null
+++ b/min_ops_arr_eq_k.c
@@ -0,0 +1,35 @@
+// 3375. Minimum Operations to Make Array Values Equal to K
+#include "leetcode.h"
+
+/*
+ * you are given an integer array 'nums' and an integer 'k'. an integer 'h' is
+ * called valid if all values in the array that are strictly greater than 'h'
+ * are identical. for example, if 'nums = [10, 8, 10, 8]', a valid integer is 'h
+ * = 9' because all 'nums[i] > 9' are equal to 10, but 5 is not a valid integer.
+ * you are allowed to perform the following operation on 'nums': select an
+ * integer 'h' that is valid for the current value in 'nums', for each index 'i'
+ * where 'nums[i] > h', set 'nums[i]' to 'h'. return the minimum number of
+ * operations required to make every element in 'nums' equal to 'k'. if it is
+ * impossible, return -1
+ */
+
+int minOperations(int *nums, int numsSize, int k) {
+ ulong bits[2] = {0};
+ int distinct = 0;
+ for (int i = 0; i < numsSize; i++) {
+ if (nums[i] < k)
+ return -1;
+ if (!(bits[nums[i] / 64] & (ulong)1 << (nums[i] % 64)) && nums[i] > k) {
+ distinct++;
+ bits[nums[i] / 64] |= (ulong)1 << (nums[i] % 64);
+ }
+ }
+ return distinct;
+}
+
+int main() {
+ int n1[] = {5, 2, 5, 4, 5}, n2[] = {2, 1, 2}, n3[] = {9, 7, 5, 3};
+ printf("%d\n", minOperations(n1, ARRAY_SIZE(n1), 2)); // expect: 2
+ printf("%d\n", minOperations(n2, ARRAY_SIZE(n2), 2)); // expect: -1
+ printf("%d\n", minOperations(n3, ARRAY_SIZE(n3), 1)); // expect: 4
+}
diff --git a/min_ops_arr_eq_k.py b/min_ops_arr_eq_k.py
new file mode 100644
index 0000000..67ff36e
--- /dev/null
+++ b/min_ops_arr_eq_k.py
@@ -0,0 +1,37 @@
+# 3375. Minimum Operations to Make Array Values Equal to K
+
+"""
+you are given an integer array 'nums' and an integer 'k'. an integer 'h' is
+called valid if all values in the array that are strictly greater than 'h'
+are identical. for example, if 'nums = [10, 8, 10, 8]', a valid integer is 'h
+= 9' because all 'nums[i] > 9' are equal to 10, but 5 is not a valid integer.
+you are allowed to perform the following operation on 'nums': select an
+integer 'h' that is valid for the current value in 'nums', for each index 'i'
+where 'nums[i] > h', set 'nums[i]' to 'h'. return the minimum number of
+operations required to make every element in 'nums' equal to 'k'. if it is
+impossible, return -1
+"""
+
+
+class Solution(object):
+ def minOperations(self, nums, k):
+ """
+ :type nums: List[int]
+ :type k: int
+ :rtype: int
+ """
+ distinct, bits = 0, 101
+ for i in nums:
+ distinct |= 1 << i
+ bits = min(bits, i)
+ if bits < k:
+ return -1
+ cnt = distinct.bit_count()
+ return cnt - 1 if bits == k else cnt
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.minOperations(nums=[5, 2, 5, 4, 5], k=2))
+ print(obj.minOperations(nums=[2, 1, 2], k=2))
+ print(obj.minOperations(nums=[9, 7, 5, 3], k=1))