8000 Create Minimum Number of Operations to Make Elements in Array Distinc… · erjan/coding_exercises@0153742 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0153742

Browse files
authored
Create Minimum Number of Operations to Make Elements in Array Distinct.py
1 parent 71fd6a0 commit 0153742

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
You are given an integer array nums. You need to ensure that the elements in the array are distinct. To achieve this, you can perform the following operation any number of times:
3+
4+
Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.
5+
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.
6+
'''
7+
#my solution
8+
class Solution:
9+
def minimumOperations(self, nums: List[int]) -> int:
10+
n = len(nums)
11+
12+
13+
res = 0
14+
15+
while len(set(nums))!= len(nums) :
16+
17+
if len(nums)>3:
18+
nums = nums[3:]
19+
res +=1
20+
else:
21+
res+=1
22+
break
23+
return res
24+
25+
------------------------------------------------------------------
26+
#another solution
27+
class Solution:
28+
def minimumOperations(self, nums: List[int]) -> int:
29+
count=0
30+
while len(nums)>len(set(nums)):
31+
nums=nums[3:]
32+
count+=1
33+
return count
34+
35+

0 commit comments

Comments
 (0)
0