From f1519483461e9d99171cafbc93f177ad1b46fb68 Mon Sep 17 00:00:00 2001 From: Heejun Lee Date: Sat, 10 Aug 2019 17:28:53 +0900 Subject: [PATCH 1/3] =?UTF-8?q?resolve=20#128-mark-and-toys=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=ED=92=80=EC=9D=B4=20=EB=B0=8F=20=EB=AC=B8=EC=84=9C?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sorting/mark-and-toys.js | 23 +++++++++++++++++++ README.md | 7 ++++++ 2 files changed, 30 insertions(+) create mode 100644 Interview-Preparation-Kit/Sorting/mark-and-toys.js diff --git a/Interview-Preparation-Kit/Sorting/mark-and-toys.js b/Interview-Preparation-Kit/Sorting/mark-and-toys.js new file mode 100644 index 0000000..dbf4743 --- /dev/null +++ b/Interview-Preparation-Kit/Sorting/mark-and-toys.js @@ -0,0 +1,23 @@ +/** + * @title Mark and Toys + * @difficulty Easy + * @link https://www.hackerrank.com/challenges/mark-and-toys/problem + */ + +const ascendingCompare = (a, b) => a - b; + +function maximumToys(prices, budget) { + const sorted = prices.sort(ascendingCompare); + let sumPrice = 0; + + for (const idx in sorted) { + if (sorted.isOwnProperty(idx)) { + sumPrice += sorted[idx]; + if (sumPrice > budget) { + return idx; + } + } + } + + return prices.length; +} diff --git a/README.md b/README.md index e1e1f85..eef0d17 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,13 @@ Generates the README.md. | --- | --- | --- | | Easy | [2D Array - DS](https://www.hackerrank.com/challenges/2d-array/problem) | [Solution](./Interview-Preparation-Kit/Arrays/2d-array-ds.js)| | Easy | [Left Rotation](https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem) | [Solution](./Interview-Preparation-Kit/Arrays/left-rotation.js)| +#### Sorting +| Difficulty | Problem | Solution | +| --- | --- | --- | +| Easy | [Mark and Toys](https://www.hackerrank.com/challenges/mark-and-toys/problem) | [Solution](./Interview-Preparation-Kit/Sorting/mark-and-toys.js)| +#### Strings +| Difficulty | Problem | Solution | +| --- | --- | --- | #### Warm-up-Challenges | Difficulty | Problem | Solution | | --- | --- | --- | From 4d3df4bbcf43d7e323f03fca8a6e3687cc23b69f Mon Sep 17 00:00:00 2001 From: Heejun Lee Date: Sat, 10 Aug 2019 17:36:44 +0900 Subject: [PATCH 2/3] =?UTF-8?q?refactor=20#129-=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=ED=91=9C=ED=98=84=EC=8B=9D=EC=9C=BC=EB=A1=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Interview-Preparation-Kit/Sorting/mark-and-toys.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Interview-Preparation-Kit/Sorting/mark-and-toys.js b/Interview-Preparation-Kit/Sorting/mark-and-toys.js index dbf4743..e5ce867 100644 --- a/Interview-Preparation-Kit/Sorting/mark-and-toys.js +++ b/Interview-Preparation-Kit/Sorting/mark-and-toys.js @@ -6,7 +6,7 @@ const ascendingCompare = (a, b) => a - b; -function maximumToys(prices, budget) { +const maximumToys = (prices, budget) => { const sorted = prices.sort(ascendingCompare); let sumPrice = 0; From fd6e4b4a82100844be58c89d88b2b678a3102802 Mon Sep 17 00:00:00 2001 From: Heejun Lee Date: Thu, 15 Aug 2019 00:42:56 +0900 Subject: [PATCH 3/3] =?UTF-8?q?refactor=20#129=20for...in=20=EB=AF=B8?= =?UTF-8?q?=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sorting/mark-and-toys.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Interview-Preparation-Kit/Sorting/mark-and-toys.js b/Interview-Preparation-Kit/Sorting/mark-and-toys.js index e5ce867..b27168c 100644 --- a/Interview-Preparation-Kit/Sorting/mark-and-toys.js +++ b/Interview-Preparation-Kit/Sorting/mark-and-toys.js @@ -6,18 +6,16 @@ const ascendingCompare = (a, b) => a - b; -const maximumToys = (prices, budget) => { +function maximumToys(prices, budget) { const sorted = prices.sort(ascendingCompare); + const {length} = sorted; let sumPrice = 0; - - for (const idx in sorted) { - if (sorted.isOwnProperty(idx)) { - sumPrice += sorted[idx]; - if (sumPrice > budget) { - return idx; - } + for (let idx = 0; idx < length; idx++) { + sumPrice += sorted[idx]; + if (sumPrice > budget) { + return idx; } } - return prices.length; + return length; }