File tree Expand file tree Collapse file tree 5 files changed +172
-0
lines changed
longest-palindromic-substring
number-of-connected-components-in-an-undirected-graph Expand file tree Collapse file tree 5 files changed +172
-0
lines changed Original file line number Diff line number Diff line change 1+ // Time Complexity: O(n)
2+ // Space Complexity: O(n)
3+
4+ class Solution {
5+ validTree ( n , edges ) {
6+ // initialize Union-Find data structure
7+ const parent = Array ( n )
8+ . fill ( 0 )
9+ . map ( ( _ , index ) => index ) ;
10+ const rank = Array ( n ) . fill ( 1 ) ;
11+
12+ // find function with path compression
13+ function find ( x ) {
14+ if ( parent [ x ] !== x ) {
15+ parent [ x ] = find ( parent [ x ] ) ;
16+ }
17+ return parent [ x ] ;
18+ }
19+
20+ // union function with union by rank
21+ function union ( x , y ) {
22+ const rootX = find ( x ) ;
23+ const rootY = find ( y ) ;
24+ if ( rootX !== rootY ) {
25+ if ( rank [ rootX ] > rank [ rootY ] ) {
26+ parent [ rootY ] = rootX ;
27+ } else if ( rank [ rootX ] < rank [ rootY ] ) {
28+ parent [ rootX ] = rootY ;
29+ } else {
30+ parent [ rootY ] = rootX ;
31+ rank [ rootX ] += 1 ;
32+ }
33+ } else {
34+ // if rootX == rootY, there is a cycle
35+ return false ;
36+ }
37+ return true ;
38+ }
39+
40+ // process each edge
41+ for ( const [ u , v ] of edges ) {
42+ if ( ! union ( u , v ) ) {
43+ // if union returns false, a cycle is detected
44+ return false ;
45+ }
46+ }
47+
48+ // if all unions are successful, it's a valid tree
49+ return true ;
50+ }
51+ }
Original file line number Diff line number Diff line change 1+ // Time Complexity: O(n)
2+ // Space Complexity: O(n)
3+
4+ var rob = function ( nums ) {
5+ // to rob a linear list of houses
6+ function robLinear ( houses ) {
7+ let prev1 = 0 ;
8+ let prev2 = 0 ;
9+
10+ for ( let money of houses ) {
11+ let temp = Math . max ( prev1 , money + prev2 ) ;
12+ prev2 = prev1 ;
13+ prev1 = temp ;
14+ }
15+
16+ return prev1 ;
17+ }
18+
19+ // 1. excluding the last house (rob from first to second-to-last)
20+ // 2. excluding the first house (rob from second to last)
21+ let robFirstToSecondLast = robLinear ( nums . slice ( 0 , - 1 ) ) ;
22+ let robSecondToLast = robLinear ( nums . slice ( 1 ) ) ;
23+
24+ // return the maximum money
25+ return Math . max ( robFirstToSecondLast , robSecondToLast ) ;
26+ } ;
Original file line number Diff line number Diff line change 1+ // Time Complexity: O(n)
2+ // Space Complexity: O(n)
3+
4+ var rob = function ( nums ) {
5+ // to store the maximum money that can be robbed up to each house
6+ let memo = new Array ( nums . length ) . fill ( - 1 ) ;
7+
8+ function robFrom ( i ) {
9+ // if the index is out of bounds, return 0
10+ if ( i >= nums . length ) return 0 ;
11+
12+ // 1. rob this house and move to the house two steps ahead
13+ // 2. skip this house and move to the next house
14+ // take the maximum of these two choices
15+ let result = Math . max ( nums [ i ] + robFrom ( i + 2 ) , robFrom ( i + 1 ) ) ;
16+
17+ // store the result
18+ memo [ i ] = result ;
19+
20+ return result ;
21+ }
22+
23+ // start robbing from the first house
24+ return robFrom ( 0 ) ;
25+ } ;
Original file line number Diff line number Diff line change 1+ // Time Complexity: O(n^2)
2+ // Space Complexity: O(1)
3+
4+ var longestPalindrome = function ( s ) {
5+ let start = 0 ,
6+ maxLength = 1 ;
7+
8+ // to expand around the center and update the start and maxLength
9+ function expandAroundCenter ( left , right ) {
10+ while ( left >= 0 && right < s . length && s [ left ] === s [ right ] ) {
11+ left -- ;
12+ right ++ ;
13+ }
14+ // update the start and maxLength if a longer palindrome is found
15+ if ( maxLength < right - left - 1 ) {
16+ start = left + 1 ;
17+ maxLength = right - left - 1 ;
18+ }
19+ }
20+
21+ // iterate through each character in the string
22+ for ( let i = 0 ; i < s . length ; i ++ ) {
23+ // expand around the current character
24+ expandAroundCenter ( i , i ) ;
25+ // expand around the current and next character
26+ expandAroundCenter ( i , i + 1 ) ;
27+ }
28+
29+ // return the longest palindromic substring
30+ return s . substring ( start , start + maxLength ) ;
31+ } ;
Original file line number Diff line number Diff line change 1+ // Time Complexity: O(n + m) m : number of edges
2+ // Space Complexity: O(n)
3+
4+ class Solution {
5+ countComponents ( n , edges ) {
6+ // initialize the parent array where each node is its own parent initially
7+ const parent = new Array ( n ) . fill ( 0 ) . map ( ( _ , index ) => index ) ;
8+
9+ // to find the root of a node with path compression
10+ const find = ( node ) => {
11+ if ( parent [ node ] !== node ) {
12+ parent [ node ] = find ( parent [ node ] ) ;
13+ }
14+ return parent [ node ] ;
15+ } ;
16+
17+ // to union two nodes
18+ const union = ( node1 , node2 ) => {
19+ const root1 = find ( node1 ) ;
20+ const root2 = find ( node2 ) ;
21+ if ( root1 !== root2 ) {
22+ parent [ root1 ] = root2 ;
23+ }
24+ } ;
25+
26+ // union all the edges
27+ for ( let [ a , b ] of edges ) {
28+ union ( a , b ) ;
29+ }
30+
31+ // count the number of unique roots
32+ const uniqueRoots = new Set ( ) ;
33+ for ( let i = 0 ; i < n ; i ++ ) {
34+ uniqueRoots . add ( find ( i ) ) ;
35+ }
36+
37+ return uniqueRoots . size ;
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments