-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmark_and_toys.py
87 lines (73 loc) · 2.19 KB
/
mark_and_toys.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
##############################
# Mark and Toys - just loops #
# 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 and Mark has to spend, he can buy items for , or for units of currency. He would choose the first group of 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, and , the number of priced toys and the amount Mark has to spend.
# The next line contains space-separated integers
#
# Constraints
#
#
#
# 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 toys at most. These toys have the following prices: .
#!/bin/python
import math
import os
import random
import re
import sys
# Complete the maximumToys function below.
def maximumToys(prices, k):
list.sort(prices)
sum = 0
cnt = 0
p = 0
for i in range(len(prices)-1):
if prices[i] > k:
p = i-1
elif prices[i] < k:
p = len(prices)-1
for x in range(0,p):
sum = sum + prices[x]
if sum < k:
cnt += 1
x +=1
else:
break
return cnt
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
nk = raw_input().split()
n = int(nk[0])
k = int(nk[1])
prices = map(int, raw_input().rstrip().split())
result = maximumToys(prices, k)
fptr.write(str(result) + '\n')
fptr.close()