경쟁 프로그래밍쉬움

거의 정렬된 배열에서 검색

'거의 정렬된 배열에서 검색' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

거의 정렬된 배열 arr에서 target 값을 검색하는 함수 search_almost_sorted(arr, target)을 작성하세요. 거의 정렬된 배열은 완전히 정렬된 배열에서 인덱스 i에 있어야 하는 요소가 대신 인덱스 i-1, i 또는 i+1에 있을 수 있는 배열입니다. 발견되면 대상의 0 기반 인덱스를 반환하고, 그렇지 않으면 -1을 반환합니다.

제약
  • 1 <= len(arr) <= 10^5
  • -10^9 <= arr[i], target <= 10^9
  • All elements in arr are unique.

Example 1
Input
search_almost_sorted([10, 3, 40, 20, 50, 80, 70], 40)
Output
2
Explanation

40 is at index 2 (which is its correct position in a fully sorted version).

Example 2
Input
search_almost_sorted([10, 3, 40, 20, 50, 80, 70], 90)
Output
-1
Explanation

90 does not exist in the array.

Need a Hint?
세트나 힙과 같은 검색 및 정렬 관련 데이터 구조를 사용해 보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.