컴퓨터공학/LeetCode Solutions

LeetCode 704 Binary search 이진 탐색

saurus2 2023. 10. 14. 07:38

안녕하세요 코딩세끼 입니다.
개발자가 되고 싶은 구독자들을 위해 코딩 스터디 영상을 제작하고 있습니다.
미국 실리콘 밸리 뿐만아니라 한국에서도 개발자 능력의 필수 요소로 코딩테스트 및 코딩 인터뷰가 요구 되고 있습니다. 영상 구성은 다음과 같습니다.

-영상 구성-
스터디 영상 하나에 리트코드 문제 하나를 각자 풀 수 있도록 영상을 구성하였습니다. 
스터디를 진행해보시면서 궁금한 점이나 피드백이 있으면 댓글 부탁드립니다. 

이번 영상은 릿코드(리트코드) LeetCode 704 Binary search 문제를 다루는 스터디입니다.
LeetCode 704 Binary search

-스터디 방식-
1. 동영상을 재생하는 동안 각자 문제를 풀 시간 20분이 주어집니다.
2. 5분마다 힌트를 드립니다.
2-1) 문제 번역
2-2) 문제 접근 방법
2-3) 문제 풀이 힌트
3. 20분이 지나면 문제 풀이를 진행 합니다.
3-1) 한 문제에서 나오는 모든 풀이들을 다루려고 노력하고 있습니다.

현재는 Python으로 해설을 진행하며, 추후 요청에따라 다른 언어로도 영상을 찍도록 하겠습니다.

https://youtu.be/cMWio2e7rlI

 

https://leetcode.com/problems/binary-search/description/

 

Binary Search - LeetCode

Can you solve this real interview question? Binary Search - Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

leetcode.com

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

You must write an algorithm with O(log n) runtime complexity.

 

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1

 

Constraints:

  • 1 <= nums.length <= 104
  • -104 < nums[i], target < 104
  • All the integers in nums are unique.
  • nums is sorted in ascending order.