Skip to content

Commit

Permalink
feat: spiral-matrix, longest-increasing-subsequence solution
Browse files Browse the repository at this point in the history
  • Loading branch information
YeomChaeeun committed Jan 18, 2025
1 parent f1ff0ef commit 1d87efd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
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
}

0 comments on commit 1d87efd

Please sign in to comment.