wherehows
Home
  • 분류 전체보기 (35)
    • 알고리즘 (31)
      • 코드트리 (0)
      • 백준 (1)
      • 리트코드 (21)
      • 프로그래머스 (9)
Home
  • 분류 전체보기 (35)
    • 알고리즘 (31)
      • 코드트리 (0)
      • 백준 (1)
      • 리트코드 (21)
      • 프로그래머스 (9)
블로그 내 검색

wherehows

  • 알고리즘/리트코드

    34. Find First and Last Position of Element in Sorted Array

    2021. 6. 16.

    by. 창고

    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


    특정 원소를 찾는데, 중복된 조건에서 가장 끝과 끝쪽에 있는 원소 찾기.

     

    - 이진 탐색 활용

    - 부등호를 어떻게 사용하느냐에 따라서 탐색 방향이 결정된다.

    - 기존 이진 탐색의 if(arr[mid] = target) return target; 과 같이, target을 발견하면 바로 return하는 꼴이 아님.

      target이 중복되므로, if(arr[mid] = target) idx = target; 을 통해서 idx를 계속해서 업데이트 한다.

     

    https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/discuss/14699/Clean-iterative-solution-with-two-binary-searches-(with-explanation) 

    https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/discuss/1181832/JS-Python-Java-C%2B%2B-or-Easy-Binary-Search-Solution-w-Explanation

    https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/discuss/14734/Easy-java-O(logn)-solution

     

     

    '알고리즘 > 리트코드' 카테고리의 다른 글

    287. Find the Duplicate Number  (0) 2021.06.18
    153. Find Minimum in Rotated Sorted Array  (0) 2021.06.17
    1314. Matrix Block Sum  (0) 2021.06.01
    1130. Minimum Cost Tree From Leaf Values  (0) 2021.06.01
    1043. Partition Array for Maximum Sum  (0) 2021.05.27

    댓글

    관련글

    • 287. Find the Duplicate Number 2021.06.18
    • 153. Find Minimum in Rotated Sorted Array 2021.06.17
    • 1314. Matrix Block Sum 2021.06.01
    • 1130. Minimum Cost Tree From Leaf Values 2021.06.01
    맨 위로
전체 글 보기
Tistory 로그인
Tistory 로그아웃
로그아웃 글쓰기 관리

Today

Total

Powered by ⓒ Kakao Corp.

Designed by Nana
블로그 이미지
창고

티스토리툴바