Sorting: Mark and Toys

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the maximumToys function below.
def maximumToys(prices, k):
    p=sorted(prices) ####直接用sorted(),如果用自己写的算法,runtime error
    count=0
    amount=0
    for price in p:
        amount+=price
        if amount>k:
            return count
        else:
            count+=1

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    nk = input().split()

    n = int(nk[0])

    k = int(nk[1])

    prices = list(map(int, input().rstrip().split()))

    result = maximumToys(prices, k)

    fptr.write(str(result) + '\n')

    fptr.close()
发布了163 篇原创文章 · 获赞 90 · 访问量 6285

猜你喜欢

转载自blog.csdn.net/weixin_45405128/article/details/104217838