Last active
April 18, 2022 18:11
-
-
Save mununki/67b11c72bfb27cbedd89f3026bdd7e27 to your computer and use it in GitHub Desktop.
프로그래머스 코테 - 논문 g-index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Belt | |
let solution = xs => { | |
// 인용 횟수를 내림차순으로 정렬합니다. [3, 2, 1, ...] | |
// g-index가 제일 높은 경우, 논문 N개 | |
// 인용 횟수 배열의 맨 뒤부터 하나씩 sum을 비교하여, sum >= gIndex^2 인 gIndex를 찾습니다. | |
// 역으로 찾는 이유는 앞에서부터 찾는 경우 중간 이후 다시 조건을 만족하게 되는 경우를 찾을 수 없기 때문입니다. | |
// [3, 2, 1, ...] | |
let sorted = xs->List.fromArray->List.sort((a, b) => b - a)->List.toArray | |
// 전체 합 | |
let sum = sorted->Array.reduce(0, (a, b) => a + b) | |
// 논문 N 개 = 제일 높은 gIndex | |
let gIndex = sorted->Array.length | |
let rec solutionAux = (sum, gIndex, i) => { | |
if sum >= gIndex * gIndex { | |
gIndex | |
} else { | |
solutionAux(sum - sorted->Array.getUnsafe(i), gIndex - 1, i - 1) | |
} | |
} | |
solutionAux(sum, gIndex, sorted->Array.length - 1) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 문제: g-index를 구하라 | |
// 배열의 상위 g개의 합이 g^2가 되는 값중 최대값을 구하라 | |
// paper result | |
// [1, 0, 0, 3, 0, 1] 2 | |
// [7, 5, 8, 10, 6, 9, 5] 7 | |
// [3, 0, 3, 0, 3, 0] 3 | |
let solution: array<int> => int |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment