Skip to content

Commit 1a003ec

Browse files
authored
Merge pull request #930 from YeomChaeeun/main
[YeomChaeeun] Week 7
2 parents 2f399bf + 10426cf commit 1a003ec

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 중복된 문자가 없는 가장 긴 부분 문자열 구하기
3+
* 달레 알고리즘 해설을 참고하여 작성했습니다
4+
* 슬라이딩 윈도우 방식 적용
5+
* 알고리즘 복잡도
6+
* - 시간 복잡도: O(n)
7+
* - 공간 복잡도: O(n)
8+
* @param s
9+
*/
10+
function lengthOfLongestSubstring(s: string): number {
11+
const set = new Set<string>();
12+
let start = 0;
13+
let end = 0;
14+
let maxLength = 0;
15+
16+
while (end < s.length) {
17+
if (set.has(s[end])) {
18+
set.delete(s[start])
19+
start++
20+
} else {
21+
set.add(s[end])
22+
maxLength = Math.max(maxLength, set.size)
23+
end++
24+
}
25+
}
26+
return maxLength
27+
}
28+

number-of-islands/YeomChaeeun.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 2차 배열의 1로된 섬 구하기
3+
* 달레 알고리즘 해석을 참고하여 작성했습니다.
4+
* DFS(깊이 우선 알고리즘)
5+
* 알고리즘 복잡도
6+
* - 시간 복잡도: O(m x n)
7+
* - 공간 복잡도: O(m x n)
8+
* @param grid
9+
*/
10+
function numIslands(grid: string[][]): number {
11+
const rows = grid.length
12+
const cols = grid[0].length
13+
let islands = 0
14+
15+
// 인접한 땅의 1을 찾아야 함 - 상하좌우
16+
const dfs = (i: number, j: number): void => {
17+
if (i < 0 || i >= rows || j < 0 || j >= cols || grid[i][j] !== '1') {
18+
return
19+
}
20+
21+
grid[i][j] = '0' // 한번 확인한 경우 물로 바꿔줌
22+
23+
dfs(i + 1, j)
24+
dfs(i - 1, j)
25+
dfs(i, j + 1)
26+
dfs(i, j - 1)
27+
}
28+
29+
for (let i = 0; i < rows; i++) {
30+
for (let j = 0; j < cols; j++) {
31+
if (grid[i][j] === '1') {
32+
dfs(i, j)
33+
islands++
34+
}
35+
}
36+
}
37+
38+
return islands
39+
}

reverse-linked-list/YeomChaeeun.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 연결 리스트를 뒤집는 알고리즘
3+
* 알고리즘 복잡도
4+
* - 시간 복잡도: O(n)
5+
* - 공간 복잡도: O(n)
6+
*/
7+
/**
8+
* Definition for singly-linked list.
9+
* class ListNode {
10+
* val: number
11+
* next: ListNode | null
12+
* constructor(val?: number, next?: ListNode | null) {
13+
* this.val = (val===undefined ? 0 : val)
14+
* this.next = (next===undefined ? null : next)
15+
* }
16+
* }
17+
*/
18+
function reverseList(head: ListNode | null): ListNode | null {
19+
// console.log(head)
20+
if (head === null || head.next === null) {
21+
return head
22+
}
23+
24+
// 마지막 노드에 도달할 때까지 계속 재귀 호출
25+
const newHead = reverseList(head.next)
26+
27+
// 백트래킹 과정
28+
head.next.next = head
29+
head.next = null
30+
31+
return newHead
32+
}

0 commit comments

Comments
 (0)