Last active
June 12, 2020 06:21
-
-
Save wenweixu/16b9d2b3927102126683dce2afca3e00 to your computer and use it in GitHub Desktop.
Hackerrank Greedy Florist Python solution
This file contains 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
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is indeed needed..Try running your code without that "If" statement