From 81db191e08ce5d488bca5e25a61de763a1f54818 Mon Sep 17 00:00:00 2001 From: Alexei Date: Wed, 3 Jan 2024 20:46:29 -0500 Subject: [PATCH] Min Operations --- lc/Python/Jan-challenge/minOperations.py | 51 ++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 lc/Python/Jan-challenge/minOperations.py diff --git a/lc/Python/Jan-challenge/minOperations.py b/lc/Python/Jan-challenge/minOperations.py new file mode 100644 index 00000000..2165fab1 --- /dev/null +++ b/lc/Python/Jan-challenge/minOperations.py @@ -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])) \ No newline at end of file