From f6e95c8ec7009e9bdb92dfa9aba153222faf7e1f Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Sun, 2 Feb 2025 00:44:50 +0900 Subject: [PATCH 1/6] minimum-window-substring solution --- minimum-window-substring/jdy8738.js | 68 +++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 minimum-window-substring/jdy8738.js diff --git a/minimum-window-substring/jdy8738.js b/minimum-window-substring/jdy8738.js new file mode 100644 index 000000000..3d3bc9e9c --- /dev/null +++ b/minimum-window-substring/jdy8738.js @@ -0,0 +1,68 @@ +/** + * @param {string} s + * @param {string} t + * @return {string} + */ +var minWindow = function (s, t) { + let start = 0; + let end = 0; + + /** t의 모든 문자열이 슬라이드 윈도우의 범위에 들어왔을 때, 그 범위의 최소 길이 */ + let min = s.length; + + /** while 문이 한 번 순회할 때, start 또는 end값 중 end값이 증가했는지 여부 */ + let isEndIndexUp = true; + + /** min 값이 갱신되면 s문자열의 start, end 인덱스값을 저장할 변수 */ + let index = null; + + /** 문자열 t가 보유한 문자와 해당 문자가 들어있는 수를 key, value로 설정한 map객체 */ + const charMap = t.split('').reduce((acc, cur) => { + if (acc.has(cur)) { + acc.set(cur, acc.get(cur) + 1); + } else { + acc.set(cur, 1); + } + + return acc; + }, new Map()); + + while (end < s.length) { + const curChar = s[end]; + + if (isEndIndexUp && t.includes(curChar)) { + // end가 증감되었고 s의 end 인덱스의 문자가 t에 존재한다면 해당 키의 값에 1을 감소 + charMap.set(curChar, charMap.get(curChar) - 1); + } + + /** 모든 t의 문자들이 슬라이드 윈도우의 범위에 들어왔는지 여부를 체크하는 불린값 */ + const everyCharCollected = [...charMap].every(([_, value]) => value <= 0); + + if (everyCharCollected) { + // 모든 문자열이 슬라이드 윈도우의 문자열에 포착된 경우 + if (t.includes(s[start])) { + // s의 start 인덱스 문자가 t에 존재한다면 charMap에 해당 키의 값에 1을 증감 + charMap.set(s[start], (charMap.get(s[start]) || 0) + 1); + } + + const gap = end - start; + if (gap < min) { + // t의 모든 문자가 슬라이드 윈도우의 범위에 있고, 현재 그 윈도우의 길이가 이전의 min값 보다 작다면 업데이트 + min = gap; + index = [start, end]; + } + + start++; + isEndIndexUp = false; + } else { + // 윈도우에 문자열이 하나라도 부족한 경우 + end++; + isEndIndexUp = true; + } + } + + return index ? s.slice(index[0], index[1] + 1) : ''; +}; + +// 공간복잡도 O(t) -> 최대 t의 길이에 따라 맵의 크기가 결정되므로 +// 시간복잡도 O(s * t) -> s를 순회하는 while 문에서 최대 O(t)의 공간복잡도를 따르는 map 객체가 배열로 변환되어 every 메소드가 호출되기 때문에 From e682e7b3409ada3604d76147ee5818ac06864c76 Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Sun, 2 Feb 2025 00:47:30 +0900 Subject: [PATCH 2/6] minimum-window-substring solution --- minimum-window-substring/{jdy8738.js => jdy8739.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename minimum-window-substring/{jdy8738.js => jdy8739.js} (100%) diff --git a/minimum-window-substring/jdy8738.js b/minimum-window-substring/jdy8739.js similarity index 100% rename from minimum-window-substring/jdy8738.js rename to minimum-window-substring/jdy8739.js From 090ccc560feecf077899aa12f4909917b443992c Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Mon, 3 Feb 2025 01:36:10 +0900 Subject: [PATCH 3/6] liked-list-cycle --- linked-list-cycle/jdy8739.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 linked-list-cycle/jdy8739.js diff --git a/linked-list-cycle/jdy8739.js b/linked-list-cycle/jdy8739.js new file mode 100644 index 000000000..99718b900 --- /dev/null +++ b/linked-list-cycle/jdy8739.js @@ -0,0 +1,35 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ + +/** + * @param {ListNode} head + * @return {boolean} + */ +var hasCycle = function (head) { + if (!head?.next) { + return false; + } + + let current = head; + + const set = new Set(); + + while (current.next) { + if (set.has(current)) { + return true; + } + + set.add(current); + current = current.next; + } + + return false; +}; + +// 시간복잡도 O(n) -> 최대 리스트의 노드 수 만큼 while문이 반복되므로 +// 공간복잡도 O(n) -> set에 최대 리스트의 노드 수 만큼 size가 증가되므로 From 66c3e3af5d171fac8c39fd4a0de34fde61014fa3 Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Tue, 4 Feb 2025 00:26:00 +0900 Subject: [PATCH 4/6] find-minimum-in-rotated-sorted-array solution --- .../jdy8739.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/jdy8739.js diff --git a/find-minimum-in-rotated-sorted-array/jdy8739.js b/find-minimum-in-rotated-sorted-array/jdy8739.js new file mode 100644 index 000000000..3dde69c23 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/jdy8739.js @@ -0,0 +1,23 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var findMin = function (nums) { + while (1 < nums.length) { + + const mid = Math.floor(nums.length / 2); + + const firstToHalf = [...nums].slice(0, mid); + + const midToEnd = [...nums].slice(mid, nums.length); + + const isInFront = Math.min(...firstToHalf) < Math.min(...midToEnd); + + nums = isInFront ? firstToHalf : midToEnd; + } + + return nums[0]; +}; + +// 시간복잡도 0(logn) -> 이진탐색을 통해 배열 nums를 반으로 쪼개가며 해답을 찾기때문에 +// 공간복잡도 O(n) -> 처음 while문이 돌 때, nums를 쪼갠 두 배열 총 길이의 합은 nums의 처음 길이와 같기때문에 From 284e10e8d68ec8c5535e5a84656aacd3a1d295cb Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Wed, 5 Feb 2025 00:58:56 +0900 Subject: [PATCH 5/6] maximum-product-subarray --- maximum-product-subarray/jdy8739.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 maximum-product-subarray/jdy8739.js diff --git a/maximum-product-subarray/jdy8739.js b/maximum-product-subarray/jdy8739.js new file mode 100644 index 000000000..e9685f48d --- /dev/null +++ b/maximum-product-subarray/jdy8739.js @@ -0,0 +1,24 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var maxProduct = function (nums) { + let answer = nums[0]; + let max = 1; + let min = 1; + + for (let i = 0; i < nums.length; i++) { + const current = nums[i]; + + const candidates = [max * current, min * current, current]; + + max = Math.max(...candidates); + min = Math.min(...candidates); + answer = Math.max(answer, max); + } + + return answer; +}; + +// 시간복잡도 O(n) * O(8) -> nums의 길이 만큼을 for 문으로 순환하면서 Math클래스의 max, min메소드를 호출(인자가 3개, 3개, 2개 이므로 총 8회 순회) +// 공간복잡도 O(1) -> 파라미터 nums에 대해 의미있는 공간복잡도를 가지는 변수할당이 없음 From 024f8d31bbc7906a22d38ec8274e8c0174f9690c Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Thu, 6 Feb 2025 23:30:37 +0900 Subject: [PATCH 6/6] linked-list-cycle solution --- linked-list-cycle/jdy8739.js | 68 ++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/linked-list-cycle/jdy8739.js b/linked-list-cycle/jdy8739.js index 99718b900..6fda3cc38 100644 --- a/linked-list-cycle/jdy8739.js +++ b/linked-list-cycle/jdy8739.js @@ -33,3 +33,71 @@ var hasCycle = function (head) { // 시간복잡도 O(n) -> 최대 리스트의 노드 수 만큼 while문이 반복되므로 // 공간복잡도 O(n) -> set에 최대 리스트의 노드 수 만큼 size가 증가되므로 + + +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ + +/** + * @param {ListNode} head + * @return {boolean} + */ +var hasCycle = function(head) { + if (!head?.next) { + return false; + } + + let turtle = head; + let rabbit = head; + + while (turtle.next && rabbit.next) { + if (turtle === rabbit) { + return true; + } + + turtle = turtle.next; + rabbit = rabbit.next.next; + } + + return false; +}; + +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ + +/** + * @param {ListNode} head + * @return {boolean} + */ +var hasCycle = function (head) { + if (!head?.next || !head?.next.next) { + return false; + } + + let turtle = head; + let rabbit = head; + + while (turtle && rabbit?.next) { + turtle = turtle.next; + rabbit = rabbit.next.next; + + if (turtle === rabbit) { + return true; + } + } + + return false; +}; + +// 시간복잡도 O(n) -> 최대 리스트의 노드 수 만큼 while문이 반복되므로 +// 공간복잡도 O(1) -> 고정된 두 변수 turtle, rabbit을 사용하기때문에 (거북이와 토끼 알고리즘 사용)