File tree Expand file tree Collapse file tree
find-minimum-in-rotated-sorted-array
maximum-depth-of-binary-tree Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ 이진탐색을 이용한 풀이
3+
4+ TC: O(log n)
5+ SC: O(1)
6+ */
7+ /**
8+ * @param {number[] } nums
9+ * @return {number }
10+ */
11+ function findMin ( nums ) {
12+ let left = 0 ;
13+ let right = nums . length - 1 ;
14+
15+ if ( nums [ left ] <= nums [ right ] ) return nums [ left ] ; // 일자 배열인 경우
16+
17+ while ( left < right ) {
18+ let mid = Math . floor ( ( left + right ) / 2 ) ;
19+
20+ if ( nums [ mid ] > nums [ mid + 1 ] ) return nums [ mid + 1 ] ; // mid의 바로 옆에서 초기화되는 경우
21+
22+ if ( nums [ mid ] > nums [ right ] ) {
23+ left = mid + 1 ;
24+ } else {
25+ right = mid ;
26+ }
27+ }
28+
29+ return nums [ left ] ;
30+ }
Original file line number Diff line number Diff line change 1+ /**
2+ 이 문제를 푸는 핵심은 DFS(깊이 우선 탐색)다.
3+ 왼쪽으로, 오른쪽으로 쭉 들어가는 값을 반환해서 변수에 저장한다.
4+ 이를 재귀호출하면 쉽게 해결할 수 있다.
5+ */
6+ /**
7+ * Definition for a binary tree node.
8+ * function TreeNode(val, left, right) {
9+ * this.val = (val===undefined ? 0 : val)
10+ * this.left = (left===undefined ? null : left)
11+ * this.right = (right===undefined ? null : right)
12+ * }
13+ */
14+ /**
15+ * @param {TreeNode } root
16+ * @return {number }
17+ */
18+ function maxDepth ( root ) {
19+ if ( root === null ) return 0 ;
20+
21+ let left_depth = maxDepth ( root . left ) ;
22+ let right_depth = maxDepth ( root . right ) ;
23+
24+ return Math . max ( left_depth , right_depth ) + 1 ;
25+ }
Original file line number Diff line number Diff line change 1+ /**
2+ list1, list2는 시작 노드 객체
3+ 핵심은 '가위바위보 기찻길 룰'
4+ cur.next가 list1, list2 비교 결과를 배치해주는 역할
5+
6+ TC : O(n)
7+ SC : O(1)
8+ */
9+ /**
10+ * Definition for singly-linked list.
11+ * function ListNode(val, next) {
12+ * this.val = (val===undefined ? 0 : val)
13+ * this.next = (next===undefined ? null : next)
14+ * }
15+ */
16+ /**
17+ * @param {ListNode } list1
18+ * @param {ListNode } list2
19+ * @return {ListNode }
20+ */
21+ function mergeTwoLists ( list1 , list2 ) {
22+ let dummy = new ListNode ( ) ;
23+ let cur = dummy ;
24+
25+ while ( list1 && list2 ) {
26+ if ( list1 . val > list2 . val ) {
27+ cur . next = list2 ;
28+ list2 = list2 . next ;
29+ } else {
30+ cur . next = list1 ;
31+ list1 = list1 . next ;
32+ }
33+ cur = cur . next ;
34+ }
35+ cur . next = list1 || list2 ; // 남아있는 원소 붙이기
36+
37+ return dummy . next ;
38+ }
You can’t perform that action at this time.
0 commit comments