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() |
Author
wenweixu
commented
Feb 19, 2020
via email
Hi, thank you so much for your comments. I looked at it and was quite
confused as well why I had the second condition. It turns out that the
second condition is indeed redundant and should be removed.
I will modify my code accordingly, thanks, -Wenwei
…On Wed, Feb 19, 2020 at 11:41 AM Muzammil-Elahi ***@***.***> wrote:
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.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/16b9d2b3927102126683dce2afca3e00?email_source=notifications&email_token=AEFYVC6MEQKD2MXXVZKGCMDRDWDOTA5CNFSM4KYAKKH2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAGCH2I#gistcomment-3182500>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEFYVCZYKGXIULMWYIUR7U3RDWDOTANCNFSM4KYAKKHQ>
.
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