Skip to content

Commit

Permalink
Merge pull request #458 from sir-gon/feature/mark-and-toys
Browse files Browse the repository at this point in the history
[Hacker Rank] Interview Preparation Kit: Sorting: Mark and Toys. Solv…
  • Loading branch information
sir-gon authored Aug 28, 2024
2 parents 6eb4114 + 66c3db0 commit 03ab8f7
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
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 };

0 comments on commit 03ab8f7

Please sign in to comment.