Skip to content

Commit f0ab0bd

Browse files
authored
Merge pull request #1530 from HoonDongKang/main
[HoonDongKang] Week 9 Solutions
2 parents 790b428 + 11f3506 commit f0ab0bd

File tree

5 files changed

+242
-0
lines changed

5 files changed

+242
-0
lines changed

linked-list-cycle/HoonDongKang.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* [Problem]: [141] Linked List Cycle
3+
* (https://leetcode.com/problems/linked-list-cycle/description/)
4+
*/
5+
class ListNode {
6+
val: number;
7+
next: ListNode | null;
8+
constructor(val?: number, next?: ListNode | null) {
9+
this.val = val === undefined ? 0 : val;
10+
this.next = next === undefined ? null : next;
11+
}
12+
}
13+
14+
function hasCycle(head: ListNode | null): boolean {
15+
//시간복잡도 O(n)
16+
//공간복잡도 O(n)
17+
function setFunc(head: ListNode | null): boolean {
18+
if (!head) return false;
19+
const set = new Set<ListNode>();
20+
let currentNode: ListNode = head;
21+
22+
while (currentNode) {
23+
if (set.has(currentNode)) return true;
24+
25+
set.add(currentNode);
26+
currentNode = currentNode.next!;
27+
}
28+
29+
return false;
30+
}
31+
//시간복잡도 O(n)
32+
//공간복잡도 O(1)
33+
function pointerFunc(head: ListNode | null): boolean {
34+
let slow = head;
35+
let fast = head;
36+
while (fast && fast.next) {
37+
slow = slow.next;
38+
fast = fast.next.next;
39+
40+
if (slow === fast) return true;
41+
}
42+
return false;
43+
}
44+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* [Problem]: [152] Maximum Product Subarray
3+
* (https://leetcode.com/problems/maximum-product-subarray/description/)
4+
*/
5+
function maxProduct(nums: number[]): number {
6+
// 시간복잡도 O(n^2)
7+
// 공간복잡도 O(1)
8+
function bruteForce(nums: number[]): number {
9+
let maxProduct = nums[0];
10+
11+
for (let i = 0; i < nums.length; i++) {
12+
let product = 1;
13+
for (let j = i; j < nums.length; j++) {
14+
product *= nums[j];
15+
maxProduct = Math.max(maxProduct, product);
16+
}
17+
}
18+
19+
return maxProduct;
20+
}
21+
22+
// 시간복잡도 O(n)
23+
// 공간복잡도 O(1)
24+
function optimizedFunc(nums: number[]): number {
25+
let maxResult = nums[0];
26+
let minResult = nums[0];
27+
let result = nums[0];
28+
29+
for (let i = 1; i < nums.length; i++) {
30+
const num = nums[i];
31+
32+
if (num < 0) {
33+
[maxResult, minResult] = [minResult, maxResult];
34+
}
35+
36+
maxResult = Math.max(num, num * maxResult);
37+
minResult = Math.min(num, num * minResult);
38+
result = Math.max(result, maxResult);
39+
}
40+
41+
return result;
42+
}
43+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* [Problem]: [76] Minimum Window Substring
3+
* (https://leetcode.com/problems/minimum-window-substring/description/)
4+
*/
5+
function minWindow(s: string, t: string): string {
6+
//시간복잡도 O(n)
7+
//공간복잡도 O(n)
8+
function windowFunc(s: string, t: string): string {
9+
let left = 0;
10+
let right = 0;
11+
let s_map = new Map<string, number>();
12+
let t_map = new Map<string, number>();
13+
let minCount = Infinity;
14+
let minStart = 0;
15+
16+
for (const char of t) {
17+
t_map.set(char, (t_map.get(char) || 0) + 1);
18+
}
19+
20+
while (right < s.length) {
21+
let char = s[right];
22+
s_map.set(char, (s_map.get(char) || 0) + 1);
23+
right++;
24+
25+
while (isValid(s_map)) {
26+
if (right - left < minCount) {
27+
minCount = right - left;
28+
minStart = left;
29+
}
30+
31+
let char = s[left];
32+
s_map.set(char, s_map.get(char)! - 1);
33+
left++;
34+
}
35+
}
36+
37+
return minCount === Infinity ? "" : s.slice(minStart, minStart + minCount);
38+
39+
function isValid(map: Map<string, number>): boolean {
40+
for (let i of t_map.keys()) {
41+
if ((map.get(i) || 0) < t_map.get(i)!) return false;
42+
}
43+
return true;
44+
}
45+
}
46+
47+
//시간복잡도 O(n)
48+
//공간복잡도 O(n)
49+
function optimizedFunc(s: string, t: string): string {
50+
const map = new Map<string, number>();
51+
let required = t.length;
52+
let left = 0;
53+
let minLen = Infinity;
54+
let minStart = 0;
55+
56+
for (const char of t) {
57+
map.set(char, (map.get(char) || 0) + 1);
58+
}
59+
60+
for (let right = 0; right < s.length; right++) {
61+
const char = s[right];
62+
63+
if (map.has(char)) {
64+
const count = map.get(char)!;
65+
if (0 < count) required--;
66+
map.set(char, count - 1);
67+
}
68+
69+
while (required === 0) {
70+
const char = s[left];
71+
72+
if (right - left + 1 < minLen) {
73+
minLen = right - left + 1;
74+
minStart = left;
75+
}
76+
77+
if (map.has(char)) {
78+
map.set(char, map.get(char)! + 1);
79+
if (map.get(char)! > 0) {
80+
required++;
81+
}
82+
}
83+
left++;
84+
}
85+
}
86+
return minLen === Infinity ? "" : s.slice(minStart, minStart + minLen);
87+
}
88+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* [Problem]: [417] Pacific Atlantic Water Flow
3+
* (https://leetcode.com/problems/pacific-atlantic-water-flow/description/)
4+
*/
5+
function pacificAtlantic(heights: number[][]): number[][] {
6+
//시간복잡도 O(n)
7+
//공간복잡도 O(n)
8+
const result: number[][] = [];
9+
const rows = heights.length;
10+
const cols = heights[0].length;
11+
12+
const pacificArea: boolean[][] = Array.from({ length: rows }, () => Array(cols).fill(false));
13+
const atlanticArea: boolean[][] = Array.from({ length: rows }, () => Array(cols).fill(false));
14+
15+
function dfs(visited: boolean[][], row: number, col: number) {
16+
if (row < 0 || col < 0 || row >= rows || col >= cols) return;
17+
if (visited[row][col]) return;
18+
19+
visited[row][col] = true;
20+
21+
[
22+
[row - 1, col],
23+
[row + 1, col],
24+
[row, col - 1],
25+
[row, col + 1],
26+
].forEach(([r, c]) => {
27+
if (r >= 0 && c >= 0 && r < rows && c < cols) {
28+
if (heights[r][c] >= heights[row][col]) {
29+
dfs(visited, r, c);
30+
}
31+
}
32+
});
33+
}
34+
35+
for (let i = 0; i < rows; i++) {
36+
dfs(pacificArea, i, 0);
37+
dfs(atlanticArea, i, cols - 1);
38+
}
39+
for (let j = 0; j < cols; j++) {
40+
dfs(pacificArea, 0, j);
41+
dfs(atlanticArea, rows - 1, j);
42+
}
43+
44+
for (let i = 0; i < rows; i++) {
45+
for (let j = 0; j < cols; j++) {
46+
if (pacificArea[i][j] && atlanticArea[i][j]) {
47+
result.push([i, j]);
48+
}
49+
}
50+
}
51+
52+
return result;
53+
}

sum-of-two-integers/HoonDongKang.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* [Problem]: [371] Sum of Two Integers
3+
* (https://leetcode.com/problems/sum-of-two-integers/description/)
4+
*/
5+
function getSum(a: number, b: number): number {
6+
// 시간복잡도 O(n)
7+
// 공간복잡도 O(1)
8+
while (b !== 0) {
9+
const carry = a & b;
10+
a = a ^ b;
11+
b = carry << 1;
12+
}
13+
return a;
14+
}

0 commit comments

Comments
 (0)