-
-
Notifications
You must be signed in to change notification settings - Fork 195
[SAM] Week 10 solutions #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5111968
solve : house robber
SamTheKorean bdab952
solve : number of conntected components in an undirected graph
SamTheKorean 51a8c31
fix : change space complexity
SamTheKorean 8cc29ac
solve : graph valid tree
SamTheKorean a05b534
solve house roubber il
SamTheKorean 18947f7
solve : longest palindromic substring
SamTheKorean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# O(n+e) where n is number of nodes and e is the number of edges. | ||
# O(n+e) where n is number of nodes and e is the number of edges. | ||
class Solution: | ||
def validTree(self, numNodes: int, connections: List[List[int]]) -> bool: | ||
adjacencyList = [[] for _ in range(numNodes)] | ||
for src, dst in connections: | ||
adjacencyList[src].append(dst) | ||
adjacencyList[dst].append(src) | ||
|
||
visitedNodes = set() | ||
|
||
def detectCycle(currentNode, previousNode): | ||
if currentNode in visitedNodes: | ||
return True | ||
visitedNodes.add(currentNode) | ||
for neighbor in adjacencyList[currentNode]: | ||
if neighbor == previousNode: | ||
continue | ||
if detectCycle(neighbor, currentNode): | ||
return True | ||
return False | ||
|
||
if detectCycle(0, -1): | ||
return False | ||
return len(visitedNodes) == numNodes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# TC : O(n), where n is the number of houses. | ||
# SC : O(1) | ||
class Solution: | ||
def rob(self, nums): | ||
n = len(nums) | ||
if n == 1: | ||
return nums[0] | ||
if n == 2: | ||
return max(nums[0], nums[1]) | ||
if n == 3: | ||
return max(nums[0], max(nums[1], nums[2])) | ||
|
||
a = nums[0] | ||
b = max(nums[0], nums[1]) | ||
c = -1 | ||
a1 = nums[1] | ||
b1 = max(nums[1], nums[2]) | ||
c1 = -1 | ||
|
||
for i in range(2, n): | ||
if i < n - 1: | ||
c = max(nums[i] + a, b) | ||
a = b | ||
b = c | ||
if i > 2: | ||
c1 = max(nums[i] + a1, b1) | ||
a1 = b1 | ||
b1 = c1 | ||
|
||
return max(c, c1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# TC : O(n) | ||
# SC : O(n) | ||
class Solution: | ||
def rob(self, nums): | ||
n = len(nums) | ||
if n == 0: | ||
return 0 | ||
if n == 1: | ||
return nums[0] | ||
|
||
dp = [-1] * n | ||
dp[0] = nums[0] | ||
if n > 1: | ||
dp[1] = max(nums[0], nums[1]) | ||
|
||
for i in range(2, n): | ||
dp[i] = max(dp[i - 1], nums[i] + dp[i - 2]) | ||
|
||
return dp[-1] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# O(n^2) | ||
# O(1) | ||
class Solution: | ||
def longestPalindrome(self, s: str) -> str: | ||
if len(s) <= 1: | ||
return s | ||
|
||
def expand_from_center(left, right): | ||
while left >= 0 and right < len(s) and s[left] == s[right]: | ||
left -= 1 | ||
right += 1 | ||
return s[left + 1 : right] | ||
|
||
max_str = s[0] | ||
|
||
for i in range(len(s) - 1): | ||
odd = expand_from_center(i, i) | ||
even = expand_from_center(i, i + 1) | ||
|
||
if len(odd) > len(max_str): | ||
max_str = odd | ||
if len(even) > len(max_str): | ||
max_str = even | ||
|
||
return max_str |
23 changes: 23 additions & 0 deletions
23
number-of-connected-components-in-an-undirected-graph/samthekorean.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# O(n+e) where n is number of nodes and e is the number of edges. | ||
# O(n+e) where n is number of nodes and e is the number of edges. | ||
class Solution: | ||
def countComponents(self, numNodes: int, connections: List[List[int]]) -> int: | ||
adjacencyList = [[] for _ in range(numNodes)] | ||
for src, dst in connections: | ||
adjacencyList[src].append(dst) | ||
adjacencyList[dst].append(src) | ||
|
||
visitedNodes = set() | ||
|
||
def depthFirstSearch(node): | ||
visitedNodes.add(node) | ||
for neighbor in adjacencyList[node]: | ||
if neighbor not in visitedNodes: | ||
depthFirstSearch(neighbor) | ||
|
||
componentCount = 0 | ||
for node in range(numNodes): | ||
if node not in visitedNodes: | ||
componentCount += 1 | ||
depthFirstSearch(node) | ||
return componentCount |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분은
n <= 3
일 때max(nums)
로 해결할 수 있을 것 같아보여요. :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그렇게 하면 더 간결하겠군요! 피드백 감사합니다!