Skip to content

Conversation

sonjh1217
Copy link
Contributor

@sonjh1217 sonjh1217 commented Sep 16, 2025

답안 제출 문제

작성자 체크 리스트

  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 StatusIn Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

검토자 체크 리스트

Important

본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!

  • 바로 이전에 올라온 PR에 본인을 코드 리뷰어로 추가해주세요.
  • 본인이 검토해야하는 PR의 답안 코드에 피드백을 주세요.
  • 토요일 전까지 PR을 병합할 수 있도록 승인해주세요.

Copy link
Contributor

@yhkee0404 yhkee0404 left a comment

Choose a reason for hiding this comment

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

Swift 문법이 풍성하네요! 덕분에 배웠습니다: extension, Equatable, Hashable, ObjectIdentifier, guard let, _, let, [[Int]], [(Int,Int)], inout, repeating, count

Comment on lines +31 to +42
while node != nil {
guard let currentNode = node else {
return false
}

if visitedNodes.contains(currentNode) {
return true
}

visitedNodes.insert(currentNode)
node = currentNode.next
}
Copy link
Contributor

Choose a reason for hiding this comment

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

이미 nil Check를 했으니까 Force Unwrapping은 어떨까요?

Suggested change
while node != nil {
guard let currentNode = node else {
return false
}
if visitedNodes.contains(currentNode) {
return true
}
visitedNodes.insert(currentNode)
node = currentNode.next
}
while node != nil {
if visitedNodes.contains(node!) {
return true
}
visitedNodes.insert(node!)
node = node!.next
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

훨씬 깔끔하네요! 감사합니답!
force unwrapping은 지양하려고 하구 있어요 수정하다가 윗 라인이 날아가거나 할 수도 있으니까 만약을 위해서ㅎㅎ

Comment on lines 15 to 40
func bfs(queue: [(Int, Int)], goes: inout [[Bool]]) {
var queue = queue
var head = 0
for (i, j) in queue {
goes[i][j] = true
}

while head < queue.count {
var (i, j) = queue[head]
head += 1

for (di, dj) in directions {
let ni = i + di
let nj = j + dj
if ni >= 0
&& ni < m
&& nj >= 0
&& nj < n
&& !goes[ni][nj]
&& heights[ni][nj] >= heights[i][j] {
goes[ni][nj] = true
queue.append((ni, nj))
}
}
}
}
Copy link
Contributor

@yhkee0404 yhkee0404 Sep 20, 2025

Choose a reason for hiding this comment

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

재밌는 거 하나 공유하고 갑니다! (저만 재밌음 주의)
최단거리 구하지는 못하지만 Flood-Fill 하기는 좋아요!

Suggested change
func bfs(queue: [(Int, Int)], goes: inout [[Bool]]) {
var queue = queue
var head = 0
for (i, j) in queue {
goes[i][j] = true
}
while head < queue.count {
var (i, j) = queue[head]
head += 1
for (di, dj) in directions {
let ni = i + di
let nj = j + dj
if ni >= 0
&& ni < m
&& nj >= 0
&& nj < n
&& !goes[ni][nj]
&& heights[ni][nj] >= heights[i][j] {
goes[ni][nj] = true
queue.append((ni, nj))
}
}
}
}
func depthFirstBreadthSecondSearch(queue: [(Int, Int)], goes: inout [[Bool]]) {
var stack = queue
for (i, j) in stack {
goes[i][j] = true
}
while !stack.isEmpty {
let (i, j) = stack.removeLast()
for (di, dj) in directions {
let ni = i + di
let nj = j + dj
if ni >= 0
&& ni < m
&& nj >= 0
&& nj < n
&& !goes[ni][nj]
&& heights[ni][nj] >= heights[i][j] {
goes[ni][nj] = true
stack.append((ni, nj))
}
}
}
}

Copy link
Contributor Author

@sonjh1217 sonjh1217 Sep 20, 2025

Choose a reason for hiding this comment

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

음.. 저도 removeLast도 시도해보긴 했었는데.. 이점을 찾기가 어렵더라구요ㅠ 어떤 점이 좋은가요?

Copy link
Contributor

@yhkee0404 yhkee0404 Sep 20, 2025

Choose a reason for hiding this comment

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

구현하신 dfs의 재귀함수 대신 stack 반복문으로 구현하실 때 이 구현과 비교해 보시면 DFS도 BFS도 아니면서 일부 중복 검사도 head도 생략 가능한 혼종임을 아실 수 있으실 거예요. 다만 익숙하지 않으면 혼동을 초래할지도 몰라서 주의해야 하지만 오히려 DFS와 BFS 구현 실수를 줄이는 데 기여하는 변별력을 높일 수 있는 방법이라고도 생각해요 ㅎㅎ

Copy link
Contributor Author

Choose a reason for hiding this comment

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

아.. 구현의 이점을 말씀하신 거군요! 공감합니당ㅎㅎ

@yhkee0404
Copy link
Contributor

Swift가 싫은 점도 생겼습니다ㅋㅋ ! a가 안 되고 !a여야 하는군요!

Line 25: Char 19: error: unary operator cannot be separated from its operand in solution.swift
 23 |             }
 24 |             
 25 |             while ! stack.isEmpty {
    |                   `- error: unary operator cannot be separated from its operand
 26 |                 let (i, j) = stack.removeLast()
 27 |                 

Co-authored-by: yhkee0404 <[email protected]>
@sonjh1217
Copy link
Contributor Author

Swift가 싫은 점도 생겼습니다ㅋㅋ ! a가 안 되고 !a여야 하는군요!

Line 25: Char 19: error: unary operator cannot be separated from its operand in solution.swift
 23 |             }
 24 |             
 25 |             while ! stack.isEmpty {
    |                   `- error: unary operator cannot be separated from its operand
 26 |                 let (i, j) = stack.removeLast()
 27 |                 

와 다 돌려보셨군요 대단하세용!! Swift가 공백에 좀 예민합니당 ㅎㅎ

@sonjh1217
Copy link
Contributor Author

Swift 문법이 풍성하네요! 덕분에 배웠습니다: extension, Equatable, Hashable, ObjectIdentifier, guard let, _, let, [[Int]], [(Int,Int)], inout, repeating, count

protocol oriented 라서 상속 대신 Equatable, Hashable 같은 protocol들을 조합하는 것을 지향하구... 또 type safety를 중요시하는 특징이 있어용! 이렇게 꼼꼼히 리뷰해주시다니 넘 감사합니답!!

@sonjh1217 sonjh1217 merged commit 7938bc7 into DaleStudy:main Sep 20, 2025
1 check passed
@github-project-automation github-project-automation bot moved this from Solving to Completed in 리트코드 스터디 5기 Sep 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

2 participants