File tree Expand file tree Collapse file tree 5 files changed +122
-0
lines changed
longest-palindromic-substring
number-of-connected-components-in-an-undirected-graph Expand file tree Collapse file tree 5 files changed +122
-0
lines changed Original file line number Diff line number Diff line change 1+ # O(n+e) where n is number of nodes and e is the number of edges.
2+ # O(n+e) where n is number of nodes and e is the number of edges.
3+ class Solution :
4+ def validTree (self , numNodes : int , connections : List [List [int ]]) -> bool :
5+ adjacencyList = [[] for _ in range (numNodes )]
6+ for src , dst in connections :
7+ adjacencyList [src ].append (dst )
8+ adjacencyList [dst ].append (src )
9+
10+ visitedNodes = set ()
11+
12+ def detectCycle (currentNode , previousNode ):
13+ if currentNode in visitedNodes :
14+ return True
15+ visitedNodes .add (currentNode )
16+ for neighbor in adjacencyList [currentNode ]:
17+ if neighbor == previousNode :
18+ continue
19+ if detectCycle (neighbor , currentNode ):
20+ return True
21+ return False
22+
23+ if detectCycle (0 , - 1 ):
24+ return False
25+ return len (visitedNodes ) == numNodes
Original file line number Diff line number Diff line change 1+ # TC : O(n), where n is the number of houses.
2+ # SC : O(1)
3+ class Solution :
4+ def rob (self , nums ):
5+ n = len (nums )
6+ if n == 1 :
7+ return nums [0 ]
8+ if n == 2 :
9+ return max (nums [0 ], nums [1 ])
10+ if n == 3 :
11+ return max (nums [0 ], max (nums [1 ], nums [2 ]))
12+
13+ a = nums [0 ]
14+ b = max (nums [0 ], nums [1 ])
15+ c = - 1
16+ a1 = nums [1 ]
17+ b1 = max (nums [1 ], nums [2 ])
18+ c1 = - 1
19+
20+ for i in range (2 , n ):
21+ if i < n - 1 :
22+ c = max (nums [i ] + a , b )
23+ a = b
24+ b = c
25+ if i > 2 :
26+ c1 = max (nums [i ] + a1 , b1 )
27+ a1 = b1
28+ b1 = c1
29+
30+ return max (c , c1 )
Original file line number Diff line number Diff line change 1+ # TC : O(n)
2+ # SC : O(n)
3+ class Solution :
4+ def rob (self , nums ):
5+ n = len (nums )
6+ if n == 0 :
7+ return 0
8+ if n == 1 :
9+ return nums [0 ]
10+
11+ dp = [- 1 ] * n
12+ dp [0 ] = nums [0 ]
13+ if n > 1 :
14+ dp [1 ] = max (nums [0 ], nums [1 ])
15+
16+ for i in range (2 , n ):
17+ dp [i ] = max (dp [i - 1 ], nums [i ] + dp [i - 2 ])
18+
19+ return dp [- 1 ]
Original file line number Diff line number Diff line change 1+ # O(n^2)
2+ # O(1)
3+ class Solution :
4+ def longestPalindrome (self , s : str ) -> str :
5+ if len (s ) <= 1 :
6+ return s
7+
8+ def expand_from_center (left , right ):
9+ while left >= 0 and right < len (s ) and s [left ] == s [right ]:
10+ left -= 1
11+ right += 1
12+ return s [left + 1 : right ]
13+
14+ max_str = s [0 ]
15+
16+ for i in range (len (s ) - 1 ):
17+ odd = expand_from_center (i , i )
18+ even = expand_from_center (i , i + 1 )
19+
20+ if len (odd ) > len (max_str ):
21+ max_str = odd
22+ if len (even ) > len (max_str ):
23+ max_str = even
24+
25+ return max_str
Original file line number Diff line number Diff line change 1+ # O(n+e) where n is number of nodes and e is the number of edges.
2+ # O(n+e) where n is number of nodes and e is the number of edges.
3+ class Solution :
4+ def countComponents (self , numNodes : int , connections : List [List [int ]]) -> int :
5+ adjacencyList = [[] for _ in range (numNodes )]
6+ for src , dst in connections :
7+ adjacencyList [src ].append (dst )
8+ adjacencyList [dst ].append (src )
9+
10+ visitedNodes = set ()
11+
12+ def depthFirstSearch (node ):
13+ visitedNodes .add (node )
14+ for neighbor in adjacencyList [node ]:
15+ if neighbor not in visitedNodes :
16+ depthFirstSearch (neighbor )
17+
18+ componentCount = 0
19+ for node in range (numNodes ):
20+ if node not in visitedNodes :
21+ componentCount += 1
22+ depthFirstSearch (node )
23+ return componentCount
You can’t perform that action at this time.
0 commit comments