From 5d64f0bd5acfa81af4538d2018c98cbe3f471b3a Mon Sep 17 00:00:00 2001 From: Heejun Lee Date: Thu, 15 Aug 2019 00:29:16 +0900 Subject: [PATCH] =?UTF-8?q?resolve=20#137-luck-balance=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 --- .../Greedy-Algorithms/luck-balance.js | 29 +++++++++++++++++++ README.md | 7 +++++ 2 files changed, 36 insertions(+) create mode 100644 Interview-Preparation-Kit/Greedy-Algorithms/luck-balance.js diff --git a/Interview-Preparation-Kit/Greedy-Algorithms/luck-balance.js b/Interview-Preparation-Kit/Greedy-Algorithms/luck-balance.js new file mode 100644 index 0000000..dff1a4d --- /dev/null +++ b/Interview-Preparation-Kit/Greedy-Algorithms/luck-balance.js @@ -0,0 +1,29 @@ +/** + * @title Luck Balance + * @difficulty Easy + * @link https://www.hackerrank.com/challenges/luck-balance/problem + */ + +const luckBalance = (limit, contests) => { + const {length} = contests; + const sorted = contests.sort((a, b) => { + if (a[1] === b[1]) { + return b[0] - a[0]; + } + + return (a[1] > b[1]) ? 1 : -1; + }); + + let numImportant = length - [...sorted].findIndex(x => x[1] === 1); + numImportant = (numImportant > length) ? 0 : numImportant; + + const unImportant = [...sorted].slice(0, length - numImportant); + const loseImportant = [...sorted].slice(length - numImportant, length - numImportant + limit); + const winImportant = [...sorted].slice(length - numImportant + limit); + + const sumLuck = (unImportant.reduce((sum, [luck]) => sum + luck, 0) || 0) + + (loseImportant.reduce((sum, [luck]) => sum + luck, 0) || 0) - + (winImportant.reduce((sum, [luck]) => sum + luck, 0) || 0); + + return sumLuck; +}; diff --git a/README.md b/README.md index e1e1f85..546e179 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)| +#### Greedy-Algorithms +| Difficulty | Problem | Solution | +| --- | --- | --- | +| Easy | [Luck Balance](https://www.hackerrank.com/challenges/luck-balance/problem) | [Solution](./Interview-Preparation-Kit/Greedy-Algorithms/luck-balance.js)| +#### Strings +| Difficulty | Problem | Solution | +| --- | --- | --- | #### Warm-up-Challenges | Difficulty | Problem | Solution | | --- | --- | --- |