File tree Expand file tree Collapse file tree 4 files changed +118
-0
lines changed
binary-tree-maximum-path-sum Expand file tree Collapse file tree 4 files changed +118
-0
lines changed 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
+ // TC: O(n)
13
+ // SC: O(n)
14
+ function maxPathSum ( root : TreeNode | null ) : number {
15
+ let maxSum = - Infinity ;
16
+
17
+ const dfs = ( node : TreeNode | null ) => {
18
+ if ( ! node ) return 0 ;
19
+
20
+ const leftMax = Math . max ( dfs ( node . left ) , 0 ) ;
21
+ const rightMax = Math . max ( dfs ( node . right ) , 0 ) ;
22
+
23
+ maxSum = Math . max ( node . val + leftMax + rightMax , maxSum ) ;
24
+
25
+ return node . val + Math . max ( leftMax , rightMax ) ;
26
+ } ;
27
+
28
+ dfs ( root ) ;
29
+ return maxSum ;
30
+ }
Original file line number Diff line number Diff line change
1
+ // TC: O(n * log n)
2
+ // SC: O(n)
3
+ function merge ( intervals : number [ ] [ ] ) : number [ ] [ ] {
4
+ if ( intervals . length === 0 ) return [ ] ;
5
+
6
+ intervals . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
7
+
8
+ const merged = [ intervals [ 0 ] ] ;
9
+
10
+ for ( let i = 1 ; i < intervals . length ; i ++ ) {
11
+ const last = merged [ merged . length - 1 ] ;
12
+ const current = intervals [ i ] ;
13
+
14
+ if ( current [ 0 ] <= last [ 1 ] ) {
15
+ last [ 1 ] = Math . max ( last [ 1 ] , current [ 1 ] ) ;
16
+ } else {
17
+ merged . push ( intervals [ i ] ) ;
18
+ }
19
+ }
20
+
21
+ return merged ;
22
+ }
Original file line number Diff line number Diff line change
1
+ // TC: O(n)
2
+ // SC: O(1)
3
+ function missingNumber ( nums : number [ ] ) : number {
4
+ // 0, 1, 2, 3 => n * (n + 1) / 2
5
+
6
+ let sum = ( nums . length * ( nums . length + 1 ) ) / 2 ;
7
+
8
+ for ( const num of nums ) {
9
+ sum -= num ;
10
+ }
11
+
12
+ return sum ;
13
+ }
14
+
15
+
16
+ // TC: O(n)
17
+ // SC: (1)
18
+ // function missingNumber(nums: number[]): number {
19
+ // let xor = 0;
20
+
21
+ // for (let i = 0; i <= nums.length; i++) {
22
+ // xor ^= i;
23
+ // }
24
+
25
+ // for (const num of nums) {
26
+ // xor ^= num;
27
+ // }
28
+
29
+ // return xor;
30
+ // }
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
+ /**
11
+ Do not return anything, modify head in-place instead.
12
+ */
13
+
14
+ // TC: O(n)
15
+ // SC: O(n)
16
+ function reorderList ( head : ListNode | null ) : void {
17
+ const nodes : ListNode [ ] = [ ] ;
18
+
19
+ while ( head ) {
20
+ nodes . push ( head ) ;
21
+ head = head . next ;
22
+ }
23
+
24
+ let start = 0 ;
25
+ let end = nodes . length - 1 ;
26
+
27
+ while ( start < end ) {
28
+ nodes [ start ] . next = nodes [ end ] ;
29
+ start ++ ;
30
+ if ( start === end ) break ;
31
+ nodes [ end ] . next = nodes [ start ] ;
32
+ end -- ;
33
+ }
34
+
35
+ nodes [ start ] . next = null ;
36
+ }
You can’t perform that action at this time.
0 commit comments