File tree Expand file tree Collapse file tree 5 files changed +154
-0
lines changed
binary-tree-maximum-path-sum Expand file tree Collapse file tree 5 files changed +154
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val, left, right) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.left = (left===undefined ? null : left)
6
+ * this.right = (right===undefined ? null : right)
7
+ * }
8
+ */
9
+ /**
10
+ * @param {TreeNode } root
11
+ * @return {number }
12
+ */
13
+ const maxPathSum = function ( root ) {
14
+ let max = root . val ;
15
+
16
+ function getMaxSum ( node ) {
17
+ if ( ! node ) return 0 ;
18
+
19
+ const leftVal = Math . max ( getMaxSum ( node . left ) , 0 ) ;
20
+ const rightVal = Math . max ( getMaxSum ( node . right ) , 0 ) ;
21
+
22
+ max = Math . max ( node . val + leftVal + rightVal , max ) ;
23
+
24
+ return node . val + Math . max ( leftVal , rightVal ) ;
25
+ }
26
+
27
+ getMaxSum ( root ) ;
28
+
29
+ return max ;
30
+ } ;
31
+
32
+ // ์๊ฐ๋ณต์ก๋: O(n)
33
+ // ๊ณต๊ฐ๋ณต์ก๋: O(h) (h: ํธ๋ฆฌ์ ๋์ด, ์ฆ ์ฌ๊ท ์คํ ๊น์ด)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } n
3
+ * @param {number[][] } edges
4
+ * @returns {boolean }
5
+ */
6
+ function validTree ( n , edges ) {
7
+ // ํธ๋ฆฌ์ ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง ํ์ธ(V = E + 1)
8
+ if ( edges . length !== n - 1 ) {
9
+ return false ;
10
+ }
11
+
12
+ const graph = Array . from ( { length : n } ) . map ( ( ) => [ ] ) ;
13
+
14
+ for ( const [ u , v ] of edges ) {
15
+ graph [ u ] . push ( v ) ;
16
+ graph [ v ] . push ( u ) ;
17
+ }
18
+
19
+ const queue = [ [ - 1 , 0 ] ] ; // ๋ถ๋ชจ ๋
ธ๋, ์์ ๋
ธ๋
20
+ const visited = new Set ( ) ;
21
+
22
+ while ( queue . length ) {
23
+ const [ parent , current ] = queue . shift ( ) ;
24
+ if ( visited . has ( current ) ) {
25
+ return false ;
26
+ }
27
+ visited . add ( current ) ;
28
+
29
+ for ( const neighbor of graph [ current ] ) {
30
+ if ( neighbor === parent ) continue ;
31
+ queue . push ( [ current , neighbor ] ) ;
32
+ }
33
+ }
34
+
35
+ return visited . size === n ;
36
+ }
37
+
38
+ // ์๊ฐ๋ณต์ก๋: O(V + E)
39
+ // ๊ณต๊ฐ๋ณต์ก๋: O(V)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } intervals
3
+ * @return {number[][] }
4
+ */
5
+ const merge = function ( intervals ) {
6
+ // ์์์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
7
+ intervals . sort ( ( a , b ) => Number ( a [ 0 ] ) - Number ( b [ 0 ] ) ) ;
8
+ const result = [ ] ;
9
+
10
+ for ( const current of intervals ) {
11
+ const last = result [ result . length - 1 ] ;
12
+
13
+ // ๊ฒน์น๋ ๊ตฌ๊ฐ์ด ์์ผ๋ฉด, ๊ตฌ๊ฐ์ ๋์ ์ ๋ ํฐ ๊ฒ์ผ๋ก ๋ฎ์ด์์ฐ๊ธฐ
14
+ if ( last && current [ 0 ] <= last [ 1 ] ) {
15
+ result [ result . length - 1 ] [ 1 ] = Math . max ( current [ 1 ] , last [ 1 ] ) ;
16
+ }
17
+ // ๊ฒน์น๋ ๊ตฌ๊ฐ์ด ์์ผ๋ฉด, ์๋ก ์ง์ด ๋ฃ๊ธฐ
18
+ else {
19
+ result . push ( current ) ;
20
+ }
21
+ }
22
+
23
+ return result ;
24
+ } ;
25
+
26
+ // ์๊ฐ๋ณต์ก๋: O(n * log n)
27
+ // ๊ณต๊ฐ๋ณต์ก๋: O(n)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {number }
4
+ */
5
+ const missingNumber = function ( nums ) {
6
+ const n = nums . length ;
7
+ let sum = ( n * ( n + 1 ) ) / 2 ;
8
+
9
+ for ( let num of nums ) {
10
+ sum -= num ;
11
+ }
12
+
13
+ return sum ;
14
+ } ;
15
+
16
+ // ์๊ฐ๋ณต์ก๋: O(n)
17
+ // ๊ณต๊ฐ๋ณต์ก๋: O(1)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val, next) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.next = (next===undefined ? null : next)
6
+ * }
7
+ */
8
+ /**
9
+ * @param {ListNode } head
10
+ * @return {void } Do not return anything, modify head in-place instead.
11
+ */
12
+ var reorderList = function ( head ) {
13
+ const nodes = { } ;
14
+ let current = head ;
15
+ let n = 0 ; // ์ธ๋ฑ์ค ์ญํ
16
+
17
+ // ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ฉด์ ๊ฐ ๋
ธ๋๋ฅผ ๊ฐ์ฒด์ ์ ์ฅ
18
+ while ( current ) {
19
+ nodes [ n ] = current ;
20
+ current = current . next ;
21
+ nodes [ n ] . next = null ; // ์ฌ๊ธฐ์ ๋ชจ๋ ๋
ธ๋์ next๋ฅผ null๋ก ๋ณ๊ฒฝ
22
+ // ์ด๋ ๊ฒ ์ ํ๋ฉด next ๋ฐ๊พธ๊ธฐ ํ ๋ค์์ ๋งจ ๋ง์ง๋ง ๋
ธ๋์ next๋ง null๋ก ๋ฐ๊ฟ์ผ ํจ
23
+ n ++
24
+ }
25
+
26
+ // ์ ์ฅํ ๋
ธ๋๋ฅผ i, n-i, ... ์์๋ก ์ ๊ทผํ๋ฉด์ next ๋ฐ๊พธ๊ธฐ
27
+ for ( let i = 0 ; i < Math . floor ( n / 2 ) ; i ++ ) {
28
+ nodes [ i ] . next = nodes [ n - i - 1 ] ;
29
+
30
+ // n-i-1 === i+1 ์ธ ๊ฒฝ์ฐ, node.next = node ์ฒ๋ผ ์
ํ ์ํ์ด ๋ ์ ์์
31
+ if ( n - i - 1 !== i + 1 ) {
32
+ nodes [ n - i - 1 ] . next = nodes [ i + 1 ] ;
33
+ }
34
+ }
35
+ } ;
36
+
37
+ // ์๊ฐ๋ณต์ก๋: O(n)
38
+ // ๊ณต๊ฐ๋ณต์ก๋: O(n)
You canโt perform that action at this time.
0 commit comments