๐Ÿ“„ Description

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.

์ •๋ ฌ๋œ nums๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ด์ง„ ๊ฒ€์ƒ‰์œผ๋กœ target์— ํ•ด๋‹น๋˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„๋ผ.

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

๐Ÿ”จ My Solution

  • This is the Iterative implmentation of Binary Search.

๐Ÿ’ป My Submission

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        left = 0
        right = len(nums)-1
        
        while left<=right:
            mid=left + (right-left)//2
            if target==nums[mid]:
                return mid
            # If x is greater, ignore left half
            elif target>nums[mid]:
                    left = mid+1
            # If x is greater, ignore right half
            else:
                right = mid-1
                
        return -1

๐Ÿ’ก What I learned

There is TWO ways to implement Binary Search.

๐Ÿ‘‰ [1. Recursive] - ์žฌ๊ท€ ํ’€์ด

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        left = 0
        right = len(nums)-1
        
        def dfs(nums, l, r, target):
          if l<=r:
              mid=l+(r-l)//2
              if target==nums[mid]:
                  return mid
              # If element is smaller than mid, then it
        	  # can only be present in left subarray
              elif target>nums[mid]:
                  dfs(arr, mid+1, r, target)
              # Else the element can only be present
        	  # in right subarray
              else:
              	  dfs(arr, l, mid-1, target)
          else:
              return -1

๐Ÿ‘‰ [2. Iterative] - ๋ฐ˜๋ณต ํ’€์ด

My solution was "Iterative" implementation for Binary Search.

๐Ÿ’ญ My question

Then, what's the difference between "Iterative" approach and "Recursive" approach?

Recursion vs Iteration, What's the difference?

๐Ÿ’ญ Another Approach

๐Ÿ‘‰ Using Binary Search Module bisect() - ๋ชจ๋“ˆ ์‚ฌ์šฉ

ํŒŒ์ด์ฌ์—์„œ๋Š” ์ด์ง„ ๊ฒ€์ƒ‰์„ ์ง์ ‘ ๊ตฌํ˜„ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ์ด์ง„ ๊ฒ€์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ง€์›ํ•˜๋Š” bisect ๋ชจ๋“ˆ์„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•œ๋‹ค.

class Solution:
    def search(self, nums: List[int], target: int) -> int:
		index = bisect.bisect(nums, target)
        
        if index<len(nums) and nums[index]==target:
        	return index
        else:    
        	return -1

๐Ÿ’ก About bisect()

์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ์— ์ƒˆ๋กœ์šด ์•„์ดํ…œ ์‚ฝ์ž… ํ›„์— ๋‹ค์‹œ ์ •๋ ฌํ•  ํ•„์š” ์—†๋„๋ก ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•˜๋Š” ๋ชจ๋“ˆ์ด๋‹ค.
๊ธฐ๋ณธ์ ์ธ ์ด์ง„ ๋ถ„ํ•  ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— bisect๋ผ๊ณ  ๋ถˆ๋ฆฐ๋‹ค.

๐Ÿ“Œ bisect.bisect_left( )

์ •๋ ฌ๋œ a์— x๋ฅผ ์‚ฝ์ž…ํ•  ์œ„์น˜, ์ขŒ์ธก์œผ๋กœ ์‚ฝ์ž…๋˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
์ฐพ์•„๋‚ธ ๊ฐ’์˜ ํ•ด๋‹น ์œ„์น˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌํ„ด

๐Ÿ“Œ bisect.bisect_right( ), bisect.bisect_right( )

์ •๋ ฌ๋œ a์— x๋ฅผ ์‚ฝ์ž…ํ•  ์œ„์น˜, ์ขŒ์ธก์ด ์•„๋‹Œ ์šฐ์ธก์œผ๋กœ ์‚ฝ์ž…๋˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
์ฐพ์•„๋‚ธ ๊ฐ’์˜ ๋‹ค์Œ ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌํ„ด

References

profile
Make your lives Extraordinary!

0๊ฐœ์˜ ๋Œ“๊ธ€