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)

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Binary Search / 278번 / First Bad Version / JS

2023. 4. 5. 16:13

< 문제 바로가기 >

 

First Bad Version - LeetCode

Can you solve this real interview question? First Bad Version - You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed base

leetcode.com

 

< 문제 간단설명 >

나쁜 버전인지 판단해서 boolean 값으로 반환하는 api가 isBadVersion 라는 이름으로 주어진다.

만약 n번째 버전이 나쁜 버전이라면 그 후의 n+1번째 버전부터는 쭉 나쁜 버전이다.

isBadVersion을 이용해서 맨 처음 나오는 나쁜 버전이 몇 버전인지 찾는 문제이다.

 

/**
 * Definition for isBadVersion()
 * 
 * @param {integer} version number
 * @return {boolean} whether the version is bad
 * isBadVersion = function(version) {
 *     ...
 * };
 */

/**
 * @param {function} isBadVersion()
 * @return {function}
 */
var solution = function(isBadVersion) {
    /**
     * @param {integer} n Total versions
     * @return {integer} The first bad version
     */
    return function(n) {
        let start = 0;
        let end = n;
        let version = 0;

        while(start <= end) {
            version = Math.floor((start + end) / 2);

            if(isBadVersion(version) === true) {
                end = version - 1;
            }
            else {
                start = version + 1;
            }
        }

        return start;
    };
};

 

저작자표시

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

LeetCode / Binary Search Tree / 235번 / Lowest Common Ancestor of a Binary Search Tree / JS  (0) 2023.04.05
LeetCode / Binary Search Tree / 98번 / Validate Binary Search Tree / JS  (2) 2023.04.05
LeetCode / Binary Search / 704번 / Binary Search / JS  (0) 2023.04.05
LeetCode / Tree / 102번 / Binary Tree Level Order Traversal / JS  (0) 2023.04.05
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Binary Search Tree / 235번 / Lowest Common Ancestor of a Binary Search Tree / JS
    • LeetCode / Binary Search Tree / 98번 / Validate Binary Search Tree / JS
    • LeetCode / Binary Search / 704번 / Binary Search / JS
    • LeetCode / Tree / 102번 / Binary Tree Level Order Traversal / JS
    KimMinJun
    KimMinJun

    티스토리툴바