[DaleSeo] WEEK 07 Solutions - #2807
Merged
Merged
Conversation
Contributor
📊 DaleSeo 님의 학습 현황이번 주 제출 문제
누적 학습 요약
문제 풀이 현황
🤖 이 댓글은 GitHub App을 통해 자동으로 작성되었습니다. 🔢 API 사용량 (gpt-5-nano)
|
Contributor
There was a problem hiding this comment.
🏷️ 알고리즘 패턴 분석
number-of-islands/DaleSeo.rs
// TC: O(m * n)
// SC: O(m * n)
impl Solution {
pub fn num_islands(mut grid: Vec<Vec<char>>) -> i32 {
let n_rows = grid.len();
let n_cols = grid[0].len();
let mut count = 0;
for i in 0..n_rows {
for j in 0..n_cols {
if grid[i][j] != '1' {
continue;
}
count += 1;
grid[i][j] = '0';
let mut stack = vec![(i, j)];
while let Some((r, c)) = stack.pop() {
let neighbors = [
(r.wrapping_sub(1), c),
(r + 1, c),
(r, c.wrapping_sub(1)),
(r, c + 1),
];
for (nr, nc) in neighbors {
if nr < n_rows && nc < n_cols && grid[nr][nc] == '1' {
grid[nr][nc] = '0';
stack.push((nr, nc));
}
}
}
}
}
count
}
}- 패턴: Depth-First Search, Hash Map / Hash Set
- 설명: 그리드에서 섬의 모든 1을 DFS로 탐색하며 방문처리하고 인접한 노드를 스택에 쌓아 연결된 영역을 제거합니다. 각 섬을 발견할 때마다 카운트를 증가시키고, 방문 여부를 0으로 표시하여 중복 방문을 방지합니다.
📊 시간/공간 복잡도 분석
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(m * n) | O(n * m) | ❌ |
| Space | O(m * n) | O(n * m) | ❌ |
피드백: 그리드의 모든 셀을 한 번씩 방문하고 each 섬에 대해 DFS 스택을 사용해 연결된 1을 제거한다.
개선 제안: 현재 구현이 적절해 보입니다.
Contributor
There was a problem hiding this comment.
🏷️ 알고리즘 패턴 분석
number-of-islands/DaleSeo.rs
// TC: O(m * n)
// SC: O(m * n)
impl Solution {
pub fn num_islands(mut grid: Vec<Vec<char>>) -> i32 {
let mut cnt = 0;
for r in 0..grid.len() {
for c in 0..grid[r].len() {
if grid[r][c] == '1' {
cnt += 1;
Self::sink(&mut grid, r, c);
}
}
}
cnt
}
fn sink(grid: &mut Vec<Vec<char>>, row: usize, col: usize) {
let mut stack = vec![(row, col)];
while let Some((row, col)) = stack.pop() {
grid[row][col] = '0';
for (r, c) in [
(row, col.wrapping_sub(1)),
(row, col + 1),
(row.wrapping_sub(1), col),
(row + 1, col),
] {
if r < grid.len() && c < grid[r].len() && grid[r][c] == '1' {
stack.push((r, c));
}
}
}
}
}- 패턴: Depth-First Search, Hash Map / Hash Set
- 설명: 주요 아이디어는 뿌리 노드(육지)를 발견하면 스택으로 깊이 우선 방문해 연결된 '1'들을 모두 방문 처리(탐색)하고, 이미 방문한 부분을 '0'으로 표기하여 섬의 경계를 제거한다. 이 과정에서 인접 노드를 스택에 쌓고 순회하는 DFS 패턴이 활용된다.
📊 시간/공간 복잡도 분석
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(m * n) | O(n * m) | ❌ |
| Space | O(m * n) | O(n * m) | ❌ |
피드백: 그리드를 한 번 순회하면서 땅을 만나면 깊이 우선 탐색으로 해당 섬의 모든 칸을 0으로 바꾼다.
개선 제안: 현재 구현이 적절해 보입니다.
Contributor
There was a problem hiding this comment.
🏷️ 알고리즘 패턴 분석
number-of-islands/DaleSeo.rs
// TC: O(m * n)
// SC: O(m * n)
impl Solution {
pub fn num_islands(mut grid: Vec<Vec<char>>) -> i32 {
let mut cnt = 0;
for r in 0..grid.len() {
for c in 0..grid[r].len() {
if grid[r][c] == '1' {
cnt += 1;
Self::sink(&mut grid, r, c);
}
}
}
cnt
}
fn sink(grid: &mut Vec<Vec<char>>, row: usize, col: usize) {
grid[row][col] = '0';
let mut stack = vec![(row, col)];
while let Some((row, col)) = stack.pop() {
for (r, c) in [
(row, col.wrapping_sub(1)),
(row, col + 1),
(row.wrapping_sub(1), col),
(row + 1, col),
] {
if r < grid.len() && c < grid[r].len() && grid[r][c] == '1' {
grid[r][c] = '0';
stack.push((r, c));
}
}
}
}
}- 패턴: Depth-First Search, Greedy, Hash Map / Hash Set
- 설명: 섬 탐지를 위해 인접 '1'을 DFS로 방문하며 연결된 영역을 모두 방문(제거)하는 방식이 핵심이다. 비연결된 섬들을 차례대로 탐색하며 수를 증가시키는 점도 간접적으로 탐색 기법에 해당한다.
📊 시간/공간 복잡도 분석
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(m * n) | O(N * M) | ❌ |
| Space | O(m * n) | O(N * M) | ❌ |
피드백: 그리드 전체를 한 번씩 방문하고, 땅인 칸을 방문할 때마다 인접한 칸을 스택으로 탐색합니다. 재귀 대신 명시적 스택을 사용해 스택 오버플로 문제를 피합니다.
개선 제안: 현재 구현이 적절해 보입니다.
parkhojeong
approved these changes
Aug 8, 2026
| for c in 0..grid[r].len() { | ||
| if grid[r][c] == '1' { | ||
| cnt += 1; | ||
| Self::sink(&mut grid, r, c); |
Contributor
There was a problem hiding this comment.
sink라는 이름도 직관적이고 함수로 분리하니 훨씬 가독성이 좋네요.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
답안 제출 문제
작성자 체크 리스트
In Review로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!