Skip to content

[hoyeongkwak\ Week 09 Solutions #1532

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 1 commit into from
Jun 2, 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
28 changes: 28 additions & 0 deletions linked-list-cycle/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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)
* }
* }
*/
/*
Time complexity : O(n)
Space complexity : O(1)
*/
function hasCycle(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
};
16 changes: 16 additions & 0 deletions maximum-product-subarray/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Time complexity : O(n)
Space complexity : O(1)
*/
function maxProduct(nums: number[]): number {
let max = 1
let min = 1
let result = nums[0]
nums.forEach((num) => {
const tempMax = Math.max(num, max * num, min * num)
min = Math.min(num, max * num, min * num)
max = tempMax
result = Math.max(result, max)
})
return result
}
47 changes: 47 additions & 0 deletions minimum-window-substring/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Time complexity: O(s+t)
Space complexity: O(s+t)
*/
function minWindow(s: string, t: string): string {
const targetMap = new Map<string, number>()
for (const char of t) {
targetMap.set(char, (targetMap.get(char) || 0) + 1)
}

const requiredCnt = targetMap.size

let left = 0
let right = 0
let formed = 0
const windowCounts = new Map<string, number>()

let minLen = Infinity
let minLeft = 0
let minRight = 0

while (right < s.length) {
const rightChar = s[right]
windowCounts.set(rightChar, (windowCounts.get(rightChar) || 0) + 1)
if (targetMap.has(rightChar) &&
windowCounts.get(rightChar) === targetMap.get(rightChar)) {
formed++
}

while (left <= right && formed == requiredCnt) {
if (right - left + 1 < minLen) {
minLen = right - left + 1
minLeft = left
minRight = right
}
const leftChar = s[left]
windowCounts.set(leftChar, windowCounts.get(leftChar)! - 1)

if (targetMap.has(leftChar) && windowCounts.get(leftChar)! < targetMap.get(leftChar)) {
formed--
}
left++
}
right++
}
return minLen === Infinity ? "" : s.substring(minLeft, minRight + 1)
};
48 changes: 48 additions & 0 deletions pacific-atlantic-water-flow/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Time complexity : O(m * n)
Space complexity : O(m * n)
*/
function pacificAtlantic(heights: number[][]): number[][] {
const m = heights.length
const n = heights[0].length
const pacific: boolean[][] = Array(m).fill(null).map(() => Array(n).fill(false))
const atlantic: boolean[][] = Array(m).fill(null).map(() => Array(n).fill(false))

const dfs = (row: number, col: number, visited: boolean[][], prevH: number): void => {
if (row < 0 || row >= m || col < 0 || col >= n || visited[row][col] || heights[row][col] < prevH) {
return
}
visited[row][col] = true
const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]
for (const [dr, dc] of directions) {
dfs(row + dr, col + dc, visited, heights[row][col])
}
}

for (let row = 0; row < m; row++) {
dfs(row, 0, pacific, heights[row][0])

}

for (let col = 0; col < n; col++) {
dfs(0, col, pacific, heights[0][col])
}

for (let row = 0; row < m; row++) {
dfs(row, n - 1, atlantic, heights[row][n - 1])
}

for (let col = 0; col < n; col++) {
dfs(m - 1, col, atlantic, heights[m - 1][col])
}

const result: number[][] = []
for (let row = 0; row < m; row++) {
for (let col = 0; col < n; col++) {
if (pacific[row][col] && atlantic[row][col]) {
result.push([row, col])
}
}
}
return result
};
15 changes: 15 additions & 0 deletions sum-of-two-integers/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Time complexity : O(1)
Space complexity : O(1)
*/
function getSum(a: number, b: number): number {
let xor = a ^ b
let carry = (a & b) << 1

while (carry !== 0) {
const tempXor = xor ^ carry
carry = (xor & carry) << 1
xor = tempXor
}
return xor
};