얼렁뚱땅 스며드는 Data Science

Algorithm/Daily Coding Tests Challenge

[Leetcode] Median : Find First and Last Position of Element in Sorted Array

Jesip14 2021. 10. 9. 16:17

Description

Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.

If target is not found in the array, return [-1, -1].

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

 

My Solution

class Solution:
    def searchRange(self, nums: List[int], target: int) -> List[int]:
        from bisect import bisect_left, bisect_right
        
        if target not in nums:
            return [-1, -1]
        else:
            return[bisect_left(nums, target), bisect_right(nums, target)-1]

 

문제 출처 : https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/