Skip to content

resolve #137-luck-balance 문제풀이 및 문서추가 #138

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 1 commit 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
29 changes: 29 additions & 0 deletions Interview-Preparation-Kit/Greedy-Algorithms/luck-balance.js
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Owner

Choose a reason for hiding this comment

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

[...sorted]

를 사용하는 이유가 있나요 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

findIndex를 쓰려고 사용했습니다

Copy link
Owner

Choose a reason for hiding this comment

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

numImportant 아래의 unImportant, loseImportant, winImportant 와 이름 형식이 같은데
아랫것들은 배열이고 numImportant 는 number 로 보입니다.
배열의 경우 s 복수형을 쓰는것이 좋을것 같습니다.

numImportant = (numImportant > length) ? 0 : numImportant;

const unImportant = [...sorted].slice(0, length - numImportant);
Copy link
Owner

Choose a reason for hiding this comment

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

length - numImportant 반복되서 사용되는 부분은 상수로 선언하는게 좋을것 같습니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👌

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) +
Copy link
Owner

Choose a reason for hiding this comment

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

(sum, [luck]) => sum + luck

반복되는 부분은 함수로 빼는것이 좋을것 같습니다.

(loseImportant.reduce((sum, [luck]) => sum + luck, 0) || 0) -
(winImportant.reduce((sum, [luck]) => sum + luck, 0) || 0);

return sumLuck;
};
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)|
#### 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 |
| --- | --- | --- |
Expand Down