Skip to content

Commit 03ab8f7

Browse files
authored
Merge pull request #458 from sir-gon/feature/mark-and-toys
[Hacker Rank] Interview Preparation Kit: Sorting: Mark and Toys. Solv…
2 parents 6eb4114 + 66c3db0 commit 03ab8f7

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# [Mark and Toys](https://www.hackerrank.com/challenges/mark-and-toys)
2+
3+
You are Mark's best friend and have to help him buy as many toys as possible.
4+
5+
- Difficulty: `#easy`
6+
- Category: `#ProblemSolvingBasic` `#greedy` `#sorting`
7+
8+
Mark and Jane are very happy after having their first child.
9+
Their son loves toys, so Mark wants to buy some.
10+
There are a number of different toys lying in front of him,
11+
tagged with their prices.
12+
Mark has only a certain amount to spend, and he wants to maximize
13+
the number of toys he buys with this money.
14+
Given a list of toy prices and an amount to spend,
15+
determine the maximum number of gifts he can buy.
16+
17+
**Note** Each toy can be purchased only once.
18+
19+
## Example
20+
21+
The budget is `7` units of currency. He can buy items that cost `[1, 2, 3]`
22+
for `6`, or `[3, 4]`, for units.
23+
The maximum is `3` items.
24+
25+
## Function Description
26+
27+
Complete the function maximumToys in the editor below.
28+
29+
maximumToys has the following parameter(s):
30+
31+
- `int prices[n]`: the toy prices
32+
- `int k`: Mark's budget
33+
34+
## Returns
35+
36+
- `int`: the maximum number of toys
37+
38+
## Input Format
39+
40+
The first line contains two integers, `n` and `k`, the number of priced toys
41+
and the amount Mark has to spend.
42+
The next line contains `n` space-separated integers `prices[i]`
43+
44+
## Constraints
45+
46+
- $ 1 \leq n \leq 10^5 $
47+
- $ 1 \leq k \leq 10^9 $
48+
- $ 1 \leq prices[i] \leq 10^9 $
49+
50+
A toy can't be bought multiple times.
51+
52+
## Sample Input
53+
54+
```text
55+
7 50
56+
1 12 5 111 200 1000 10
57+
```
58+
59+
## Sample Output
60+
61+
```text
62+
4
63+
```
64+
65+
## Explanation
66+
67+
He can buy only `4` toys at most.
68+
These toys have the following prices: `1, 12, 5, 10`.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
3+
import { maximumToys } from './mark_and_toys';
4+
import TEST_CASES from './mark_and_toys.testcases.json';
5+
6+
describe('maximumToys', () => {
7+
it('maximumToys test cases', () => {
8+
expect.assertions(3);
9+
10+
TEST_CASES.forEach((test) => {
11+
const result = maximumToys(test.prices, test.budget);
12+
13+
expect(result).toStrictEqual(test.expected);
14+
});
15+
});
16+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"title": "Sample Test Case 0",
4+
"budget": 50,
5+
"prices": [50, 1, 12, 5, 111, 200, 1000, 10],
6+
"expected": 4
7+
},
8+
{
9+
"title": "Sample Test Case 1",
10+
"budget": 7,
11+
"prices": [1, 2, 3, 4],
12+
"expected": 3
13+
},
14+
{
15+
"title": "Sample Test Case 2",
16+
"budget": 15,
17+
"prices": [3, 7, 2, 9, 4],
18+
"expected": 3
19+
}
20+
]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/sort/mark-and-toys.md]]
3+
*/
4+
5+
export function maximumToys(prices: number[], k: number): number {
6+
const group = [...prices];
7+
group.sort((a: number, b: number) => a - b);
8+
9+
let budget = k;
10+
const shoppingCart: number[] = [];
11+
12+
while (group.length > 0 && budget >= 0) {
13+
const currentItem = group.shift();
14+
budget -= currentItem!;
15+
if (budget >= 0) {
16+
shoppingCart.push(currentItem!);
17+
}
18+
}
19+
20+
return shoppingCart.length;
21+
}
22+
23+
export default { maximumToys };

0 commit comments

Comments
 (0)