Skip to content

Commit

Permalink
Merge pull request #909 from Yjason-K/main
Browse files Browse the repository at this point in the history
[gomgom22] Week6
  • Loading branch information
Yjason-K authored Jan 18, 2025
2 parents 8ee2e60 + a34edf6 commit 7e7fc1f
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
34 changes: 34 additions & 0 deletions container-with-most-water/Yjason-K.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉด์ ์—์„œ ๊ฐ€์žฅ ํฐ ๋ฉด์  ๊ตฌํ•˜๊ธฐ.
*
* @param {number[]} height - x ์ถ• ๋ฐฉํ–ฅ์˜ ๋†’์ด ๋ฐฐ์—ด
* @returns {number} - ๊ฐ€์žฅ ๋„“์€ ๋ฉด์  ๋ฐ˜ํ™˜
*
* ์‹œ๊ฐ„ ๋ณต์žก๋„ O(n)
* - n์€ height ๋ฐฐ์—ด์˜ ๊ธธ์ด
*
* ๊ณต๊ฐ„ ๋ณต์žก๋„ O(1)
* - ํฌ์ธํ„ฐ ๋ฐ ๊ฒฐ๊ณผ๊ฐ’ ์ €์žฅ ๊ณต๊ฐ„๋งŒ ์‚ฌ์šฉ
*/
function maxArea(height: number[]): number {
let start = 0; // ์‹œ์ž‘ ํฌ์ธํ„ฐ ์ดˆ๊ธฐํ™”
let end = height.length - 1; // ๋ ํฌ์ธํ„ฐ ์ดˆ๊ธฐํ™”
let result = 0; // ์ตœ๋Œ€ ๋ฉด์  ์ €์žฅ

while (start < end) {
// ํ˜„์žฌ ๋ฉด์  ๊ณ„์‚ฐ
const currentArea = Math.min(height[start], height[end]) * (end - start);
// ์ตœ๋Œ€ ๋ฉด์  ์—…๋ฐ์ดํŠธ
result = Math.max(result, currentArea);

// ํฌ์ธํ„ฐ ์ด๋™ (๋†’์ด๊ฐ€ ๋‚ฎ์€ ์ชฝ์„ ์ด๋™)
if (height[start] > height[end]) {
end--;
} else {
start++;
}
}

return result;
}

41 changes: 41 additions & 0 deletions longest-increasing-subsequence/Yjason-K.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

/**
* ์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด์„ ๊ณ„์‚ฐ
* @param {number[]} nums
* @returns {number} - ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์˜ ์ตœ๋Œ€ ๊ธธ์ด
*
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O( n log(n) )
* - n์€ ๋ฐฐ์—ด์˜ ๊ธธ์ด
* - ๊ฐ ์š”์†Œ๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ ์œ„์น˜๋ฅผ ์ฐพ๊ธฐ ๋•Œ๋ฌธ์— log(n)์ด ์†Œ์š”๋จ
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
* - ๋ฐฐ์—ด์— ์ตœ๋Œ€ n๊ฐœ์˜ ์š”์†Œ๋ฅผ ์ €์žฅ ๊ฐ€๋Šฅ
*/
function lengthOfLIS(nums: number[]): number {
// ์ˆ˜์—ด์„ ์ €์žฅํ•  ๋ฐฐ์—ด
const sequences: number[] = [];

for (let num of nums) {
// ์ด๋ถ„ ํƒ์ƒ‰์„ ์‚ฌ์šฉํ•˜์—ฌ num์ด ๋“ค์–ด๊ฐˆ ์œ„์น˜๋ฆ„ ์ฐพ์Œ
let left = 0;
let right = sequences.length;

while (left < right) {
const mid = Math.floor((left + right) / 2);
if (sequences[mid] < num) {
left = mid + 1;
} else {
right = mid;
}
}

// ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด ์š”์†Œ๋ฅผ ๋Œ€์ฒด
if (left < sequences.length) {
sequences[left] = num;
} else {
sequences.push(num)
}
}

return sequences.length;
}

52 changes: 52 additions & 0 deletions spiral-matrix/Yjason-K.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* 2์ฐจ์› ๋ฐฐ์—ด์„ [0][0] ์‹œ์ž‘์œผ๋กœ ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ 1์ฐจ์› ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜.
*
* @param {number[][]} matrix - 2์ฐจ์› ๋ฐฐ์—ด
* @returns {number[]} ์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ 1์ฐจ์›์œผ๋กœ ํŽผ์นœ ๋ฐฐ์—ด
*
*
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n * m)
* ๋ชจ๋“  ์›์†Œ๊ฐ€ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธ๋˜๋ฏ€๋กœ ํ–‰๊ณผ ์—ด์˜ ๊ณฑ์— ๋น„๋ก€ํ•ฉ๋‹ˆ๋‹ค.
*
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n * m)
* ๋ฐฉ๋ฌธ ๋ฐฐ์—ด๊ณผ ์ถœ๋ ฅ ๋ชฉ๋ก์ด ํ•„์š”ํ•˜๋ฉฐ, ๋‘˜ ๋‹ค ์ž…๋ ฅ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ์™€ ๊ฐ™์€ ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
*/
function spiralOrder(matrix: number[][]): number[] {
// ๋ฐฉ๋ถ„ ๋ฐฐ์—ด ์ƒ์„ฑ
const visited: boolean[][] = Array.from({length: matrix.length}, () => Array(matrix[0].length).fill(false));
// flattenํ•  ๋ฐฐ์—ด
const orderList: number[] = [];
// ์ „์ฒด ๊ธธ์ด
const totalLength = matrix.length * matrix[0].length;
// ์ง„ํ–‰ ๋ฐฉํ–ฅ (์šฐ, ํ•˜, ์ขŒ, ์ƒ)
const directions = [[0,1], [1,0], [0,-1], [-1,0]];

let i = 0; // ์‹œ์ž‘ i ์ขŒํ‘œ
let j = 0; // ์‹œ์ž‘ j ์ขŒํ‘œ
let directionIndex = 0; // ์šฐ๋ฐฉํ–ฅ๋ถ€ํ„ฐ ์‹œ์ž‘

while (orderList.length < totalLength) {
// ํ˜„์žฌ ์œ„์น˜๋ฅผ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ orderList์— ์ถ”๊ฐ€
orderList.push(matrix[i][j])
visited[i][j] = true;

// ๋‹ค์Œ ์œ„์น˜
let nextI = i + directions[directionIndex][0];
let nextJ = j + directions[directionIndex][1];

// ๋‹ค์Œ ์œ„์น˜๊ฐ€ ๋ฒ”์œ„ ๋‚ด์ด๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๊ทธ ์œ„์น˜๋กœ ์ด๋™
if (nextI >= 0 && nextI < matrix.length && nextJ >= 0 && nextJ < matrix[0].length && !visited[nextI][nextJ]) {
i = nextI;
j = nextJ;
} else {
// ๋‹ค์Œ ์œ„์น˜๊ฐ€ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜, ๋ฐฉ๋ฌธํ•œ ๊ฒฝ์šฐ ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ
directionIndex = (directionIndex + 1) % 4;
i += directions[directionIndex][0];
j += directions[directionIndex][1];
}

}

return orderList;
}

35 changes: 35 additions & 0 deletions valid-parentheses/Yjason-K.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s๊ฐ€ ์œ ํšจํ•œ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธ.
* @param {string} s - ์ž…๋ ฅ ๋ฌธ์ž์—ด (๊ด„ํ˜ธ๋งŒ ํฌํ•จ๋จ)
* @returns {boolean} - ์œ ํšจํ•œ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ฉด true, ์•„๋‹ˆ๋ฉด false
*
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
* - ๋ฌธ์ž์—ด์˜ ๊ธธ์ด n๋งŒํผ ํ•œ ๋ฒˆ์”ฉ ์ˆœํšŒํ•˜๋ฉฐ, ๊ฐ ๋ฌธ์ž์— ๋Œ€ํ•ด ๊ณ ์ •๋œ ์—ฐ์‚ฐ(์Šคํƒ ์กฐ์ž‘)์„ ์ˆ˜ํ–‰.
*
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
* - ์ตœ์•…์˜ ๊ฒฝ์šฐ ์Šคํƒ์— ์—ฌ๋Š” ๊ด„ํ˜ธ n๊ฐœ๋ฅผ ์ €์žฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ O(n)์˜ ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
*/
function isValid(s: string): boolean {
if (s.length % 2 !== 0) return false;
if (s === '') return true;

const bracketSets: Record<string, string> = { '(': ')', '{': '}', '[': ']' };
const bracketStack: string[] = [];

for (const char of s) {
if (char in bracketSets) {
// ์—ฌ๋Š” ๊ด„ํ˜ธ์ธ ๊ฒฝ์šฐ ์Šคํƒ์— ์ถ”๊ฐ€
bracketStack.push(char);
} else {
// ๋‹ซ๋Š” ๊ด„ํ˜ธ์ธ ๊ฒฝ์šฐ ์Šคํƒ์—์„œ ๋งˆ์ง€๋ง‰ ์—ฌ๋Š” ๊ด„ํ˜ธ๋ฅผ ๊บผ๋ƒ„
const lastOpeningBracket = bracketStack.pop();
// ์œ ํšจํ•˜์ง€ ์•Š์€ ๊ด„ํ˜ธ ์กฐํ•ฉ์ธ ๊ฒฝ์šฐ
if (bracketSets[lastOpeningBracket!] !== char) {
return false;
}
}
}

// ์Šคํƒ์ด ๋น„์–ด์žˆ์œผ๋ฉด ๋ชจ๋“  ๊ด„ํ˜ธ๊ฐ€ ์œ ํšจ
return bracketStack.length === 0;
}

0 comments on commit 7e7fc1f

Please sign in to comment.