-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. numImportant 아래의 unImportant, loseImportant, winImportant 와 이름 형식이 같은데 |
||
numImportant = (numImportant > length) ? 0 : numImportant; | ||
|
||
const unImportant = [...sorted].slice(0, length - numImportant); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. length - numImportant 반복되서 사용되는 부분은 상수로 선언하는게 좋을것 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
를 사용하는 이유가 있나요 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
findIndex를 쓰려고 사용했습니다