We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
No need for DP. Key insight is that adding to all colleagues except one is equivalent to taking away from only one colleague, with the modification of not "bottoming out" in cases where equality can be reached in fewer moves with bigger additions. For example [1, 5, 5] can reach equality in 4 add-2 moves at [9, 9, 9] ([1, 1, 1] if you take-away), but in only 3 total moves at [11, 11, 11] ([0, 0, 0] if you take-away). This is the reason for my offset param. The main logic gets the distance between each colleague and the minimum, and sums the multiples of 5, 2, and 1 necessary to equalize all colleagues at the minimum.
defequal(arr):defequalize(arr,offset):n_rounds=0least=min(arr)-offsetgaps=[x-leastforxinarr]forchocin(5,2,1):n_rounds+=sum(x// choc for x in gaps)gaps=[x%chocforxingaps]returnn_roundsreturnmin(equalize(arr,x)forxinrange(4))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Equal
You are viewing a single comment's thread. Return to all comments →
No need for DP. Key insight is that adding to all colleagues except one is equivalent to taking away from only one colleague, with the modification of not "bottoming out" in cases where equality can be reached in fewer moves with bigger additions. For example [1, 5, 5] can reach equality in 4 add-2 moves at [9, 9, 9] ([1, 1, 1] if you take-away), but in only 3 total moves at [11, 11, 11] ([0, 0, 0] if you take-away). This is the reason for my
offset
param. The main logic gets the distance between each colleague and the minimum, and sums the multiples of 5, 2, and 1 necessary to equalize all colleagues at the minimum.