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
32 changes: 32 additions & 0 deletions linked-list-cycle/sadie100.ts
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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set
  • 설명: 이 코드는 노드를 저장하는 Map을 이용하여 이미 방문한 노드를 체크하는 방식으로 순환 여부를 판단합니다. 따라서 Hash Map / Hash Set 패턴에 속합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

/*
리스트 노드를 저장하는 Map을 생성, 노드를 탐색하며 해당 Map에 노드를 저장하고 이미 있는 노드일 경우 true 리턴

시간복잡도 : O(N) - N은 리스트의 개수
공간복잡도 : O(N) - Map 객체
*/

function hasCycle(head: ListNode | null): boolean {
const nodeMap = new Map<ListNode, boolean>()
Comment thread
sadie100 marked this conversation as resolved.

while (head) {
if (nodeMap.get(head) === true) {
return true
}
nodeMap.set(head, true)
head = head.next
}

return false
}
79 changes: 79 additions & 0 deletions pacific-atlantic-water-flow/sadie100.ts
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 방식을 사용하여 문제를 해결합니다. 높이 조건에 따라 탐색을 제한하며, 연결된 영역을 찾는 데 적합합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
pacific에 닿을 수 있는 grid를 판별하는 이중 배열과 atlantic에 닿을 수 있는지를 판별하는 이중 배열을 생성한다.
pacific 배열은 r, c 중 하나라도 0일 경우 true값,
atlantic 배열은 r, c 중 하나라도 length값과 같을 경우 true값을 채운다

이후 r, c 탐색을 돌며 pacific/ atlantic true인 값이 있으면 dfs를 돌린다
- 이전 heights값보다 크거나 같으면 true 처리, 인접한 셀을 또 dfs 탐색

두 배열값이 둘 다 true인 위치들을 리턴한다

시간복잡도 : O(M*N) - 순회
*/

function pacificAtlantic(heights: number[][]): number[][] {
const m = heights.length
const n = heights[0].length
const result = []
const pacificChecker = Array.from({ length: m }).map((el, idx) => {
if (idx === 0) {
return new Array(n).fill(true)
} else {
const arr = new Array(n).fill(false)
arr[0] = true
return arr
}
})
const atlanticChecker = Array.from({ length: m }).map((el, idx) => {
if (idx === m - 1) {
return new Array(n).fill(true)
} else {
const arr = new Array(n).fill(false)
arr[n - 1] = true
return arr
}
})

const dx = [0, 1, -1, 0]
const dy = [1, 0, 0, -1]

const search = (row, col, checker, stdValue) => {
const value = heights[row][col]
if (value < stdValue) return

checker[row][col] = true

for (let i = 0; i < 4; i++) {
const newRow = row + dx[i]
const newCol = col + dy[i]
if (newRow < 0 || newRow >= m) continue
if (newCol < 0 || newCol >= n) continue
if (checker[newRow][newCol]) continue

search(newRow, newCol, checker, value)
}
}

// dfs 탐색
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (pacificChecker[i][j]) {
search(i, j, pacificChecker, heights[i][j])
}
if (atlanticChecker[i][j]) {
search(i, j, atlanticChecker, heights[i][j])
}
}
}

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (pacificChecker[i][j] && atlanticChecker[i][j]) {
result.push([i, j])
}
}
}

//pacific, atlantic 모두 true인 인덱스배열 반환
return result
}
Loading