File tree Expand file tree Collapse file tree 3 files changed +79
-0
lines changed
find-minimum-in-rotated-sorted-array
maximum-depth-of-binary-tree Expand file tree Collapse file tree 3 files changed +79
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 1. Brute-force (시간복잡도: O(n))
2
+ function findMin ( nums : number [ ] ) : number {
3
+ return Math . min ( ...nums ) ;
4
+ } ;
5
+
6
+ // 2 Binary Search (시간복잡도: O(log n))
Original file line number Diff line number Diff line change
1
+ class TreeNode {
2
+ val : number ;
3
+ left : TreeNode | null ;
4
+ right : TreeNode | null ;
5
+ constructor ( val ?: number , left ?: TreeNode | null , right ?: TreeNode | null ) {
6
+ this . val = val === undefined ? 0 : val ;
7
+ this . left = left === undefined ? null : left ;
8
+ this . right = right === undefined ? null : right ;
9
+ }
10
+ }
11
+
12
+ function maxDepth ( root : TreeNode | null ) : number {
13
+ if ( ! root ) return 0 ;
14
+
15
+ const leftDepth = maxDepth ( root . left ) ;
16
+ const rightDepth = maxDepth ( root . right ) ;
17
+
18
+ return Math . max ( leftDepth , rightDepth ) + 1 ;
19
+ } ;
Original file line number Diff line number Diff line change
1
+ class ListNode {
2
+ val : number ;
3
+ next : ListNode | null ;
4
+ constructor ( val ?: number , next ?: ListNode | null ) {
5
+ this . val = val === undefined ? 0 : val ;
6
+ this . next = next === undefined ? null : next ;
7
+ }
8
+ }
9
+
10
+ function mergeTwoLists (
11
+ list1 : ListNode | null ,
12
+ list2 : ListNode | null
13
+ ) : ListNode | null {
14
+ const dummy = new ListNode ( ) ;
15
+ let current = dummy ;
16
+
17
+ const addNode = ( val : number ) => {
18
+ current . next = new ListNode ( val ) ;
19
+ current = current . next ;
20
+ } ;
21
+
22
+ while ( list1 !== null && list2 !== null ) {
23
+ if ( list1 . val < list2 . val ) {
24
+ addNode ( list1 . val ) ;
25
+ list1 = list1 . next ;
26
+ } else if ( list1 . val > list2 . val ) {
27
+ addNode ( list2 . val ) ;
28
+ list2 = list2 . next ;
29
+ } else {
30
+ addNode ( list1 . val ) ;
31
+ addNode ( list2 . val ) ;
32
+ list1 = list1 . next ;
33
+ list2 = list2 . next ;
34
+ }
35
+ }
36
+
37
+ current . next = list1 !== null ? list1 : list2 ;
38
+ return dummy . next ;
39
+ } ;
40
+
41
+ // 2번째 풀이 (재귀)
42
+ function mergeTwoLists2 (
43
+ list1 : ListNode | null ,
44
+ list2 : ListNode | null
45
+ ) : ListNode | null {
46
+ if ( ! ( list1 && list2 ) ) return list1 || list2 ;
47
+ if ( list1 . val < list2 . val ) {
48
+ list1 . next = mergeTwoLists ( list1 . next , list2 ) ;
49
+ return list1 ;
50
+ } else {
51
+ list2 . next = mergeTwoLists ( list1 , list2 . next ) ;
52
+ return list2 ;
53
+ }
54
+ } ;
You can’t perform that action at this time.
0 commit comments