Soluție HackerRank pentru Mark and Toys. Include cerința formatată, exemple, explicația pașilor și cod sursă.

  • Problemă: Mark and Toys

Cerinta completa

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 toy prices and an amount to spend, determine the maximum number of gifts he can buy.

Note Each toy can be purchased only once.

Example
[Expresie matematică indisponibilă în copia arhivată]
[Expresie matematică indisponibilă în copia arhivată]

The budget is [Expresie matematică indisponibilă în copia arhivată] units of currency. He can buy items that cost [Expresie matematică indisponibilă în copia arhivată] for [Expresie matematică indisponibilă în copia arhivată], or [Expresie matematică indisponibilă în copia arhivată] for [Expresie matematică indisponibilă în copia arhivată] units. The maximum is [Expresie matematică indisponibilă în copia arhivată] items.

Function Description

Complete the function maximumToys in the editor below.

maximumToys has the following parameter(s):

  • int prices[n]: the toy prices
  • int k: Mark’s budget

Returns

  • int: the maximum number of toys

Input Format

The first line contains two integers, [Expresie matematică indisponibilă în copia arhivată] and [Expresie matematică indisponibilă în copia arhivată], the number of priced toys and the amount Mark has to spend.
The next line contains [Expresie matematică indisponibilă în copia arhivată] space-separated integers [Expresie matematică indisponibilă în copia arhivată]

Constraints

[Expresie matematică indisponibilă în copia arhivată]
[Expresie matematică indisponibilă în copia arhivată]
[Expresie matematică indisponibilă în copia arhivată]
A toy can’t be bought multiple times.

Sample Input

7 50
1 12 5 111 200 1000 10

Sample Output

4

Explanation

He can buy only [Expresie matematică indisponibilă în copia arhivată] toys at most. These toys have the following prices: [Expresie matematică indisponibilă în copia arhivată].


Limbajul de programare folosit: java8

Cod:

import java.util.Arrays;
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] prices = new int[n];
		for (int i = 0; i < prices.length; i++) {
			prices[i] = sc.nextInt();
		}

		System.out.println(solve(prices, k));

		sc.close();
	}

	static int solve(int[] prices, int k) {
		Arrays.sort(prices);

		for (int i = 0, sum = 0;; sum += prices[i], i++) {
			if (sum > k) {
				return i - 1;
			} else if (i == prices.length) {
				return i;
			}
		}
	}
}

Scor obtinut: 1.0

Submission ID: 464603214

Link challenge: https://www.hackerrank.com/challenges/mark-and-toys/problem

Mark and Toys