diff --git a/docs/hackerrank/interview_preparation_kit/sort/mark-and-toys.md b/docs/hackerrank/interview_preparation_kit/sort/mark-and-toys.md new file mode 100644 index 00000000..5de4e7e4 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/sort/mark-and-toys.md @@ -0,0 +1,68 @@ +# [Mark and Toys](https://www.hackerrank.com/challenges/mark-and-toys) + +You are Mark's best friend and have to help him buy as many toys as possible. + +- Difficulty: `#easy` +- Category: `#ProblemSolvingBasic` `#greedy` `#sorting` + +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 + +The budget is `7` units of currency. He can buy items that cost `[1, 2, 3]` +for `6`, or `[3, 4]`, for units. +The maximum is `3` 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, `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 \leq n \leq 10^5 $ +- $ 1 \leq k \leq 10^9 $ +- $ 1 \leq prices[i] \leq 10^9 $ + +A toy can't be bought multiple times. + +## Sample Input + +```text +7 50 +1 12 5 111 200 1000 10 +``` + +## Sample Output + +```text +4 +``` + +## Explanation + +He can buy only `4` toys at most. +These toys have the following prices: `1, 12, 5, 10`. diff --git a/src/hackerrank/interview_preparation_kit/sort/mark_and_toys.test.ts b/src/hackerrank/interview_preparation_kit/sort/mark_and_toys.test.ts new file mode 100644 index 00000000..cdbf5684 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/sort/mark_and_toys.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from '@jest/globals'; + +import { maximumToys } from './mark_and_toys'; +import TEST_CASES from './mark_and_toys.testcases.json'; + +describe('maximumToys', () => { + it('maximumToys test cases', () => { + expect.assertions(3); + + TEST_CASES.forEach((test) => { + const result = maximumToys(test.prices, test.budget); + + expect(result).toStrictEqual(test.expected); + }); + }); +}); diff --git a/src/hackerrank/interview_preparation_kit/sort/mark_and_toys.testcases.json b/src/hackerrank/interview_preparation_kit/sort/mark_and_toys.testcases.json new file mode 100644 index 00000000..9acd3f96 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/sort/mark_and_toys.testcases.json @@ -0,0 +1,20 @@ +[ + { + "title": "Sample Test Case 0", + "budget": 50, + "prices": [50, 1, 12, 5, 111, 200, 1000, 10], + "expected": 4 + }, + { + "title": "Sample Test Case 1", + "budget": 7, + "prices": [1, 2, 3, 4], + "expected": 3 + }, + { + "title": "Sample Test Case 2", + "budget": 15, + "prices": [3, 7, 2, 9, 4], + "expected": 3 + } +] diff --git a/src/hackerrank/interview_preparation_kit/sort/mark_and_toys.ts b/src/hackerrank/interview_preparation_kit/sort/mark_and_toys.ts new file mode 100644 index 00000000..af165334 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/sort/mark_and_toys.ts @@ -0,0 +1,23 @@ +/** + * @link Problem definition [[docs/hackerrank/interview_preparation_kit/sort/mark-and-toys.md]] + */ + +export function maximumToys(prices: number[], k: number): number { + const group = [...prices]; + group.sort((a: number, b: number) => a - b); + + let budget = k; + const shoppingCart: number[] = []; + + while (group.length > 0 && budget >= 0) { + const currentItem = group.shift(); + budget -= currentItem!; + if (budget >= 0) { + shoppingCart.push(currentItem!); + } + } + + return shoppingCart.length; +} + +export default { maximumToys };