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.
문제
풀이 후기
안풀려서 서치를 통해 도움을 받고 풀어낸 문제다.
고전한 첫 번째 이유는 BFS를 너무 전형적인 방식으로밖에 안풀어보아서 틀에 박혀 있었던 탓이다.
기본적인 BFS 틀에서 벗어나 큐의 특성을 좀 더 잘 활용할 수 있었다면 스스로 풀 수 있었을 것이다.
시작점이 여러 개인 BFS를 동시에 할 수 있나? 싶었는데 큐를 이용하면 할 수 있었던 것이다.
또한 answer를 갱신할 때 처음엔 기존 값과 갱신하려는
day+1
을 비교해 max 값으로 해주려고 했는데 큐의 특성상 계속 더 큰 값만 올 것이기 때문에 그럴 필요가 없었다.두 번쨰 이유는 시간초과다.
JS로 큐를 구현할 때 보통 shift()를 많이 이용하는데 이것이 O(n)이기 때문에 이런 문제에서는 시간 초과가 날 수 있다.
이는 직접 큐를 Linked List로 구현해서 해결한 사람들이 있던데 나는 실제 코테 환경에서 그럴 여유가 없을 것 같아 아예 dequeue를 하지 않고 index를 더해주는 방식으로 해결했다.