Skip to content

Commit 7e7fc1f

Browse files
authored
Merge pull request #909 from Yjason-K/main
[gomgom22] Week6
2 parents 8ee2e60 + a34edf6 commit 7e7fc1f

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉด์ ์—์„œ ๊ฐ€์žฅ ํฐ ๋ฉด์  ๊ตฌํ•˜๊ธฐ.
3+
*
4+
* @param {number[]} height - x ์ถ• ๋ฐฉํ–ฅ์˜ ๋†’์ด ๋ฐฐ์—ด
5+
* @returns {number} - ๊ฐ€์žฅ ๋„“์€ ๋ฉด์  ๋ฐ˜ํ™˜
6+
*
7+
* ์‹œ๊ฐ„ ๋ณต์žก๋„ O(n)
8+
* - n์€ height ๋ฐฐ์—ด์˜ ๊ธธ์ด
9+
*
10+
* ๊ณต๊ฐ„ ๋ณต์žก๋„ O(1)
11+
* - ํฌ์ธํ„ฐ ๋ฐ ๊ฒฐ๊ณผ๊ฐ’ ์ €์žฅ ๊ณต๊ฐ„๋งŒ ์‚ฌ์šฉ
12+
*/
13+
function maxArea(height: number[]): number {
14+
let start = 0; // ์‹œ์ž‘ ํฌ์ธํ„ฐ ์ดˆ๊ธฐํ™”
15+
let end = height.length - 1; // ๋ ํฌ์ธํ„ฐ ์ดˆ๊ธฐํ™”
16+
let result = 0; // ์ตœ๋Œ€ ๋ฉด์  ์ €์žฅ
17+
18+
while (start < end) {
19+
// ํ˜„์žฌ ๋ฉด์  ๊ณ„์‚ฐ
20+
const currentArea = Math.min(height[start], height[end]) * (end - start);
21+
// ์ตœ๋Œ€ ๋ฉด์  ์—…๋ฐ์ดํŠธ
22+
result = Math.max(result, currentArea);
23+
24+
// ํฌ์ธํ„ฐ ์ด๋™ (๋†’์ด๊ฐ€ ๋‚ฎ์€ ์ชฝ์„ ์ด๋™)
25+
if (height[start] > height[end]) {
26+
end--;
27+
} else {
28+
start++;
29+
}
30+
}
31+
32+
return result;
33+
}
34+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
/**
3+
* ์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด์„ ๊ณ„์‚ฐ
4+
* @param {number[]} nums
5+
* @returns {number} - ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์˜ ์ตœ๋Œ€ ๊ธธ์ด
6+
*
7+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O( n log(n) )
8+
* - n์€ ๋ฐฐ์—ด์˜ ๊ธธ์ด
9+
* - ๊ฐ ์š”์†Œ๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ ์œ„์น˜๋ฅผ ์ฐพ๊ธฐ ๋•Œ๋ฌธ์— log(n)์ด ์†Œ์š”๋จ
10+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
11+
* - ๋ฐฐ์—ด์— ์ตœ๋Œ€ n๊ฐœ์˜ ์š”์†Œ๋ฅผ ์ €์žฅ ๊ฐ€๋Šฅ
12+
*/
13+
function lengthOfLIS(nums: number[]): number {
14+
// ์ˆ˜์—ด์„ ์ €์žฅํ•  ๋ฐฐ์—ด
15+
const sequences: number[] = [];
16+
17+
for (let num of nums) {
18+
// ์ด๋ถ„ ํƒ์ƒ‰์„ ์‚ฌ์šฉํ•˜์—ฌ num์ด ๋“ค์–ด๊ฐˆ ์œ„์น˜๋ฆ„ ์ฐพ์Œ
19+
let left = 0;
20+
let right = sequences.length;
21+
22+
while (left < right) {
23+
const mid = Math.floor((left + right) / 2);
24+
if (sequences[mid] < num) {
25+
left = mid + 1;
26+
} else {
27+
right = mid;
28+
}
29+
}
30+
31+
// ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด ์š”์†Œ๋ฅผ ๋Œ€์ฒด
32+
if (left < sequences.length) {
33+
sequences[left] = num;
34+
} else {
35+
sequences.push(num)
36+
}
37+
}
38+
39+
return sequences.length;
40+
}
41+

โ€Žspiral-matrix/Yjason-K.tsโ€Ž

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* 2์ฐจ์› ๋ฐฐ์—ด์„ [0][0] ์‹œ์ž‘์œผ๋กœ ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ 1์ฐจ์› ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜.
3+
*
4+
* @param {number[][]} matrix - 2์ฐจ์› ๋ฐฐ์—ด
5+
* @returns {number[]} ์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ 1์ฐจ์›์œผ๋กœ ํŽผ์นœ ๋ฐฐ์—ด
6+
*
7+
*
8+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n * m)
9+
* ๋ชจ๋“  ์›์†Œ๊ฐ€ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธ๋˜๋ฏ€๋กœ ํ–‰๊ณผ ์—ด์˜ ๊ณฑ์— ๋น„๋ก€ํ•ฉ๋‹ˆ๋‹ค.
10+
*
11+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n * m)
12+
* ๋ฐฉ๋ฌธ ๋ฐฐ์—ด๊ณผ ์ถœ๋ ฅ ๋ชฉ๋ก์ด ํ•„์š”ํ•˜๋ฉฐ, ๋‘˜ ๋‹ค ์ž…๋ ฅ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ์™€ ๊ฐ™์€ ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
13+
*/
14+
function spiralOrder(matrix: number[][]): number[] {
15+
// ๋ฐฉ๋ถ„ ๋ฐฐ์—ด ์ƒ์„ฑ
16+
const visited: boolean[][] = Array.from({length: matrix.length}, () => Array(matrix[0].length).fill(false));
17+
// flattenํ•  ๋ฐฐ์—ด
18+
const orderList: number[] = [];
19+
// ์ „์ฒด ๊ธธ์ด
20+
const totalLength = matrix.length * matrix[0].length;
21+
// ์ง„ํ–‰ ๋ฐฉํ–ฅ (์šฐ, ํ•˜, ์ขŒ, ์ƒ)
22+
const directions = [[0,1], [1,0], [0,-1], [-1,0]];
23+
24+
let i = 0; // ์‹œ์ž‘ i ์ขŒํ‘œ
25+
let j = 0; // ์‹œ์ž‘ j ์ขŒํ‘œ
26+
let directionIndex = 0; // ์šฐ๋ฐฉํ–ฅ๋ถ€ํ„ฐ ์‹œ์ž‘
27+
28+
while (orderList.length < totalLength) {
29+
// ํ˜„์žฌ ์œ„์น˜๋ฅผ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ orderList์— ์ถ”๊ฐ€
30+
orderList.push(matrix[i][j])
31+
visited[i][j] = true;
32+
33+
// ๋‹ค์Œ ์œ„์น˜
34+
let nextI = i + directions[directionIndex][0];
35+
let nextJ = j + directions[directionIndex][1];
36+
37+
// ๋‹ค์Œ ์œ„์น˜๊ฐ€ ๋ฒ”์œ„ ๋‚ด์ด๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๊ทธ ์œ„์น˜๋กœ ์ด๋™
38+
if (nextI >= 0 && nextI < matrix.length && nextJ >= 0 && nextJ < matrix[0].length && !visited[nextI][nextJ]) {
39+
i = nextI;
40+
j = nextJ;
41+
} else {
42+
// ๋‹ค์Œ ์œ„์น˜๊ฐ€ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜, ๋ฐฉ๋ฌธํ•œ ๊ฒฝ์šฐ ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ
43+
directionIndex = (directionIndex + 1) % 4;
44+
i += directions[directionIndex][0];
45+
j += directions[directionIndex][1];
46+
}
47+
48+
}
49+
50+
return orderList;
51+
}
52+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s๊ฐ€ ์œ ํšจํ•œ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธ.
3+
* @param {string} s - ์ž…๋ ฅ ๋ฌธ์ž์—ด (๊ด„ํ˜ธ๋งŒ ํฌํ•จ๋จ)
4+
* @returns {boolean} - ์œ ํšจํ•œ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ฉด true, ์•„๋‹ˆ๋ฉด false
5+
*
6+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
7+
* - ๋ฌธ์ž์—ด์˜ ๊ธธ์ด n๋งŒํผ ํ•œ ๋ฒˆ์”ฉ ์ˆœํšŒํ•˜๋ฉฐ, ๊ฐ ๋ฌธ์ž์— ๋Œ€ํ•ด ๊ณ ์ •๋œ ์—ฐ์‚ฐ(์Šคํƒ ์กฐ์ž‘)์„ ์ˆ˜ํ–‰.
8+
*
9+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
10+
* - ์ตœ์•…์˜ ๊ฒฝ์šฐ ์Šคํƒ์— ์—ฌ๋Š” ๊ด„ํ˜ธ n๊ฐœ๋ฅผ ์ €์žฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ O(n)์˜ ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
11+
*/
12+
function isValid(s: string): boolean {
13+
if (s.length % 2 !== 0) return false;
14+
if (s === '') return true;
15+
16+
const bracketSets: Record<string, string> = { '(': ')', '{': '}', '[': ']' };
17+
const bracketStack: string[] = [];
18+
19+
for (const char of s) {
20+
if (char in bracketSets) {
21+
// ์—ฌ๋Š” ๊ด„ํ˜ธ์ธ ๊ฒฝ์šฐ ์Šคํƒ์— ์ถ”๊ฐ€
22+
bracketStack.push(char);
23+
} else {
24+
// ๋‹ซ๋Š” ๊ด„ํ˜ธ์ธ ๊ฒฝ์šฐ ์Šคํƒ์—์„œ ๋งˆ์ง€๋ง‰ ์—ฌ๋Š” ๊ด„ํ˜ธ๋ฅผ ๊บผ๋ƒ„
25+
const lastOpeningBracket = bracketStack.pop();
26+
// ์œ ํšจํ•˜์ง€ ์•Š์€ ๊ด„ํ˜ธ ์กฐํ•ฉ์ธ ๊ฒฝ์šฐ
27+
if (bracketSets[lastOpeningBracket!] !== char) {
28+
return false;
29+
}
30+
}
31+
}
32+
33+
// ์Šคํƒ์ด ๋น„์–ด์žˆ์œผ๋ฉด ๋ชจ๋“  ๊ด„ํ˜ธ๊ฐ€ ์œ ํšจ
34+
return bracketStack.length === 0;
35+
}

0 commit comments

Comments
ย (0)