File tree Expand file tree Collapse file tree 5 files changed +184
-0
lines changed
non-overlapping-intervals
number-of-connected-components-in-an-undirected-graph
remove-nth-node-from-end-of-list
serialize-and-deserialize-binary-tree Expand file tree Collapse file tree 5 files changed +184
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[][] } intervals
3+ * @return {number }
4+ */
5+ var eraseOverlapIntervals = function ( intervals ) {
6+ intervals . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
7+
8+ let res = 0 ;
9+ let prevEnd = intervals [ 0 ] [ 1 ] ;
10+
11+ for ( let i = 1 ; i < intervals . length ; i ++ ) {
12+ const [ start , end ] = intervals [ i ] ;
13+
14+ if ( start >= prevEnd ) {
15+ prevEnd = end ;
16+ } else {
17+ res += 1 ;
18+ prevEnd = Math . min ( end , prevEnd ) ;
19+ }
20+ }
21+
22+ return res ;
23+ } ;
24+
Original file line number Diff line number Diff line change 1+ export class Solution {
2+ /**
3+ * @param {number } n - the number of vertices
4+ * @param {number[][] } edges - the edges of undirected graph
5+ * @return {number } - the number of connected components
6+ */
7+ countComponents ( n , edges ) {
8+ const par = Array . from ( { length : n } , ( _ , i ) => i ) ;
9+ const rank = Array ( n ) . fill ( 1 ) ;
10+
11+ const find = ( n1 ) => {
12+ let res = n1 ;
13+ while ( res !== par [ res ] ) {
14+ par [ res ] = par [ par [ res ] ] ; // path compression
15+ res = par [ res ] ;
16+ }
17+ return res ;
18+ } ;
19+
20+ const union = ( n1 , n2 ) => {
21+ const p1 = find ( n1 ) ;
22+ const p2 = find ( n2 ) ;
23+ if ( p1 === p2 ) return 0 ;
24+
25+ if ( rank [ p2 ] > rank [ p1 ] ) {
26+ par [ p1 ] = p2 ;
27+ rank [ p2 ] += rank [ p1 ] ;
28+ } else {
29+ par [ p2 ] = p1 ;
30+ rank [ p1 ] += rank [ p2 ] ;
31+ }
32+
33+ return 1 ;
34+ } ;
35+
36+ let res = n ;
37+ for ( const [ n1 , n2 ] of edges ) {
38+ res -= union ( n1 , n2 ) ;
39+ }
40+
41+ return res ;
42+ }
43+ }
44+
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+ /**
10+ * @param {ListNode } head
11+ * @param {number } n
12+ * @return {ListNode }
13+ */
14+ var removeNthFromEnd = function ( head , n ) {
15+ const dummy = new ListNode ( 0 , head ) ;
16+ let left = dummy ;
17+ let right = head ;
18+
19+ // advance right pointer n steps ahead
20+ while ( n > 0 && right ) {
21+ right = right . next ;
22+ n -- ;
23+ }
24+
25+ // move both pointers until right reaches the end
26+ while ( right ) {
27+ left = left . next ;
28+ right = right . next ;
29+ }
30+
31+ // delete the nth node from end
32+ left . next = left . next . next ;
33+
34+ return dummy . next ;
35+ } ;
36+
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+ /**
11+ * @param {TreeNode } p
12+ * @param {TreeNode } q
13+ * @return {boolean }
14+ */
15+ var isSameTree = function ( p , q ) {
16+ if ( ! p && ! q ) {
17+ return true ;
18+ }
19+ if ( ! p || ! q || p . val !== q . val ) {
20+ return false ;
21+ }
22+ return isSameTree ( p . left , q . left ) && isSameTree ( p . right , q . right ) ;
23+ } ;
24+
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * function TreeNode(val) {
4+ * this.val = val;
5+ * this.left = this.right = null;
6+ * }
7+ */
8+
9+ /**
10+ * Encodes a tree to a single string.
11+ *
12+ * @param {TreeNode } root
13+ * @return {string }
14+ */
15+ var serialize = function ( root ) {
16+ const res = [ ] ;
17+
18+ const dfs = ( node ) => {
19+ if ( ! node ) {
20+ res . push ( "N" ) ;
21+ return ;
22+ }
23+ res . push ( String ( node . val ) ) ;
24+ dfs ( node . left ) ;
25+ dfs ( node . right ) ;
26+ } ;
27+
28+ dfs ( root ) ;
29+ return res . join ( "." ) ;
30+ } ;
31+
32+ /**
33+ * Decodes your encoded data to tree.
34+ *
35+ * @param {string } data
36+ * @return {TreeNode }
37+ */
38+ var deserialize = function ( data ) {
39+ const vals = data . split ( "." ) ;
40+ let i = 0 ;
41+
42+ const dfs = ( ) => {
43+ if ( vals [ i ] === "N" ) {
44+ i ++ ;
45+ return null ;
46+ }
47+ const node = new TreeNode ( parseInt ( vals [ i ] ) ) ;
48+ i ++ ;
49+ node . left = dfs ( ) ;
50+ node . right = dfs ( ) ;
51+ return node ;
52+ } ;
53+
54+ return dfs ( ) ;
55+ } ;
56+
You can’t perform that action at this time.
0 commit comments