Skip to content

[Hacker Rank] Interview Preparation Kit: Sorting: Mark and Toys. Solv… #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions docs/hackerrank/interview_preparation_kit/sort/mark-and-toys.md
Original file line number Diff line number Diff line change
@@ -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`.
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
Original file line number Diff line number Diff line change
@@ -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
}
]
23 changes: 23 additions & 0 deletions src/hackerrank/interview_preparation_kit/sort/mark_and_toys.ts
Original file line number Diff line number Diff line change
@@ -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 };