일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- data_engineer
- 백준 온라인 저지
- datacamp
- 프로그래머스
- 백준온라인저지
- 빅데이터를 지탱하는 기술
- airflow webserver
- 2023년 목표
- MySQL
- docker image
- Spark
- telegram
- hackerrank
- 데이터 엔지니어
- SQL
- Data Engineering
- BOT
- dsf
- leetcode
- docker
- Airflow
- delete join
- terraform
- 알고리즘
- Pseudo Lab
- 그리디
- Dynamic Programming
- Python
- airflow architecture
- docker container
Archives
- Today
- Total
Lim Seunghyun Space
[Leetcode] Guess Number Higher or Lower 본문
문제 출처
Guess Number Higher or Lower - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제 해설
- 1부터 n까지 숫자들을 주어질 때, guess 함수를 이용해 찾고자 하는 숫자(pick)가 무엇인지 찾는 문제
- guess 함수는 이미 정의되어 있고, 숫자를 입력하면 -1, 0, 1 중 하나의 결괏값을 반환
- -1: 함수에 입력한 값이 pick보다 큰 경우
- 0: 함수에 입력한 값이 pick이랑 같은 경우
- 1: 함수에 입력한 값이 pick보다 작은 경우
문제 풀이
# source: https://leetcode.com/problems/guess-number-higher-or-lower/
# def guess(num: int) -> int:
class Solution:
def guessNumber(self, n: int) -> int:
start = 1
end = n
while start <= end:
mid = (end + start) // 2
res = guess(mid)
if res == 0:
return mid
elif res == -1:
end = mid - 1
elif res == 1:
start = mid + 1
- 1부터 n까지 탐색하는데 시간복잡도가 O(logN)인 이진 탐색을 이용
Github
728x90
'Algorithm > 문제' 카테고리의 다른 글
[Leetcode] Peak Index in a Mountain Array (0) | 2022.11.26 |
---|---|
[Leetcode] Search Insert Position (0) | 2022.11.24 |
[Leetcode] Binary Search (0) | 2022.11.23 |
[Leetcode] Reverse Integer (0) | 2022.11.21 |
[백준 온라인 저지] 균형잡힌 세상 (0) | 2022.11.19 |