Skip to content

[YeomChaeeun] Week 6 #890

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

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions container-with-most-water/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 배열의 두개의 높이를 가지고 최대 용량을 구하기
* 알고리즘 복잡도
* - 시간 복잡도: O(n)
* - 공간 복잡도: O(1)
* @param height
*/
function maxArea(height: number[]): number {
// end - start * min(height[start], height[end])가 큰 것

// 8 - 0 * min(1, 7) = 8
// 8 - 1 * min(8, 7) = 49
// 7 - 1 * min(8, 3) = 18
// 6 - 1 * min(8, 8) = 40
// ...

let s = 0
let e = height.length - 1
let curr = 0
let max = 0

while(s < e) {
curr = (e - s) * Math.min(height[s], height[e])
max = Math.max(curr, max)
if(height[s] < height[e]){
s += 1
} else {
e -= 1
}
}

return max
}
26 changes: 26 additions & 0 deletions longest-increasing-subsequence/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 주어진 배열에서 가장 긴 부분 수열의 길이 구하기
* 달고알레 풀이를 참고하여 동적 프로그래밍 적용했습니다
* 알고리즘 복잡도
* - 시간 복잡도: O(n2)
* - 공간 복잡도: O(n)
* @param nums
*/
function lengthOfLIS(nums: number[]): number {
// dp 배열을 1로 초기화 - 각 숫자 단독의 기본 길이는 1임
const dp: number[] = new Array(nums.length).fill(1)
let maxLength = 1

for (let i = 1; i < nums.length; i++) {
// 현재 위치(i) 이전의 모든 원소들을 확인
for (let j = 0; j < i; j++) {
// 현재 숫자가 이전 숫자보다 큰 경우 - 부분 수열이 가능하다는 것
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1)
}
}
maxLength = Math.max(maxLength, dp[i])
}

return maxLength
}
43 changes: 43 additions & 0 deletions spiral-matrix/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 달팽이 알고리즘
* 알고리즘 복잡도
* - 시간 복잡도: O(n) - 모든 행렬의 원소의 수 (rows * columns)
* - 공간 복잡도: O(n) - 결과 저장을 위한 배열
* @param matrix
*/
function spiralOrder(matrix: number[][]): number[] {
// 정처기 단골 문제였던 기억이..
const result: number[] = [];
let top = 0
let bottom = matrix.length - 1;
let left = 0
let right = matrix[0].length - 1;

while(top <= bottom && left <= right) { // 순환 조건
for(let i = left; i <= right; i++) {
result.push(matrix[top][i])
}
top++

for(let i = top; i <= bottom; i++) {
result.push(matrix[i][right])
}
right--

if(top <= bottom) {
for(let i = right; i >= left; i--) {
result.push(matrix[bottom][i])
}
bottom--
}

if(left <= right) {
for(let i = bottom; i >= top; i--) {
result.push(matrix[i][left])
}
left++
}
}

return result
}
42 changes: 42 additions & 0 deletions valid-parentheses/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* valid-parentheses
* 괄호의 열고 닫히는 짝을 확인하는 알고리즘
* Stack(LIFO) 데이터 구조 사용
* 알고리즘 복잡도
* - 시간 복잡도: O(n)
* - 공간 복잡도: O(n)
* @param s
*/
function isValid(s: string): boolean {

// 접근 1 - {}, (), [] 가 포함되는지 보고 replace문으로 단순하게 풀어봄..
// while (s.includes("{}") || s.includes("()") || s.includes("[]")) {
// s = s.replace("{}", "");
// s = s.replace("()", "");
// s = s.replace("[]", "");
// }
// return s === '';

// 접근 2 - leetCode의 hint를 보고 stack 을 적용
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const stack: string[] = []
const pairs: {[key: string]: string} = {
'}': '{',
')': '(',
']': '['
}

for (const char of s) {
if (!pairs[char]) {
// 여는 괄호 저장
stack.push(char)
} else {
// 닫는 괄호와 매칭 확인
if (stack.pop() !== pairs[char]) {
return false
}
}
}

return stack.length === 0
}

Loading