파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 7. 25. 23:45

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

 

Find First and Last Position of Element in Sorted Array - 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

문제에서는 O(logN)으로 풀라고 했는데, 그냥 O(N)으로도 정답이 나오긴 한다.

class Solution:
    def searchRange(self, nums: List[int], target: int) -> List[int]:
        start, end = -1, -1
        for i in range(len(nums)):
            if nums[i] == target:
                if start == -1:
                    start = i
                end = i
            
        return [start, end]

아마 문제의 의도는 Binary Search 알고리즘을 활용해서 풀라고 한 듯 =_=

반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.07.27
Today's Challenge  (0) 2022.07.26
Today's Challenge  (0) 2022.07.24
Today's Challenge  (0) 2022.07.23
Today's Challenge  (0) 2022.07.22
Comments