## [HackerRank] Sorting : Mark and Toys (Python) 본문

Programming/코딩 1일 1문제

### [HackerRank] Sorting : Mark and Toys (Python)

사용자 솜씨좋은장씨 2020. 3. 25. 15:29

Mark and Jane are very happy after having their first child. Their son loves toys, so Mark wants to buy some. There are a number of different toys lying in front of him, tagged with their prices. Mark has only a certain amount to spend, and he wants to maximize the number of toys he buys with this money.

Given a list of prices and an amount to spend, what is the maximum number of toys Mark can buy? For example, if prices = [ 1, 2, 3, 4 ]  and Mark has k = 7 to spend, he can buy items [ 1, 2, 3 ] for 6, or [ 3, 4 ] for 7 units of currency. He would choose the first group of 3 items.

Function Description

Complete the function maximumToys in the editor below. It should return an integer representing the maximum number of toys Mark can purchase.

maximumToys has the following parameter(s):

• prices: an array of integers representing toy prices
• k: an integer, Mark's budget

Input Format

The first line contains two integers, n and k, the number of priced toys and the amount Mark has to spend.
The next line contains n space-separated integers prices [ ]

Constraints

1 <= n <= 10^5

1 <= k <= 10^9
1 <= prices [ i  ] <= 10^9
A toy can't be bought multiple times.

Output Format

An integer that denotes the maximum number of toys Mark can buy for his son.

Sample Input

``````7 50
1 12 5 111 200 1000 10``````

Sample Output

``4``

Explanation

He can buy only 4 toys at most. These toys have the following prices: 1, 12, 5, 10.

Solution

``````#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the maximumToys function below.
def maximumToys(prices, k):
sort_prices = sorted(prices)

count = 0

for i in range(len(sort_prices)):
k = k - sort_prices[i]

if k < 0 :
break
count = count + 1

return count``````

#### 'Programming > 코딩 1일 1문제' 카테고리의 다른 글

 [leetCode] 557. Reverse Words in a String III (Python)  (0) 2020.03.27 2020.03.26 2020.03.25 2020.03.24 2020.03.23 2020.03.22