This commit is contained in:
parent
510710890d
commit
c5540e8fa6
1 changed files with 26 additions and 0 deletions
|
|
@ -0,0 +1,26 @@
|
||||||
|
# https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinct
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def minimumOperations(self, nums: List[int]) -> int:
|
||||||
|
result = 0
|
||||||
|
cnt = {}
|
||||||
|
left, right = 0, 0
|
||||||
|
while right < len(nums):
|
||||||
|
curr_num = nums[right]
|
||||||
|
cnt[curr_num] = cnt.get(curr_num, 0) + 1
|
||||||
|
while cnt[curr_num] > 1:
|
||||||
|
result += 1
|
||||||
|
pops = len(nums) - left
|
||||||
|
if pops > 3:
|
||||||
|
pops = 3
|
||||||
|
for _ in range(pops):
|
||||||
|
if left <= right:
|
||||||
|
cnt[nums[left]] -= 1
|
||||||
|
left += 1
|
||||||
|
if left > right:
|
||||||
|
right = left - 1
|
||||||
|
right += 1
|
||||||
|
return result
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue