상위 150개 인터뷰쉬움

회전 정렬 배열에서 검색

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

문제 설명

쉬움

(고유한 값을 사용하여) 오름차순으로 정렬된 정수 배열 nums이 있습니다. 함수에 전달되기 전에 nums은 알 수 없는 피벗 인덱스 k(1 <= k < nums.length)에서 회전되어 결과 배열이 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]이 될 수 있습니다.

가능한 회전 후 배열 nums과 정수 target가 주어지면 nums에 있으면 target의 인덱스를 반환하고, nums에 없으면 -1의 인덱스를 반환합니다.

O(log n) 런타임 복잡도를 갖는 알고리즘을 작성해야 합니다.

search(nums: List[int], target: int) -> int 함수를 작성하세요.

제약
  • 1 <= len(nums) <= 5000
  • -10^4 <= nums[i] <= 10^4
  • All values of nums are unique
  • nums is an ascending array that is possibly rotated
  • -10^4 <= target <= 10^4

Example 1
Input
nums = [4, 5, 6, 7, 0, 1, 2], target = 0
Output
4
Explanation

0 is found at index 4.

Example 2
Input
nums = [4, 5, 6, 7, 0, 1, 2], target = 3
Output
-1
Explanation

3 is not in the array.

Example 3
Input
nums = [1], target = 0
Output
-1
Explanation

0 is not 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 리소스

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