Last active
May 19, 2023 02:26
-
-
Save Shaddyjr/bc2630b6f7e24162b201f2dc20fa5a49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# source: https://www.hackerrank.com/challenges/equal/problem | |
# video: https://youtu.be/Mbfs-4URYmg | |
def equal(arr): | |
rounds = float("inf") | |
min_val = min(arr) | |
# Go through each target value (min, min-1, min-2, ..., min-4) | |
for n in range(5): # O(5) = O(1) | |
target = min_val - n | |
operationCount = 0 | |
# Go through each value in arr... | |
for val in arr: # O(n) | |
# calculate difference b/t val and target | |
diff = val - target | |
# use modular math and add to operationCount | |
five_count = diff // 5 | |
two_count = (diff % 5) // 2 | |
one_count = (diff % 5) % 2 | |
operationCount += five_count + two_count + one_count | |
rounds = min(rounds, operationCount) | |
return rounds # Time Complexity = O(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment