From 386f5e67bd77bea790185caaf0dfa54f0a54557e Mon Sep 17 00:00:00 2001 From: Heejun Lee Date: Sat, 10 Aug 2019 01:03:28 +0900 Subject: [PATCH 1/3] =?UTF-8?q?resolve=20#126-fraudulent-activity-notifica?= =?UTF-8?q?tions=20=EB=AC=B8=EC=A0=9C=ED=95=B4=EA=B2=B0=20=EB=B0=8F=20?= =?UTF-8?q?=EB=AC=B8=EC=84=9C=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fraudulent-activity-notifications.js | 71 +++++++++++++++++++ Interview-Preparation-Kit/Sorting/test.js | 16 +++++ README.md | 7 ++ 3 files changed, 94 insertions(+) create mode 100644 Interview-Preparation-Kit/Sorting/fraudulent-activity-notifications.js create mode 100644 Interview-Preparation-Kit/Sorting/test.js diff --git a/Interview-Preparation-Kit/Sorting/fraudulent-activity-notifications.js b/Interview-Preparation-Kit/Sorting/fraudulent-activity-notifications.js new file mode 100644 index 0000000..77a8544 --- /dev/null +++ b/Interview-Preparation-Kit/Sorting/fraudulent-activity-notifications.js @@ -0,0 +1,71 @@ +/** + * @title Fraudulent Activity Notifications + * @difficulty Median + * @link https://www.hackerrank.com/challenges/fraudulent-activity-notifications/problem + */ + +const initState = { + 'countSorted': {}, + 'recentExpenditure': 0, + 'numOverSpend': 0 +}; + +const findValueAtIndex = (countSorted, findIndex) => { + let curPosition = 0; + let foundValue = 0; + for (const num in countSorted) { + if (countSorted.hasOwnProperty(num)) { + curPosition += countSorted[num]; + if (curPosition > findIndex) { + foundValue = num; + break; + } + } + } + + return parseInt(foundValue, 10); +}; + +const findMedian = (sortedArr, length) => { + let median = findValueAtIndex(sortedArr, parseInt(length / 2, 10)); + median = (length % 2 === 0) ? + median + findValueAtIndex(sortedArr, parseInt(length / 2, 10) - 1) : + median * 2; + + return median; +}; + +const computeOverSpend = (spendState, spend, index, restExpenditure) => { + const {length: restDays} = restExpenditure; + const {length: recordDays} = spendState.recentExpenditure; + + const median = findMedian(spendState.countSorted, recordDays); + + if (spend >= median) { + spendState.numOverSpend++; + } + + spendState.countSorted[spendState.recentExpenditure.shift()]--; + spendState.recentExpenditure.push(spend); + spendState.countSorted[spend] = (spendState.countSorted[spend] || 0) + 1; + + if (index === restDays - 1) { + return spendState.numOverSpend; + } + + return spendState; +}; + +function activityNotifications(expenditure, d) { + initState.recentExpenditure = expenditure.slice(0, d); + initState.countSorted = initState.recentExpenditure.reduce((countSorted, value) => { + countSorted[value] = (countSorted[value] || 0) + 1; + + return countSorted; + }, {}); + + const restExpenditure = expenditure.splice(d); + const ret = restExpenditure.reduce(computeOverSpend, initState); + + return ret; +} diff --git a/Interview-Preparation-Kit/Sorting/test.js b/Interview-Preparation-Kit/Sorting/test.js new file mode 100644 index 0000000..ff1097b --- /dev/null +++ b/Interview-Preparation-Kit/Sorting/test.js @@ -0,0 +1,16 @@ +const getMinMax = arr => arr.reduce( + ([min, max], cur) => [Math.min(min, cur), Math.max(max, cur)], + [Infinity, -Infinity] +); + +const getSum = arr => arr.reduce( + (sum, cur) => sum + cur, + 0 +); + +const miniMaxSum = arr => { + const [min, max] = getMinMax(arr); + const sum = getSum(arr); + + console.log(`${sum - max} ${sum - min}`); +}; diff --git a/README.md b/README.md index e1e1f85..778f396 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 | +| --- | --- | --- | +| Median | [Fraudulent Activity Notifications](https://www.hackerrank.com/challenges/fraudulent-activity-notifications/problem) | [Solution](./Interview-Preparation-Kit/Sorting/fraudulent-activity-notifications.js)| +#### Strings +| Difficulty | Problem | Solution | +| --- | --- | --- | #### Warm-up-Challenges | Difficulty | Problem | Solution | | --- | --- | --- | From d97221ed0bddcc7e24b56156af2033d423ac8464 Mon Sep 17 00:00:00 2001 From: Heejun Lee Date: Sat, 10 Aug 2019 01:16:33 +0900 Subject: [PATCH 2/3] =?UTF-8?q?refactor=20#125-Destructuring=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fraudulent-activity-notifications.js | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/Interview-Preparation-Kit/Sorting/fraudulent-activity-notifications.js b/Interview-Preparation-Kit/Sorting/fraudulent-activity-notifications.js index 77a8544..f9e00a2 100644 --- a/Interview-Preparation-Kit/Sorting/fraudulent-activity-notifications.js +++ b/Interview-Preparation-Kit/Sorting/fraudulent-activity-notifications.js @@ -4,12 +4,6 @@ * @link https://www.hackerrank.com/challenges/fraudulent-activity-notifications/problem */ -const initState = { - 'countSorted': {}, - 'recentExpenditure': 0, - 'numOverSpend': 0 -}; - const findValueAtIndex = (countSorted, findIndex) => { let curPosition = 0; let foundValue = 0; @@ -35,28 +29,32 @@ const findMedian = (sortedArr, length) => { return median; }; -const computeOverSpend = (spendState, spend, index, restExpenditure) => { +const computeOverSpend = ({countSorted, recentExpenditure, numOverSpend}, spend, index, restExpenditure) => { const {length: restDays} = restExpenditure; - const {length: recordDays} = spendState.recentExpenditure; + const {length: recordDays} = recentExpenditure; - const median = findMedian(spendState.countSorted, recordDays); + const median = findMedian(countSorted, recordDays); if (spend >= median) { - spendState.numOverSpend++; + numOverSpend++; } - spendState.countSorted[spendState.recentExpenditure.shift()]--; - spendState.recentExpenditure.push(spend); - spendState.countSorted[spend] = (spendState.countSorted[spend] || 0) + 1; + countSorted[recentExpenditure.shift()]--; + recentExpenditure.push(spend); + countSorted[spend] = (countSorted[spend] || 0) + 1; - if (index === restDays - 1) { - return spendState.numOverSpend; - } - - return spendState; + return {countSorted, + recentExpenditure, + numOverSpend}; }; function activityNotifications(expenditure, d) { + const initState = { + 'countSorted': {}, + 'recentExpenditure': 0, + 'numOverSpend': 0 + }; + initState.recentExpenditure = expenditure.slice(0, d); initState.countSorted = initState.recentExpenditure.reduce((countSorted, value) => { countSorted[value] = (countSorted[value] || 0) + 1; @@ -65,7 +63,7 @@ function activityNotifications(expenditure, d) { }, {}); const restExpenditure = expenditure.splice(d); - const ret = restExpenditure.reduce(computeOverSpend, initState); + const {numOverSpend} = restExpenditure.reduce(computeOverSpend, initState); - return ret; + return numOverSpend; } From d4b53a45010239663340d210e76a39559e0005c5 Mon Sep 17 00:00:00 2001 From: Heeveloper Date: Sat, 10 Aug 2019 01:17:55 +0900 Subject: [PATCH 3/3] Delete test.js --- Interview-Preparation-Kit/Sorting/test.js | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 Interview-Preparation-Kit/Sorting/test.js diff --git a/Interview-Preparation-Kit/Sorting/test.js b/Interview-Preparation-Kit/Sorting/test.js deleted file mode 100644 index ff1097b..0000000 --- a/Interview-Preparation-Kit/Sorting/test.js +++ /dev/null @@ -1,16 +0,0 @@ -const getMinMax = arr => arr.reduce( - ([min, max], cur) => [Math.min(min, cur), Math.max(max, cur)], - [Infinity, -Infinity] -); - -const getSum = arr => arr.reduce( - (sum, cur) => sum + cur, - 0 -); - -const miniMaxSum = arr => { - const [min, max] = getMinMax(arr); - const sum = getSum(arr); - - console.log(`${sum - max} ${sum - min}`); -};