From 858b7402425aedcb02942478ac03ba227b35c720 Mon Sep 17 00:00:00 2001 From: BaeKwangho Date: Mon, 15 Dec 2025 23:00:55 +0900 Subject: [PATCH 1/2] week6 --- container-with-most-water/Baekwangho.ts | 43 ++++++++ .../Baekwangho.ts | 102 ++++++++++++++++++ valid-parentheses/Baekwangho.ts | 35 ++++++ 3 files changed, 180 insertions(+) create mode 100644 container-with-most-water/Baekwangho.ts create mode 100644 design-add-and-search-words-data-structure/Baekwangho.ts create mode 100644 valid-parentheses/Baekwangho.ts diff --git a/container-with-most-water/Baekwangho.ts b/container-with-most-water/Baekwangho.ts new file mode 100644 index 000000000..5fd8851a6 --- /dev/null +++ b/container-with-most-water/Baekwangho.ts @@ -0,0 +1,43 @@ +/** +f((a,ax), (b,bx)) => min(ax, bx) * (b - a) (단, b > a) +라고 하자 +(1,1) (2,8) => 1 +(1,1) (3,6) => 2 +(2,8) (3,6) => 6 +(1,1) (4,2) => 3 +(2,8) (4,2) => 4 +(3,6) (4,2) => 2 +... +(2,8) (5,5) => 15 + +근데 생각해보니, 순회하면서 가장 큰 왼쪽 봉을 구하고, 한칸씩 움직이며 그 봉으로부터의 너비를 계산하면 되는 것 같음. +그러다가 왼쪽 봉보다 더 큰 봉이 있다면 거기서부터 시작하되, 최댓값은 그대로 두기 + +를 생각했다가 보니, 투 포인터를 사용해서 가장 큰 수를 구하는 방법이 있을 것 같음. +힌트를 보니, 낮은 바를 항상 움직이라고 함. + +시간 복잡도: o(n) / 공간 복잡도: o(1) +*/ +function maxArea(height: number[]): number { + let left = 0; + let right = height.length - 1; + let max = 0; + + while (left < right) { + const ltemp = height[left]; + const rtemp = height[right]; + + if (ltemp < rtemp) { + left++; + } else { + right--; + } + + const result = (right - left + 1) * Math.min(ltemp, rtemp); + if (result > max) { + max = result; + } + } + + return max; +} diff --git a/design-add-and-search-words-data-structure/Baekwangho.ts b/design-add-and-search-words-data-structure/Baekwangho.ts new file mode 100644 index 000000000..66e3157b6 --- /dev/null +++ b/design-add-and-search-words-data-structure/Baekwangho.ts @@ -0,0 +1,102 @@ +/** +search 의 역할을 보았을 때, 각 글자의 길이별로 set 을 두는 형태로 구현해볼 수 있을 듯 하다. +class WordDictionary { + setArray: Array> + wordSet: Set + constructor() { + this.setArray = Array.from({length:25}, () => new Set()) + this.wordSet = new Set() + } + + addWord(word: string): void { + for(let i=0; i + call(el, subset!.slice(1, subset!.length)) + ); + } else { + const firstCharDict = dict[firstChar]; + if (!firstCharDict) return false; + + return call(firstCharDict, subset!.slice(1, subset!.length)); + } + } + + return call(this.headMap, word); + } +} + +/** + * Your WordDictionary object will be instantiated and called as such: + * var obj = new WordDictionary() + * obj.addWord(word) + * var param_2 = obj.search(word) + */ diff --git a/valid-parentheses/Baekwangho.ts b/valid-parentheses/Baekwangho.ts new file mode 100644 index 000000000..600b1e700 --- /dev/null +++ b/valid-parentheses/Baekwangho.ts @@ -0,0 +1,35 @@ +/** + * 공간 복잡도: o(n) 시간 복잡도: o(n) + */ +function isValid(s: string): boolean { + const stack = []; + + const start = ["(", "{", "["]; + + for (let i = 0; i < s.length; i++) { + if (start.includes(s[i])) { + stack.push(s[i]); + } else { + const last = stack.pop(); + switch (last) { + case "(": { + if (s[i] !== ")") return false; + break; + } + case "{": { + if (s[i] !== "}") return false; + break; + } + case "[": { + if (s[i] !== "]") return false; + break; + } + default: { + return false; + } + } + } + } + + return stack.length > 0 ? false : true; +} From 1204a0a222b6f2a0fbada2b6dc7d45674b9991ba Mon Sep 17 00:00:00 2001 From: BaeKwangho Date: Thu, 18 Dec 2025 00:00:32 +0900 Subject: [PATCH 2/2] week 06 --- longest-increasing-subsequence/Baekwangho.ts | 0 spiral-matrix/Baekwangho.ts | 86 ++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 longest-increasing-subsequence/Baekwangho.ts create mode 100644 spiral-matrix/Baekwangho.ts diff --git a/longest-increasing-subsequence/Baekwangho.ts b/longest-increasing-subsequence/Baekwangho.ts new file mode 100644 index 000000000..e69de29bb diff --git a/spiral-matrix/Baekwangho.ts b/spiral-matrix/Baekwangho.ts new file mode 100644 index 000000000..15a1578a9 --- /dev/null +++ b/spiral-matrix/Baekwangho.ts @@ -0,0 +1,86 @@ +/** +(0,0) 에서 시작해서, 위 아래 끝값 및 왼쪽 오른쪽의 끝값을 갱신해가며 +현재 위치가 이보다 커질 경우 각각을 -1 하고 방향을 바꿔준다. + */ +function spiralOrder(matrix: number[][]): number[] { + let hmin = 0, + hmax = matrix[0].length - 1, + vmin = 0, + vmax = matrix.length - 1; + let x = 0, + y = 0; + const result = []; + let currentStep = [1, 0]; + + while (hmax >= hmin && vmax >= vmin) { + result.push(matrix[y][x]); + + const next = applyCurrentStep(currentStep, x, y, hmin, hmax, vmin, vmax); + currentStep = next.nextStep; + x = next.x; + y = next.y; + hmin = next.hmin; + hmax = next.hmax; + vmin = next.vmin; + vmax = next.vmax; + + // console.log(currentStep, x, y, hmin, hmax, vmin, vmax) + } + return result; +} + +function applyCurrentStep( + currentStep: number[], + x: number, + y: number, + hmin: number, + hmax: number, + vmin: number, + vmax: number +) { + let nextStep = currentStep; + + switch (true) { + case currentStep[0] === 1 && currentStep[1] === 0: { + if (x >= hmax) { + nextStep = [0, 1]; + y += 1; + vmin += 1; + } else { + x += 1; + } + break; + } + case currentStep[0] === 0 && currentStep[1] === 1: { + if (y >= vmax) { + nextStep = [-1, 0]; + x -= 1; + hmax -= 1; + } else { + y += 1; + } + break; + } + case currentStep[0] === -1 && currentStep[1] === 0: { + if (hmin >= x) { + nextStep = [0, -1]; + y -= 1; + vmax -= 1; + } else { + x -= 1; + } + break; + } + case currentStep[0] === 0 && currentStep[1] === -1: { + if (vmin >= y) { + nextStep = [1, 0]; + x += 1; + hmin += 1; + } else { + y -= 1; + } + break; + } + } + return { nextStep, x, y, hmin, hmax, vmin, vmax }; +}