From 21cd0ff14f059c6523bbb0ffcdd4e84cf596a0b0 Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Sat, 5 Oct 2024 16:49:12 +0900 Subject: [PATCH 1/5] 1. Linked List Cycle --- linked-list-cycle/sunjae95.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 linked-list-cycle/sunjae95.js diff --git a/linked-list-cycle/sunjae95.js b/linked-list-cycle/sunjae95.js new file mode 100644 index 000000000..fc92e171c --- /dev/null +++ b/linked-list-cycle/sunjae95.js @@ -0,0 +1,21 @@ +/** + * @description + * brainstorming: + * hash table + * + * n = length of head + * time complexity: O(n) + * space complexity: O(n) + */ +var hasCycle = function (head) { + const set = new Set(); + let node = head; + + while (node) { + if (set.has(node)) return true; + set.add(node); + node = node.next; + } + + return false; +}; From 85aef7c25ad9b1f9c2521f4053d8b43b276f9e6b Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Sun, 6 Oct 2024 20:00:53 +0900 Subject: [PATCH 2/5] 2. Find Minimum in Rotated Sorted Array --- .../sunjae95.js | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/sunjae95.js diff --git a/find-minimum-in-rotated-sorted-array/sunjae95.js b/find-minimum-in-rotated-sorted-array/sunjae95.js new file mode 100644 index 000000000..228924a70 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/sunjae95.js @@ -0,0 +1,32 @@ +/** + * @description + * brainstorming: + * brute force + * + * n = length of head + * time complexity: O(n) + * space complexity: O(1) + */ +var findMin = function (nums) { + let answer = 5000; + nums.forEach((num) => (answer = Math.min(answer, num))); + + return answer; +}; + +/* n = length of head + * time complexity: O(n) + * space complexity: O(1) + */ +var findMin = function (nums) { + let answer = nums[0]; + if (nums.length === 1) return answer; + if (answer < nums[nums.length - 1]) return answer; + + for (let i = nums.length - 1; i >= 0; i--) { + if (answer < nums[i]) return answer; + answer = nums[i]; + } + + return answer; +}; From bc4e7e527b7a4222de7409eefcab310a056ea432 Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Sun, 6 Oct 2024 21:25:50 +0900 Subject: [PATCH 3/5] 3. Pacific Atlantic Water Flow --- pacific-atlantic-water-flow/sunjae95.js | 74 +++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 pacific-atlantic-water-flow/sunjae95.js diff --git a/pacific-atlantic-water-flow/sunjae95.js b/pacific-atlantic-water-flow/sunjae95.js new file mode 100644 index 000000000..7a9bf442d --- /dev/null +++ b/pacific-atlantic-water-flow/sunjae95.js @@ -0,0 +1,74 @@ +/** + * @description + * brainstorming: + * bfs + memoization + * + * n = length of height + * m = length of height[i] + * time complexity: O(n*m * 4^n*m) + * space complexity: O(n*m) + */ +var pacificAtlantic = function (heights) { + const visited = Array.from({ length: heights.length }, (_, i) => + Array.from({ length: heights[i].length }, () => false) + ); + const memo = new Map(); + + const bfs = (row, column) => { + const dr = [0, 0, -1, 1]; + const dc = [1, -1, 0, 0]; + const queue = [[row, column]]; + let [pacific, atlantic] = [false, false]; + let queueIndex = 0; + + while (queueIndex !== queue.length) { + const currentLastLength = queue.length; + + while (queueIndex !== currentLastLength) { + const [r, c] = queue[queueIndex++]; + visited[r][c] = `${row},${column}`; + + if (memo.has(`${r},${c}`)) { + memo.set(`${row},${column}`, [row, column]); + return; + } + + for (let i = 0; i < 4; i++) { + const nextR = r + dr[i]; + const nextC = c + dc[i]; + const isPacific = nextR === -1 || nextC === -1; + const isAtlantic = + nextR === heights.length || nextC === heights[0].length; + + if (isPacific) { + pacific = true; + continue; + } + if (isAtlantic) { + atlantic = true; + continue; + } + + if (visited[nextR][nextC] === `${row},${column}`) continue; + + if (heights[r][c] < heights[nextR][nextC]) continue; + + queue.push([nextR, nextC]); + } + } + + if (pacific && atlantic) { + memo.set(`${row},${column}`, [row, column]); + return; + } + } + }; + + for (let row = 0; row < heights.length; row++) { + for (let column = 0; column < heights[row].length; column++) { + bfs(row, column); + } + } + + return [...memo.values()]; +}; From 777ffec05a8cebcbe0c10684f14df98239987dcf Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Sun, 6 Oct 2024 22:14:01 +0900 Subject: [PATCH 4/5] 4. Maximum Subarray --- maximum-subarray/sunjae95.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 maximum-subarray/sunjae95.js diff --git a/maximum-subarray/sunjae95.js b/maximum-subarray/sunjae95.js new file mode 100644 index 000000000..02dd160e4 --- /dev/null +++ b/maximum-subarray/sunjae95.js @@ -0,0 +1,23 @@ +/** + * @description + * n = length of nums + * time complexity: O(n) + * space complexity: O(1) + */ +var maxSubArray = function (nums) { + let sum = 0; + let answer = nums[0]; + + for (let end = 0; end < nums.length; end++) { + if (sum < 0) { + sum = nums[end]; + } else if (sum + nums[end] >= 0) { + sum += nums[end]; + } else { + sum = nums[end]; + } + + answer = Math.max(answer, sum); + } + return answer; +}; From 86214303ca127b9b1f2f20bc761cbeff1b39fcca Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Mon, 7 Oct 2024 00:41:57 +0900 Subject: [PATCH 5/5] 5. Minimum Window Substring --- minimum-window-substring/sunjae.js | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 minimum-window-substring/sunjae.js diff --git a/minimum-window-substring/sunjae.js b/minimum-window-substring/sunjae.js new file mode 100644 index 000000000..4c8ffa6e1 --- /dev/null +++ b/minimum-window-substring/sunjae.js @@ -0,0 +1,41 @@ +/** + * @description + * brainstorming: + * two pointer + hash table + * + * n = length of s + * m = length of t + * time complexity: O(n*n) + * space complexity: O(n) + */ +var minWindow = function (s, t) { + const requiredMap = t.split("").reduce((map, char) => { + map.set(char, (map.get(char) ?? 0) + 1); + return map; + }, new Map()); + const requiredArray = [...requiredMap.entries()]; + const map = new Map(); + const successRequired = () => + requiredArray.every(([key, value]) => map.get(key) >= value); + + let answer = ""; + let start = 0; + + for (let i = 0; i < s.length; i++) { + if (requiredMap.has(s[i])) map.set(s[i], (map.get(s[i]) ?? 0) + 1); + + while (successRequired()) { + const now = s.slice(start, i + 1); + answer = answer === "" || answer.length >= now.length ? now : answer; + + if (map.has(s[start])) { + map.set(s[start], map.get(s[start]) - 1); + if (map.get(s[start]) === -1) map.delete(s[start]); + } + + start++; + } + } + + return answer; +};