[ICE0208] WEEK 09 Solutions - #2833
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
🏷️ 알고리즘 패턴 분석
linked-list-cycle/ICE0208.java
class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
// fast가 끝에 도달하면 cycle이 없다.
while (fast != null && fast.next != null) {
slow = slow.next; // 한 칸 이동
fast = fast.next.next; // 두 칸 이동
// cycle이 있다면 두 포인터는 결국 같은 노드에서 만난다.
if (slow == fast) {
return true;
}
}
return false;
}
}- 패턴: Fast & Slow Pointers, Two Pointers
- 설명: 이 코드는 두 포인터(slow, fast)를 서로 다른 속도로 순환하며 사이클 여부를 판별하는 방식으로, Fast & Slow Pointers 패턴의 전형적인 구현입니다. 두 포인터가 서로 만나면 사이클 존재를 확인합니다.
📊 시간/공간 복잡도 분석
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(1) |
피드백: 두 포인터가 한 칸/두 칸으로 움직이며 사이클 여부를 판단합니다. 추가 메모리 없이 구현 가능합니다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
Contributor
There was a problem hiding this comment.
🏷️ 알고리즘 패턴 분석
pacific-atlantic-water-flow/ICE0208.java
import java.util.ArrayList;
import java.util.List;
class Solution {
static int[][] MOVES = {
{1, 0}, {-1, 0}, {0, 1}, {0, -1}
};
static void dfs(int[][] heights, boolean[][] visited, int i, int j) {
visited[i][j] = true;
for (int[] move : MOVES) {
int next_i = i + move[0];
int next_j = j + move[1];
if (next_i < 0 || next_i >= heights.length
|| next_j < 0 || next_j >= heights[0].length) {
continue;
}
if (visited[next_i][next_j]) {
continue;
}
// 바다에서 역방향으로 올라가므로
// 현재 높이보다 높거나 같은 곳으로만 이동
if (heights[next_i][next_j] < heights[i][j]) {
continue;
}
dfs(heights, visited, next_i, next_j);
}
}
public List<List<Integer>> pacificAtlantic(int[][] heights) {
int rows = heights.length;
int cols = heights[0].length;
boolean[][] po = new boolean[rows][cols];
boolean[][] ao = new boolean[rows][cols];
// 위 / 아래
for (int j = 0; j < cols; j++) {
dfs(heights, po, 0, j);
dfs(heights, ao, rows - 1, j);
}
// 왼쪽 / 오른쪽
for (int i = 0; i < rows; i++) {
dfs(heights, po, i, 0);
dfs(heights, ao, i, cols - 1);
}
List<List<Integer>> answer = new ArrayList<>();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (po[i][j] && ao[i][j]) {
answer.add(List.of(i, j));
}
}
}
return answer;
}
}- 패턴: Depth-First Search, Backtracking
- 설명: 제시 코드에서 물이 흐를 수 있는 경로를 DFS로 탐색해 각 위치가 두 바다에 닿는지 확인합니다. 각 방향으로 재귀적으로 탐색하며 방문 여부를 기록해 중복 방문을 방지하고 가능한 경로를 확인합니다.
📊 시간/공간 복잡도 분석
| 복잡도 | |
|---|---|
| Time | O(R * C) |
| Space | O(R * C) |
피드백: 각 셀에 대해 두 번의 DFS를 수행해야 하므로 전체적으로 행/열의 면적에 비례한 시간과 공간 복잡도를 갖습니다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
Contributor
📊 ICE0208 님의 학습 현황이번 주 제출 문제
누적 학습 요약
문제 풀이 현황
🤖 이 댓글은 GitHub App을 통해 자동으로 작성되었습니다. 🔢 API 사용량 (gpt-5-nano)
|
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 하나 이상을 반드시 검토를 해주셔야 합니다!