File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed
longest-increasing-subsequence Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * 주어진 배열에서 가장 긴 부분 수열의 길이 구하기
3+ * 달고알레 풀이를 참고하여 동적 프로그래밍 적용했습니다
4+ * 알고리즘 복잡도
5+ * - 시간 복잡도: O(n2)
6+ * - 공간 복잡도: O(n)
7+ * @param nums
8+ */
9+ function lengthOfLIS ( nums : number [ ] ) : number {
10+ // dp 배열을 1로 초기화 - 각 숫자 단독의 기본 길이는 1임
11+ const dp : number [ ] = new Array ( nums . length ) . fill ( 1 )
12+ let maxLength = 1
13+
14+ for ( let i = 1 ; i < nums . length ; i ++ ) {
15+ // 현재 위치(i) 이전의 모든 원소들을 확인
16+ for ( let j = 0 ; j < i ; j ++ ) {
17+ // 현재 숫자가 이전 숫자보다 큰 경우 - 부분 수열이 가능하다는 것
18+ if ( nums [ i ] > nums [ j ] ) {
19+ dp [ i ] = Math . max ( dp [ i ] , dp [ j ] + 1 )
20+ }
21+ }
22+ maxLength = Math . max ( maxLength , dp [ i ] )
23+ }
24+
25+ return maxLength
26+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * 달팽이 알고리즘
3+ * 알고리즘 복잡도
4+ * - 시간 복잡도: O(n) - 모든 행렬의 원소의 수 (rows * columns)
5+ * - 공간 복잡도: O(n) - 결과 저장을 위한 배열
6+ * @param matrix
7+ */
8+ function spiralOrder ( matrix : number [ ] [ ] ) : number [ ] {
9+ // 정처기 단골 문제였던 기억이..
10+ const result : number [ ] = [ ] ;
11+ let top = 0
12+ let bottom = matrix . length - 1 ;
13+ let left = 0
14+ let right = matrix [ 0 ] . length - 1 ;
15+
16+ while ( top <= bottom && left <= right ) { // 순환 조건
17+ for ( let i = left ; i <= right ; i ++ ) {
18+ result . push ( matrix [ top ] [ i ] )
19+ }
20+ top ++
21+
22+ for ( let i = top ; i <= bottom ; i ++ ) {
23+ result . push ( matrix [ i ] [ right ] )
24+ }
25+ right --
26+
27+ if ( top <= bottom ) {
28+ for ( let i = right ; i >= left ; i -- ) {
29+ result . push ( matrix [ bottom ] [ i ] )
30+ }
31+ bottom --
32+ }
33+
34+ if ( left <= right ) {
35+ for ( let i = bottom ; i >= top ; i -- ) {
36+ result . push ( matrix [ i ] [ left ] )
37+ }
38+ left ++
39+ }
40+ }
41+
42+ return result
43+ }
You can’t perform that action at this time.
0 commit comments