diff options
author | Jack Sangdahl <[email protected]> | 2025-04-07 21:15:38 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-04-07 21:15:48 -0600 |
commit | 3fdd56c0adaccc960214af925cdc5c19bf3d187a (patch) | |
tree | 528b37c78a834e01983ca893b96b8be6e833d6a6 | |
parent | 6c6072834b6854644ce87c4182a069d2f7358da7 (diff) | |
download | leetcode-3fdd56c0adaccc960214af925cdc5c19bf3d187a.tar.gz leetcode-3fdd56c0adaccc960214af925cdc5c19bf3d187a.zip |
3396. minimum number of operations to make elements in array distinct
-rw-r--r-- | min_num_op_elem_distinct.c | 30 | ||||
-rw-r--r-- | min_num_op_elem_distinct.py | 31 |
2 files changed, 61 insertions, 0 deletions
diff --git a/min_num_op_elem_distinct.c b/min_num_op_elem_distinct.c new file mode 100644 index 0000000..2ea72b5 --- /dev/null +++ b/min_num_op_elem_distinct.c @@ -0,0 +1,30 @@ +// 3396. Minimum Number of Operations to Make Elements in Array Distinct +#include "leetcode.h" + +/* + * you are given an integer array 'nums'. you need to ensure that the elements + * in the array are distinct. to achieve this, you can remove 3 elements from + * the beginning of the array. if the array has fewer than 3 elements, remove + * all remaining elements. note that an empty array is considered to have + * distinct elements. return the minimum number of operations needed to make the + * elements in the array distinct. + */ + +int minimumOperations(int *nums, int numsSize) { + char prev[101], max = -1; + memset(prev, -1, sizeof(prev)); + for (int i = 0; i < numsSize; i++) { + if (prev[nums[i]] != -1 && max < prev[nums[i]]) + max = prev[nums[i]]; + prev[nums[i]] = i; + } + return (int)(max + 1) / 3 + ((max + 1) % 3 > 0); +} + +int main() { + int n1[] = {1, 2, 3, 4, 2, 3, 3, 5, 7}, n2[] = {4, 5, 6, 4, 4}, + n3[] = {6, 7, 8, 9}; + printf("%d\n", minimumOperations(n1, ARRAY_SIZE(n1))); // expect: 2 + printf("%d\n", minimumOperations(n2, ARRAY_SIZE(n2))); // expect: 2 + printf("%d\n", minimumOperations(n3, ARRAY_SIZE(n3))); // expect: 0 +} diff --git a/min_num_op_elem_distinct.py b/min_num_op_elem_distinct.py new file mode 100644 index 0000000..a0015f9 --- /dev/null +++ b/min_num_op_elem_distinct.py @@ -0,0 +1,31 @@ +# 3396. Minimum Number of Operations to Make Elements in Array Distinct + +""" +you are given an integer array 'nums'. you need to ensure that the elements +in the array are distinct. to achieve this, you can remove 3 elements from +the beginning of the array. if the array has fewer than 3 elements, remove +all remaining elements. note that an empty array is considered to have +distinct elements. return the minimum number of operations needed to make the +elements in the array distinct. +""" + + +class Solution(object): + def minimumOperations(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + prev = [0] * 101 + for i in range(len(nums) - 1, -1, -1): + prev[nums[i]] += 1 + if prev[nums[i]] == 2: + return (i + 3) // 3 + return 0 + + +if __name__ == "__main__": + obj = Solution() + print(obj.minimumOperations(nums=[1, 2, 3, 4, 2, 3, 3, 5, 7])) + print(obj.minimumOperations(nums=[4, 5, 6, 4, 4])) + print(obj.minimumOperations(nums=[6, 7, 8, 9])) |