Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

파란색가운의 개발 블로그

[이코테] CH07 이진 탐색 실전문제 28번 고정점 찾기 본문

알고리즘 문제 풀이/이코테

[이코테] CH07 이진 탐색 실전문제 28번 고정점 찾기

파란색 가운 2024. 1. 7. 15:06
import sys
# sys.stdin.readline()
from collections import deque
sys.setrecursionlimit(100000000)
import heapq
import copy
import itertools
from itertools import combinations
from itertools import permutations
INF = 1e9
from bisect import bisect_left,bisect_right
def findIndex(start,end):
    if start > end:
        return None
    mid = (start + end) // 2
    if arr[mid] == mid:
        return mid
    elif arr[mid] > mid:
        return findIndex(start,mid-1)
    else:
        return findIndex(mid+1,end)


N = int(sys.stdin.readline())
arr = list(map(int,sys.stdin.readline().split()))

value = findIndex(0,len(arr))
if value == None:
    print(-1)
else:
    print(value)

쉬운 문제였다

그냥 array[index] == index 기점으로 해서

array[index] > index -> 왼쪽 탐색 ( start,mid-1)

array[index] < index -> 오른쪽 탐색(mid+1,end) 로 구분해주면 끝

코테 이렇게 나오고 나만 풀수 있게 사람들이 바보가 되는 세계가 있으면 좋겠어요 ㅠ