From 34258fadd551b06b31abaeb0b13b020dee478931 Mon Sep 17 00:00:00 2001 From: hyejj19 Date: Mon, 30 Mar 2026 11:00:59 +0900 Subject: [PATCH 1/3] solve best-time-to-buy-and-sell-stock --- best-time-to-buy-and-sell-stock/hyejj19.ts | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/hyejj19.ts diff --git a/best-time-to-buy-and-sell-stock/hyejj19.ts b/best-time-to-buy-and-sell-stock/hyejj19.ts new file mode 100644 index 0000000000..9c66553811 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/hyejj19.ts @@ -0,0 +1,26 @@ +function maxProfit(prices: number[]): number { + let minPrice = Number.MAX_SAFE_INTEGER; + let maxProfit = 0; + + for (let p of prices) { + if (minPrice >= p) { + minPrice = p; + continue; + } else { + let profit = p - minPrice; + if (profit > maxProfit) maxProfit = profit; + } + } + return maxProfit; +} + +function maxProfit(prices: number[]): number { + let minPrice = Infinity; + let maxProfit = 0; + + for (let p of prices) { + minPrice = Math.min(minPrice, p); + maxProfit = Math.max(maxProfit, p - minPrice); + } + return maxProfit; +} From fcf4286851c32150a4662cbd0a25fd4415061da1 Mon Sep 17 00:00:00 2001 From: hyejj19 Date: Wed, 8 Apr 2026 14:54:49 +0900 Subject: [PATCH 2/3] solve valid-parentheses --- valid-parentheses/hyejj19.ts | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 valid-parentheses/hyejj19.ts diff --git a/valid-parentheses/hyejj19.ts b/valid-parentheses/hyejj19.ts new file mode 100644 index 0000000000..49a6f5f574 --- /dev/null +++ b/valid-parentheses/hyejj19.ts @@ -0,0 +1,56 @@ +function isValid(s: string): boolean { + const stack = []; + const open = '([{'; + for (const v of s) { + if (open.includes(v)) { + stack.push(v); + } else { + if ( + (v === ')' && stack.at(-1) === '(') || + (v === '}' && stack.at(-1) === '{') || + (v === ']' && stack.at(-1) === '[') + ) { + // 일치하는 닫는 괄호라면 pop + stack.pop(); + } else { + // 일치하지 않는 닫는 괄호라면 false + return false; + } + } + } + + return stack.length === 0 ? true : false; +} + +function isValid(s: string): boolean { + const pairs = new Map([ + [')', '('], + ['}', '{'], + [']', '['], + ]); + const stack = []; + + for (const v of s) { + // v 가 닫는 괄호이면 + if (pairs.has(v)) { + if (pairs.get(v) === stack.at(-1)) { + // 일치하는 닫는 괄호라면 pop + stack.pop(); + } else { + // 일치하지 않는 닫는 괄호라면 false + return false; + } + } else { + // 여는 괄호이면 + stack.push(v); + } + } + + return stack.length === 0; +} + +/* +1. 여는 괄호에 해당한다 -> 스택에 push +2. 닫는 괄호가 나왔다 -> 스택의 top과 일치하면 pop 아니라면 false +3. 스택이 비어있다 -> true + */ From bed992d1d5668921009db26102c66071b28a6813 Mon Sep 17 00:00:00 2001 From: hyejj19 Date: Wed, 8 Apr 2026 15:27:16 +0900 Subject: [PATCH 3/3] solve container-with-most-water --- container-with-most-water/hyejj19.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 container-with-most-water/hyejj19.ts diff --git a/container-with-most-water/hyejj19.ts b/container-with-most-water/hyejj19.ts new file mode 100644 index 0000000000..769c4e6710 --- /dev/null +++ b/container-with-most-water/hyejj19.ts @@ -0,0 +1,23 @@ +function maxArea(height: number[]): number { + let left = 0; + let right = height.length - 1; + let max = -1; + + while (right > left) { + const v = Math.min(height[left], height[right]) * (right - left); + max = Math.max(max, v); + if (height[left] >= height[right]) { + right--; + } else { + left++; + } + } + return max; +} + +/* +1. Math.min(height[left], height[right]) * (right - left); +2. left 와 right 를 양 끝에서 가리킴 +3. 너비를 계산해서 max 값을 갱신하고, 둘 중 더 짧은 쪽을 안쪽으로 이동 +4. left 와 right 가 만나면 종료 + */