Skip to content

7576번 토마토 #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2022
Merged

7576번 토마토 #164

merged 1 commit into from
Jul 30, 2022

Conversation

NamJwong
Copy link
Owner

문제

풀이 후기

안풀려서 서치를 통해 도움을 받고 풀어낸 문제다.
고전한 첫 번째 이유는 BFS를 너무 전형적인 방식으로밖에 안풀어보아서 틀에 박혀 있었던 탓이다.
기본적인 BFS 틀에서 벗어나 큐의 특성을 좀 더 잘 활용할 수 있었다면 스스로 풀 수 있었을 것이다.

시작점이 여러 개인 BFS를 동시에 할 수 있나? 싶었는데 큐를 이용하면 할 수 있었던 것이다.
또한 answer를 갱신할 때 처음엔 기존 값과 갱신하려는 day+1을 비교해 max 값으로 해주려고 했는데 큐의 특성상 계속 더 큰 값만 올 것이기 때문에 그럴 필요가 없었다.

두 번쨰 이유는 시간초과다.
JS로 큐를 구현할 때 보통 shift()를 많이 이용하는데 이것이 O(n)이기 때문에 이런 문제에서는 시간 초과가 날 수 있다.
이는 직접 큐를 Linked List로 구현해서 해결한 사람들이 있던데 나는 실제 코테 환경에서 그럴 여유가 없을 것 같아 아예 dequeue를 하지 않고 index를 더해주는 방식으로 해결했다.

@NamJwong NamJwong self-assigned this Jul 30, 2022
@NamJwong NamJwong merged commit d4325dd into main Jul 30, 2022
@NamJwong NamJwong deleted the algorithm/#163 branch July 30, 2022 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7576번 토마토
1 participant