File tree Expand file tree Collapse file tree 4 files changed +144
-0
lines changed
container-with-most-water
longest-increasing-subsequence Expand file tree Collapse file tree 4 files changed +144
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * 배열의 두개의 높이를 가지고 최대 용량을 구하기
3+ * 알고리즘 복잡도
4+ * - 시간 복잡도: O(n)
5+ * - 공간 복잡도: O(1)
6+ * @param height
7+ */
8+ function maxArea ( height : number [ ] ) : number {
9+ // end - start * min(height[start], height[end])가 큰 것
10+
11+ // 8 - 0 * min(1, 7) = 8
12+ // 8 - 1 * min(8, 7) = 49
13+ // 7 - 1 * min(8, 3) = 18
14+ // 6 - 1 * min(8, 8) = 40
15+ // ...
16+
17+ let s = 0
18+ let e = height . length - 1
19+ let curr = 0
20+ let max = 0
21+
22+ while ( s < e ) {
23+ curr = ( e - s ) * Math . min ( height [ s ] , height [ e ] )
24+ max = Math . max ( curr , max )
25+ if ( height [ s ] < height [ e ] ) {
26+ s += 1
27+ } else {
28+ e -= 1
29+ }
30+ }
31+
32+ return max
33+ }
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+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * valid-parentheses
3+ * 괄호의 열고 닫히는 짝을 확인하는 알고리즘
4+ * Stack(LIFO) 데이터 구조 사용
5+ * 알고리즘 복잡도
6+ * - 시간 복잡도: O(n)
7+ * - 공간 복잡도: O(n)
8+ * @param s
9+ */
10+ function isValid ( s : string ) : boolean {
11+
12+ // 접근 1 - {}, (), [] 가 포함되는지 보고 replace문으로 단순하게 풀어봄..
13+ // while (s.includes("{}") || s.includes("()") || s.includes("[]")) {
14+ // s = s.replace("{}", "");
15+ // s = s.replace("()", "");
16+ // s = s.replace("[]", "");
17+ // }
18+ // return s === '';
19+
20+ // 접근 2 - leetCode의 hint를 보고 stack 을 적용
21+ const stack : string [ ] = [ ]
22+ const pairs : { [ key : string ] : string } = {
23+ '}' : '{' ,
24+ ')' : '(' ,
25+ ']' : '['
26+ }
27+
28+ for ( const char of s ) {
29+ if ( ! pairs [ char ] ) {
30+ // 여는 괄호 저장
31+ stack . push ( char )
32+ } else {
33+ // 닫는 괄호와 매칭 확인
34+ if ( stack . pop ( ) !== pairs [ char ] ) {
35+ return false
36+ }
37+ }
38+ }
39+
40+ return stack . length === 0
41+ }
42+
You can’t perform that action at this time.
0 commit comments