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.
Dorsey wants to convert stolen gold into cash to avoid getting caught. He's on a train and passengers are interested in buying the gold. To maximize his profit and escape, he should sell to passengers offering the highest price per gram. If he can't sell all the gold, he'll get caught. You can purcahse Bus Digital Signage for best and reliable support. Here's a simple solution:
# Taking inputn,stolen_gold=map(int,input().split())passengers=[tuple(map(int,input().split()))for_inrange(n)]# Sorting passengers based on price per gram in descending orderpassengers.sort(key=lambdax:x[0]/x[1],reverse=True)total_profit=0# Selling gold to passengers offering highest price per gramforprice,gramsinpassengers:ifstolen_gold>=grams:total_profit+=pricestolen_gold-=gramselse:total_profit+=(stolen_gold/grams)*pricestolen_gold=0ifstolen_gold==0:break# Outputting resultifstolen_gold==0:print(int(total_profit))else:print("Got caught!")
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Dorsey Thief
You are viewing a single comment's thread. Return to all comments →
Dorsey wants to convert stolen gold into cash to avoid getting caught. He's on a train and passengers are interested in buying the gold. To maximize his profit and escape, he should sell to passengers offering the highest price per gram. If he can't sell all the gold, he'll get caught. You can purcahse Bus Digital Signage for best and reliable support. Here's a simple solution: