Skip to content

Commit

Permalink
Min Operations
Browse files Browse the repository at this point in the history
  • Loading branch information
BravesDevs committed Jan 4, 2024
1 parent 92a01c2 commit 81db191
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions lc/Python/Jan-challenge/minOperations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from collections import defaultdict

class Sol:
def minOperations(self, nums):
hashMap = defaultdict(int)
originalMap = {}

for i in nums:
hashMap[i] += 1

originalMap = hashMap.copy()
res = 0
prev = 0
operationsCount = {}

for i in hashMap.keys():
while hashMap[i] >= 1:
if hashMap[i] in operationsCount:
res += operationsCount[hashMap[i]]
hashMap[i] = 0
elif hashMap[i] == 1:
return -1
elif hashMap[i] % 3 == 0:
res += hashMap[i] // 3
hashMap[i] %= 3
elif (hashMap[i] - 1) % 3 == 0 and hashMap[i] > 4:
count = ((hashMap[i] - 1) // 3) - 1
res += count
hashMap[i] -= count * 3
elif 2 <= hashMap[i] <= 4:
if hashMap[i] % 2 == 0:
res += hashMap[i] // 2
hashMap[i] %= 2
else:
res += hashMap[i] // 3
hashMap[i] %= 3
else:
res += hashMap[i] // 3
hashMap[i] %= 3
res += hashMap[i] // 2
hashMap[i] %= 2

if hashMap[i] not in operationsCount:
operationsCount[originalMap[i]] = res - prev
prev = res

return res


sln = Sol()
print(sln.minOperations([2,3,3,2,2,4,2,3,4]))

0 comments on commit 81db191

Please sign in to comment.