- Real or Not? NLP with Disaster Tweets
- 현직자와 함께하는 프로그래밍
- 인공지능 공모전
[HackerRank] Sorting : Mark and Toys (Python) 본문
[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.
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
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 [ i ]
1 <= n <= 10^5
1 <= k <= 10^9
1 <= prices [ i ] <= 10^9
A toy can't be bought multiple times.
An integer that denotes the maximum number of toys Mark can buy for his son.
7 50 1 12 5 111 200 1000 10
He can buy only 4 toys at most. These toys have the following prices: 1, 12, 5, 10.
#!/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|
|[HackerRank] String Manipulation : Alternating Characters (Python) (0)||2020.03.26|
|[HackerRank] Sorting : Mark and Toys (Python) (0)||2020.03.25|
|[leetCode] 929. Unique Email Addresses (Python) (0)||2020.03.24|
|[BaeKJoon] 10844번: 쉬운 계단수 (Python) (0)||2020.03.23|
|[HackerRank] Hash Tables : Ransom Note (Python) (0)||2020.03.22|