-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #458 from sir-gon/feature/mark-and-toys
[Hacker Rank] Interview Preparation Kit: Sorting: Mark and Toys. Solv…
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
docs/hackerrank/interview_preparation_kit/sort/mark-and-toys.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
16 changes: 16 additions & 0 deletions
16
src/hackerrank/interview_preparation_kit/sort/mark_and_toys.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
src/hackerrank/interview_preparation_kit/sort/mark_and_toys.testcases.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
src/hackerrank/interview_preparation_kit/sort/mark_and_toys.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |