Skip to content

resolve #126-fraudulent-activity-notifications 문제해결 및 문서추가 #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @title Fraudulent Activity Notifications
* @difficulty Median
* @link https://www.hackerrank.com/challenges/fraudulent-activity-notifications/problem
*/

const findValueAtIndex = (countSorted, findIndex) => {
let curPosition = 0;
let foundValue = 0;
for (const num in countSorted) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array 에 in 대신 다른 메서드를 사용할수 있을것 같습니다.

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 = ({countSorted, recentExpenditure, numOverSpend}, spend, index, restExpenditure) => {
const {length: restDays} = restExpenditure;
const {length: recordDays} = recentExpenditure;

const median = findMedian(countSorted, recordDays);

if (spend >= median) {
numOverSpend++;
}

countSorted[recentExpenditure.shift()]--;
recentExpenditure.push(spend);
countSorted[spend] = (countSorted[spend] || 0) + 1;

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;

return countSorted;
}, {});

const restExpenditure = expenditure.splice(d);
const {numOverSpend} = restExpenditure.reduce(computeOverSpend, initState);

return numOverSpend;
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| --- | --- | --- |
Expand Down