정렬된 2차원 리스트에서 이진탐색을 이용해 원하는 값이 있으면 true를 출력하고 아니라면 false를 출력하는 알고리즘을 만드려고 합니다.
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
def searchMatrix(matrix, target):
matrix1 = sum(matrix, [])
if len(matrix1) == 1 and target == matrix1[0]:
return True
if len(matrix1) == 1 and target != matrix1[0]:
return False
if len(matrix1) == 0:
return False
medium = len(matrix1) // 2
if target == matrix1[medium]:
return True
else:
if target > matrix1[medium]:
return searchMatrix(matrix1[medium:], target)
else:
return searchMatrix(matrix1[:medium], target)
생각한 방법은 2차원 list를 1차원으로 풀어내고 풀어낸 리스트에서 이진탐색을 실행하는 것인데
can only concatenate list (not "int") to list
라는 오류만 뜨고 해결을 못하네요.
어떻게 해결해야할지 알려주세요.