-
-
Notifications
You must be signed in to change notification settings - Fork 361
[DaleSeo] WEEK 07 Solutions #2807
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석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));
}
}
}
}
}
📊 시간/공간 복잡도 분석
피드백: 그리드를 한 번 순회하면서 땅을 만나면 깊이 우선 탐색으로 해당 섬의 모든 칸을 0으로 바꾼다. 개선 제안: 현재 구현이 적절해 보입니다.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석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));
}
}
}
}
}
📊 시간/공간 복잡도 분석
피드백: 그리드 전체를 한 번씩 방문하고, 땅인 칸을 방문할 때마다 인접한 칸을 스택으로 탐색합니다. 재귀 대신 명시적 스택을 사용해 스택 오버플로 문제를 피합니다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sink라는 이름도 직관적이고 함수로 분리하니 훨씬 가독성이 좋네요. |
||
| } | ||
| } | ||
| } | ||
| 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)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
number-of-islands/DaleSeo.rs
📊 시간/공간 복잡도 분석
피드백: 그리드의 모든 셀을 한 번씩 방문하고 each 섬에 대해 DFS 스택을 사용해 연결된 1을 제거한다.
개선 제안: 현재 구현이 적절해 보입니다.