Skip to content
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

[Leetcode - Medium] Shortest Bridge #20

Merged
merged 2 commits into from
Dec 21, 2024
Merged

Conversation

BangDori
Copy link
Contributor

문제

Type Info
Platform Leetcode
Level Medium
Link https://leetcode.com/problems/shortest-bridge/

풀이

  1. DFS로 첫 번째 섬을 탐색
    • 첫 번째 섬의 모든 좌표를 방문하며, 두 번째 섬과 구분하기 위해 다른 값(ISLAND2)으로 변경한다.
    • 방문한 좌표를 BFS 큐에 추가한다.
  2. BFS로 다리 길이 계산
    • 첫 번째 섬의 좌표를 시작점으로 하여 바다(WATER)를 탐색하며 확장해 나간다.
      • 탐색한 바다는 첫 번째 섬으로 부터의 거리 값(distance)으로 변환한다.
      • ⭐️ 거리 값(distance)은 두 번째 섬과 동일한 값으로 분류될 수 있기 때문에 음수로 저장한다.
    • 다른 섬(ISLAND)에 도달할 때까지 BFS를 반복하며 다리의 길이를 계산한다.

어려웠던 점

  • BFS를 구현하는데 DFS와 개념을 혼용해서, BFS에서 배열의 첫 번째 인자를 어떻게 O(1)로 가져올 수 있을지에 대해 고민을 한다고 너무 많은 시간을 사용함...

알게된 점

  • BFS는 깊이 우선 탐색으로, 자바스크립트에서 재귀 함수를 이용해서 구현할 수 있음
  • DFS는 너비 우선 탐색으로, 자바스크립트에서 Stack 배열을 통해 구현할 수 있음

Copy link
Collaborator

@suhwan2004 suhwan2004 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다! 저의 경우 DFS로만 풀이를 진행했던지라, BFS로 풀어주신 풀이가 반갑네요 :)

Comment on lines 60 to 63
nextRow >= 0 &&
nextRow < rows &&
nextCol >= 0 &&
nextCol < cols
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

뭔가, 위에서도 쓰이는지라 만약 함수로 분리한다면 분리할 수 있을 것 같은 느낌이네요

function checkInArr (row, col) {
return row>= 0 &&
row< rows &&
col>= 0 &&
col< cols
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇 그렇겠네요! 피드백 감사합니다 ㅎㅎ

@BangDori BangDori merged commit ac2baaa into main Dec 21, 2024
1 check passed
@BangDori BangDori deleted the bangdori/shortest-bridge branch December 21, 2024 02:08
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.

2 participants