Skip to content

Commit 2ef6b19

Browse files
authored
Merge pull request #1532 from hoyeongkwak/main
[hoyeongkwak\ Week 09 Solutions
2 parents 3f22d42 + 2669218 commit 2ef6b19

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed

linked-list-cycle/hoyeongkwak.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
/*
13+
Time complexity : O(n)
14+
Space complexity : O(1)
15+
*/
16+
function hasCycle(head: ListNode | null): boolean {
17+
let slow = head
18+
let fast = head
19+
20+
while (fast && fast.next) {
21+
slow = slow.next
22+
fast = fast.next.next
23+
if (slow === fast) {
24+
return true
25+
}
26+
}
27+
return false
28+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Time complexity : O(n)
3+
Space complexity : O(1)
4+
*/
5+
function maxProduct(nums: number[]): number {
6+
let max = 1
7+
let min = 1
8+
let result = nums[0]
9+
nums.forEach((num) => {
10+
const tempMax = Math.max(num, max * num, min * num)
11+
min = Math.min(num, max * num, min * num)
12+
max = tempMax
13+
result = Math.max(result, max)
14+
})
15+
return result
16+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Time complexity: O(s+t)
3+
Space complexity: O(s+t)
4+
*/
5+
function minWindow(s: string, t: string): string {
6+
const targetMap = new Map<string, number>()
7+
for (const char of t) {
8+
targetMap.set(char, (targetMap.get(char) || 0) + 1)
9+
}
10+
11+
const requiredCnt = targetMap.size
12+
13+
let left = 0
14+
let right = 0
15+
let formed = 0
16+
const windowCounts = new Map<string, number>()
17+
18+
let minLen = Infinity
19+
let minLeft = 0
20+
let minRight = 0
21+
22+
while (right < s.length) {
23+
const rightChar = s[right]
24+
windowCounts.set(rightChar, (windowCounts.get(rightChar) || 0) + 1)
25+
if (targetMap.has(rightChar) &&
26+
windowCounts.get(rightChar) === targetMap.get(rightChar)) {
27+
formed++
28+
}
29+
30+
while (left <= right && formed == requiredCnt) {
31+
if (right - left + 1 < minLen) {
32+
minLen = right - left + 1
33+
minLeft = left
34+
minRight = right
35+
}
36+
const leftChar = s[left]
37+
windowCounts.set(leftChar, windowCounts.get(leftChar)! - 1)
38+
39+
if (targetMap.has(leftChar) && windowCounts.get(leftChar)! < targetMap.get(leftChar)) {
40+
formed--
41+
}
42+
left++
43+
}
44+
right++
45+
}
46+
return minLen === Infinity ? "" : s.substring(minLeft, minRight + 1)
47+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Time complexity : O(m * n)
3+
Space complexity : O(m * n)
4+
*/
5+
function pacificAtlantic(heights: number[][]): number[][] {
6+
const m = heights.length
7+
const n = heights[0].length
8+
const pacific: boolean[][] = Array(m).fill(null).map(() => Array(n).fill(false))
9+
const atlantic: boolean[][] = Array(m).fill(null).map(() => Array(n).fill(false))
10+
11+
const dfs = (row: number, col: number, visited: boolean[][], prevH: number): void => {
12+
if (row < 0 || row >= m || col < 0 || col >= n || visited[row][col] || heights[row][col] < prevH) {
13+
return
14+
}
15+
visited[row][col] = true
16+
const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]
17+
for (const [dr, dc] of directions) {
18+
dfs(row + dr, col + dc, visited, heights[row][col])
19+
}
20+
}
21+
22+
for (let row = 0; row < m; row++) {
23+
dfs(row, 0, pacific, heights[row][0])
24+
25+
}
26+
27+
for (let col = 0; col < n; col++) {
28+
dfs(0, col, pacific, heights[0][col])
29+
}
30+
31+
for (let row = 0; row < m; row++) {
32+
dfs(row, n - 1, atlantic, heights[row][n - 1])
33+
}
34+
35+
for (let col = 0; col < n; col++) {
36+
dfs(m - 1, col, atlantic, heights[m - 1][col])
37+
}
38+
39+
const result: number[][] = []
40+
for (let row = 0; row < m; row++) {
41+
for (let col = 0; col < n; col++) {
42+
if (pacific[row][col] && atlantic[row][col]) {
43+
result.push([row, col])
44+
}
45+
}
46+
}
47+
return result
48+
};

sum-of-two-integers/hoyeongkwak.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Time complexity : O(1)
3+
Space complexity : O(1)
4+
*/
5+
function getSum(a: number, b: number): number {
6+
let xor = a ^ b
7+
let carry = (a & b) << 1
8+
9+
while (carry !== 0) {
10+
const tempXor = xor ^ carry
11+
carry = (xor & carry) << 1
12+
xor = tempXor
13+
}
14+
return xor
15+
};

0 commit comments

Comments
 (0)