Skip to content

Instantly share code, notes, and snippets.

@wenweixu
Last active June 12, 2020 06:21
Show Gist options
  • Save wenweixu/16b9d2b3927102126683dce2afca3e00 to your computer and use it in GitHub Desktop.
Save wenweixu/16b9d2b3927102126683dce2afca3e00 to your computer and use it in GitHub Desktop.
Hackerrank Greedy Florist Python solution
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the getMinimumCost function below.
def getMinimumCost(k, c):
c.sort(reverse=True)
cost = 0
previous_purchase = 0
for i in range(n):
cost += (previous_purchase +1) * c[i]
if (i+1)%k==0:
previous_purchase += 1
return cost
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
nk = input().split()
n = int(nk[0])
k = int(nk[1])
c = list(map(int, input().rstrip().split()))
minimumCost = getMinimumCost(k, c)
fptr.write(str(minimumCost) + '\n')
fptr.close()
@Irene-123
Copy link

Can I ask why you have these two conditions if (i+1)%k==0 and (i+1)//k>0? It sort of makes sense but I would like some clarification. Sorry for the inconvenience and thank you for your time.

It is indeed needed..Try running your code without that "If" statement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment