일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- github
- PYTHON
- leetcode
- Real or Not? NLP with Disaster Tweets
- Kaggle
- 맥북
- ubuntu
- 캐치카페
- Git
- 프로그래머스
- dacon
- 금융문자분석경진대회
- Docker
- AI 경진대회
- 편스토랑
- SW Expert Academy
- Baekjoon
- hackerrank
- 프로그래머스 파이썬
- 우분투
- 백준
- 파이썬
- 편스토랑 우승상품
- 자연어처리
- ChatGPT
- 데이콘
- 코로나19
- programmers
- gs25
- 더현대서울 맛집
- Today
- Total
솜씨좋은장씨
[HackerRank] Sorting : Mark and Toys (Python) 본문
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 [ i ]
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 |
---|---|
[HackerRank] String Manipulation : Alternating Characters (Python) (0) | 2020.03.26 |
[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 |