Skip to content

[HoonDongKang] Week 9 Solutions #1530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 31, 2025
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
44 changes: 44 additions & 0 deletions linked-list-cycle/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* [Problem]: [141] Linked List Cycle
* (https://leetcode.com/problems/linked-list-cycle/description/)
*/
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;
}
}

function hasCycle(head: ListNode | null): boolean {
//시간복잡도 O(n)
//공간복잡도 O(n)
function setFunc(head: ListNode | null): boolean {
if (!head) return false;
const set = new Set<ListNode>();
let currentNode: ListNode = head;

while (currentNode) {
if (set.has(currentNode)) return true;

set.add(currentNode);
currentNode = currentNode.next!;
}

return false;
}
//시간복잡도 O(n)
//공간복잡도 O(1)
function pointerFunc(head: ListNode | null): boolean {
let slow = head;
let fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;

if (slow === fast) return true;
}
return false;
}
}
43 changes: 43 additions & 0 deletions maximum-product-subarray/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* [Problem]: [152] Maximum Product Subarray
* (https://leetcode.com/problems/maximum-product-subarray/description/)
*/
function maxProduct(nums: number[]): number {
// 시간복잡도 O(n^2)
// 공간복잡도 O(1)
function bruteForce(nums: number[]): number {
let maxProduct = nums[0];

for (let i = 0; i < nums.length; i++) {
let product = 1;
for (let j = i; j < nums.length; j++) {
product *= nums[j];
maxProduct = Math.max(maxProduct, product);
}
}

return maxProduct;
}

// 시간복잡도 O(n)
// 공간복잡도 O(1)
function optimizedFunc(nums: number[]): number {
let maxResult = nums[0];
let minResult = nums[0];
let result = nums[0];

for (let i = 1; i < nums.length; i++) {
const num = nums[i];

if (num < 0) {
[maxResult, minResult] = [minResult, maxResult];
}

maxResult = Math.max(num, num * maxResult);
minResult = Math.min(num, num * minResult);
result = Math.max(result, maxResult);
}

return result;
}
}
88 changes: 88 additions & 0 deletions minimum-window-substring/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* [Problem]: [76] Minimum Window Substring
* (https://leetcode.com/problems/minimum-window-substring/description/)
*/
function minWindow(s: string, t: string): string {
//시간복잡도 O(n)
//공간복잡도 O(n)
function windowFunc(s: string, t: string): string {
let left = 0;
let right = 0;
let s_map = new Map<string, number>();
let t_map = new Map<string, number>();
let minCount = Infinity;
let minStart = 0;

for (const char of t) {
t_map.set(char, (t_map.get(char) || 0) + 1);
}

while (right < s.length) {
let char = s[right];
s_map.set(char, (s_map.get(char) || 0) + 1);
right++;

while (isValid(s_map)) {
if (right - left < minCount) {
minCount = right - left;
minStart = left;
}

let char = s[left];
s_map.set(char, s_map.get(char)! - 1);
left++;
}
}

return minCount === Infinity ? "" : s.slice(minStart, minStart + minCount);

function isValid(map: Map<string, number>): boolean {
for (let i of t_map.keys()) {
if ((map.get(i) || 0) < t_map.get(i)!) return false;
}
return true;
}
}

//시간복잡도 O(n)
//공간복잡도 O(n)
function optimizedFunc(s: string, t: string): string {
const map = new Map<string, number>();
let required = t.length;
let left = 0;
let minLen = Infinity;
let minStart = 0;

for (const char of t) {
map.set(char, (map.get(char) || 0) + 1);
}

for (let right = 0; right < s.length; right++) {
const char = s[right];

if (map.has(char)) {
const count = map.get(char)!;
if (0 < count) required--;
map.set(char, count - 1);
}

while (required === 0) {
const char = s[left];

if (right - left + 1 < minLen) {
minLen = right - left + 1;
minStart = left;
}

if (map.has(char)) {
map.set(char, map.get(char)! + 1);
if (map.get(char)! > 0) {
required++;
}
}
left++;
}
}
return minLen === Infinity ? "" : s.slice(minStart, minStart + minLen);
}
}
53 changes: 53 additions & 0 deletions pacific-atlantic-water-flow/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* [Problem]: [417] Pacific Atlantic Water Flow
* (https://leetcode.com/problems/pacific-atlantic-water-flow/description/)
*/
function pacificAtlantic(heights: number[][]): number[][] {
//시간복잡도 O(n)
//공간복잡도 O(n)
const result: number[][] = [];
const rows = heights.length;
const cols = heights[0].length;

const pacificArea: boolean[][] = Array.from({ length: rows }, () => Array(cols).fill(false));
const atlanticArea: boolean[][] = Array.from({ length: rows }, () => Array(cols).fill(false));

function dfs(visited: boolean[][], row: number, col: number) {
if (row < 0 || col < 0 || row >= rows || col >= cols) return;
if (visited[row][col]) return;

visited[row][col] = true;

[
[row - 1, col],
[row + 1, col],
[row, col - 1],
[row, col + 1],
].forEach(([r, c]) => {
if (r >= 0 && c >= 0 && r < rows && c < cols) {
if (heights[r][c] >= heights[row][col]) {
dfs(visited, r, c);
}
}
});
}

for (let i = 0; i < rows; i++) {
dfs(pacificArea, i, 0);
dfs(atlanticArea, i, cols - 1);
}
for (let j = 0; j < cols; j++) {
dfs(pacificArea, 0, j);
dfs(atlanticArea, rows - 1, j);
}

for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (pacificArea[i][j] && atlanticArea[i][j]) {
result.push([i, j]);
}
}
}

return result;
}
14 changes: 14 additions & 0 deletions sum-of-two-integers/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* [Problem]: [371] Sum of Two Integers
* (https://leetcode.com/problems/sum-of-two-integers/description/)
*/
function getSum(a: number, b: number): number {
// 시간복잡도 O(n)
// 공간복잡도 O(1)
while (b !== 0) {
const carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}