-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #909 from Yjason-K/main
[gomgom22] Week6
- Loading branch information
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |