-
-
Notifications
You must be signed in to change notification settings - Fork 245
[gomgom22] Week6 #909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[gomgom22] Week6 #909
Changes from 6 commits
e421bbb
66d005a
107cd10
df48a41
dd72377
5dacfc8
6cb7ad8
91b3877
a34edf6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
|
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 sequnces: number[] = []; | ||
|
||
for (let num of nums) { | ||
// 이분 탐색을 사용하여 num이 들어갈 위치름 찾음 | ||
let left = 0; | ||
let right = sequnces.length; | ||
|
||
while (left < right) { | ||
const mid = Math.floor((left + right) / 2); | ||
if (sequnces[mid] < num) { | ||
left = mid + 1; | ||
} else { | ||
right = mid; | ||
} | ||
} | ||
|
||
// 새로운 요소를 추가하거나 기존 요소를 대체 | ||
if (left < sequnces.length) { | ||
sequnces[left] = num; | ||
} else { | ||
sequnces.push(num) | ||
} | ||
} | ||
|
||
return sequnces.length; | ||
} | ||
|
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; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* 주어진 문자열 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 openingBrackets = ['(', '{', '[']; | ||
const bracketSets: Record<string, string> = { '(': ')', '{': '}', '[': ']' }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 변수의 이름과 사용에 정리가 필요해 보여요.
|
||
const bracketStack: string[] = []; | ||
|
||
for (const char of s) { | ||
// 여는 경우, 스택에 추가 | ||
if (openingBrackets.includes(char)) { | ||
bracketStack.push(char); | ||
} else { | ||
// 닫는 괄호 경우, 스택에서 가장 마지막에 추가한 여는 괄호를 꺼냄 | ||
const lastOpeningBracket = bracketStack.pop(); | ||
// bracketSets 과 유효하지 않은 괄호인 경우 | ||
if (bracketSets[lastOpeningBracket!] !== char) { | ||
return false; | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 시간복잡도가 O(n)으로 적어주셨는데요. for...of 문 안에 includes() 메소드를 사용하고 있어서O(n^2)이 될 듯합니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. openingBrackets 경우 사이즈 크기라 O(n)으로 생각해도 되지 않을까요? openingBrackets.includes(char)는 O(1), bracketStack.push()와 bracketStack.pop()도 배열의 끝에서 작업을 수행하므로 O(1), bracketSets[lastOpeningBracket!] !== char는 상수사이즈의 객체 조회이므로 O(1) 이렇게 생각했어요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 @Yjason-K 님 의견에 공감해요.
|
||
|
||
// 스택이 비어있으면 모든 괄호가 유효한 경우 | ||
return bracketStack.length === 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
중요하지 않은 사항입니다~
오탈자가 있네요! (
sequnces
->sequences
)