Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
문제
풀이 후기
처음으로 파라메트릭 서치 문제를 풀었을 때 종료 조건 및 start와 end 설정 기준에 대한 고민이 있었는데, 이번에 모두 해결 됐다.
특히 start와 end 설정 기준을 이 코드처럼 했을 때, 종료 조건을 이렇게 해야만 하는 이유를 알기 위해 노력했다.
종료 조건을
start <= end
가 아닌start < end
로만 해줘도 되지 않을까? 라는 의문이 있었기 때문이다.하지만 후자는 통과가 안됐고, 반례를 열심히 찾아 뒤졌다.
위 사례가 그 반례인데, 이를 통해 end가 답인 경우가 있을 수 있으니 start와 end가 같은 경우도 체크해줘야 한다는 것을 알게 됐다.
어떤 특정 구간에서 정답이 middle이 됐다면, 이는 다음 구간에서 answer에 저장되기 때문이다.
따라서
start < end
로 하면 end가 정답일 경우 정답이 middle에만 담긴 채로 끝나게 된다.