H-Index

2020.07.30

const solution = (citations) => {
  const sortedArr = citations.sort((a, b) => b - a);
  const limit = sortedArr.length;
  let h = limit;
  while (true) {
    let count = 0;
    sortedArr.every((num, idx, origin) => {
      if (num >= h) {
        count++;
        return true;
      }
      return false;
    });
    if (count >= h) {
      return h;
    }
    h--;
  }
};
  • 첫번째 난관은 h-index가 주어진 citations안에 있을 것이라고 넘겨 짚은 것

  • 두번째 난관은 h-index의 최대값이 배열의 길이보다 클 수 없다는 것을 미처 생각하지 못한 것

  • 그래도 첫 풀이보다는 많이 개선했다고 생각했는데 다른 풀이를 보니 중첩 반복문을 쓰지 않고도 풀 수 있는 거였다
    (못해먹겠네)

const solution = (citations) => {
  citations.sort((a, b) => b - a);
  const limit = citations.length;
  let index = 0;
  while (index < limit) {
    const current = citations[index];
    const hIndex = index + 1;
    if (current < hIndex) {
      break;
    }
    index++;
  }
  return index;
};
  • 그렇구나... 이렇게 풀어야 정렬 문제구나...

0개의 댓글