Sunday, February 3, 2019

Mark and Toys - Hacker Rank Solution

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: .

Mark and Toys - Hacker Rank Solution

Given that  in the constraints, we must use an O(nlogn) or better algorithm to pass all test cases. Mark wants to buy the greatest number of toys, so he purchases them in increasing order of price. Sort the array of prices then keep an item count and a total cost (or decrement the initial cash by prices[count]) as you iterate through the sorted list of prices.
Problem Setter's code:

C++

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int num, money,i,sum=0;
    cin>>num>>money;
    int cost[num];
    for (i=0;i<num;i++)
    {
        cin>>cost[i];
    }
    sort(cost,cost+num);
    i=0;
    while(cost[i]<money)
    {
        sum=sum+cost[i];
        if(sum>money) break;
        i++;
        
    }
    cout<<i;
    return 0;
}
Problem Tester's code:

Python 2

#!/usr/bin/python
    
N,K = map(int, raw_input().strip().split())
assert 1 <= N <= 10**5 and 1 <= K <= 10**9
lis = map(int, raw_input().strip().split())
for toy in lis:
    assert 1 <= toy <= 10**5
    
lis.sort()
    
count = 0
for i in range(N-1):
    K -= lis[i]
    if K < 0:
        print count
        exit(0)
    else:
        count += 1
    
print count

2 comments:

  1. import java.io.*;
    import java.util.*;
    import java.util.Arrays;
    public class Main {
    public static void main(String args[]) {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt(),k=sc.nextInt();
    int[] a=new int[n];
    for(int i=0; i<n; i++)
    {
    a[i]=sc.nextInt();
    }
    Arrays.sort(a);
    int sum=0, count=0;
    for(int i=0; i<n; i++)
    {
    sum=sum+a[i];
    if(sum<k)
    count++;
    }
    System.out.println(count); // your code here
    }
    }

    ReplyDelete

Powered by Blogger.