-
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.
feat: spiral-matrix, longest-increasing-subsequence solution
- Loading branch information
1 parent
f1ff0ef
commit 1d87efd
Showing
2 changed files
with
69 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,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 | ||
} |
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,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 | ||
} |