Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions linked-list-cycle/reeseo3o.js
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Fast & Slow Pointers
  • 설명: 이 코드는 두 포인터를 이용해 순환 여부를 판단하는 패턴으로, 느린 포인터와 빠른 포인터를 활용하여 사이클을 탐지합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(1)

피드백: 빠른 포인터와 느린 포인터를 동시에 이동시키며, 만나는 지점이 있으면 사이클이 존재하는 것으로 판단한다. 공간은 상수만 사용한다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Time Complexity: O(n)
// Space Complexity: O(1)

const hasCycle = (head) => {
if (head === null || head.next === null) {
return false;
}

let slow = head;
let fast = head.next;

while (slow !== fast) {
if (fast === null || fast.next === null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}

return true;
};

49 changes: 49 additions & 0 deletions pacific-atlantic-water-flow/reeseo3o.js
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: DFS
  • 설명: 이 코드는 깊이 우선 탐색(DFS)을 활용하여 각 셀에서 인접한 셀로 탐색하며 조건에 맞는 위치를 찾는 방식으로 문제를 해결합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(m*n)
Space O(m*n)

피드백: 각 셀에서 DFS를 수행하는 대신, 해안에서부터 역방향으로 탐색하여 도달 가능한 셀을 표시한다. 전체적으로 각 셀은 한 번씩 방문되므로 시간과 공간 모두 맵 크기에 비례한다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Time Complexity: O(m * n)
// Space Complexity: O(m * n)

const pacificAtlantic = (heights) => {
if (!heights?.length || !heights[0]?.length) {
return [];
}

const m = heights.length;
const n = heights[0].length;
const pacific = Array.from({ length: m }, () => Array(n).fill(false));
const atlantic = Array.from({ length: m }, () => Array(n).fill(false));
const dirs = [
[-1, 0],
[1, 0],
[0, -1],
[0, 1],
];

const dfs = (visited, r, c) => {
visited[r][c] = true;
for (const [dr, dc] of dirs) {
const nr = r + dr;
const nc = c + dc;
if (nr < 0 || nr >= m || nc < 0 || nc >= n || visited[nr][nc]) continue;
if (heights[nr][nc] < heights[r][c]) continue;
dfs(visited, nr, nc);
}
};

for (let i = 0; i < m; i++) {
dfs(pacific, i, 0);
dfs(atlantic, i, n - 1);
}
for (let j = 0; j < n; j++) {
dfs(pacific, 0, j);
dfs(atlantic, m - 1, j);
}

const result = [];
for (let r = 0; r < m; r++) {
for (let c = 0; c < n; c++) {
if (pacific[r][c] && atlantic[r][c]) {
result.push([r, c]);
}
}
}
return result;
};
Loading