KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • 문자열
  • 다이나믹 프로그래밍
  • 그래프
  • 수학
  • string
  • 백준
  • Level1
  • programmers
  • 정렬
  • Level 1
  • recursion
  • codeup
  • LeetCode
  • C
  • Level 2
  • tree
  • js
  • Level 0
  • 제코베 JS 100제
  • C++

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Binary Search / 1608번 / Special Array With X Elements Greater Than or Equal X / JS

2024. 5. 28. 13:08

< 문제 바로가기 >

 

< 문제 간단설명 >

양수만 존재하는 nums 배열이 주어진다.

x를 찾아서 반환하면 되는 문제인데, 이 x의 조건은 다음과 같다.

  1. nums 배열안에 존재하지 않는 수이다.
  2. x이상의 수가 정확히 x개 존재해야 한다.

만약 x가 존재할 수 없다면 -1을 반환하면 된다.

 

[ 풀이 1 - 단순 구현 ]

/**
 * @param {number[]} nums
 * @return {number}
 */
const specialArray = (nums) => {
  const sortedNumberList = [...nums].sort((a, b) => a - b);
  const min = 0;
  const max = sortedNumberList.at(-1);

  for (let x = max; x > min; x--) {
    let count = 0;
    for (let i = 0; i < sortedNumberList.length; i++) {
      const cur = sortedNumberList[i];

      if (x <= cur) {
        count++;
      }
    }

    if (count === x) {
      return x;
    }
  }

  return -1;
};

 

[ 풀이 2 - 이분 탐색 ]

/**
 * @param {number[]} nums
 * @return {number}
 */
var specialArray = function (nums) {
  const sortedNumberList = nums.sort((a, b) => a - b);
  let left = 0;
  let right = sortedNumberList.at(-1);

  while (left <= right) {
    const mid = Math.floor((left + right) / 2);

    let count = 0;
    for (let i = 0; i < sortedNumberList.length; i++) {
      if (sortedNumberList[i] >= mid) {
        count++;
      }
    }

    if (mid === count) {
      return mid;
    }

    if (count > mid) {
      left = mid + 1;
      continue;
    }
    if (count < mid) {
      right = mid - 1;
      continue;
    }
  }

  return -1;
};
저작자표시 (새창열림)

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Array / 260번 / Single Number ||| / JS  (0) 2024.05.31
LeetCode / Array / 1442번 / Count Triplets That Can Form Two Arrays of Equal XOR / JS  (0) 2024.05.30
LeetCode / Array / 661번 / Image Smoother / JS  (0) 2023.12.19
LeetCode / Array / 1582번 / Special Positions in a Binary Matrix / JS  (0) 2023.12.13
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Array / 260번 / Single Number ||| / JS
    • LeetCode / Array / 1442번 / Count Triplets That Can Form Two Arrays of Equal XOR / JS
    • LeetCode / Array / 661번 / Image Smoother / JS
    • LeetCode / Array / 1582번 / Special Positions in a Binary Matrix / JS
    KimMinJun
    KimMinJun

    티스토리툴바