-
-
Notifications
You must be signed in to change notification settings - Fork 305
[ppxyn1] WEEK 09 Solutions #2260
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
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,31 @@ | ||
| # idea : - | ||
| # Time complxity : O(n) | ||
|
|
||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, x): | ||
| # self.val = x | ||
| # self.next = None | ||
|
|
||
| class Solution: | ||
| def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
| visited = set() | ||
| while head: | ||
| if head in visited: | ||
| return True | ||
| else: | ||
| visited.add(head) | ||
| head = head.next | ||
| return False | ||
|
|
||
| # Floyd (cycle detection) | ||
| # class Solution: | ||
| # def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
| # slow, fast = head, head | ||
| # while fast and fast.next: | ||
| # slow = slow.next | ||
| # fast = fast.next.next | ||
| # if slow == fast: | ||
| # return True | ||
| # return False | ||
|
|
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 @@ | ||
| #idea : DP | ||
| # Time Complexity: O(n) | ||
| class Solution: | ||
| def maxProduct(self, nums: List[int]) -> int: | ||
| ans = 0 | ||
| max_prod = min_prod = nums[0] | ||
|
|
||
| for i in range(1, len(nums)): | ||
| if nums[i] < 0: | ||
| max_prod, min_prod = min_prod, max_prod | ||
| max_prod = max(nums[i], max_prod * nums[i]) | ||
| min_prod = min(nums[i], min_prod * nums[i]) | ||
|
|
||
| ans = max(ans, max_prod) | ||
|
|
||
| return ans | ||
|
|
||
|
|
||
| # Time Complxity: O(N^2) | ||
| # class Solution: | ||
| # def maxProduct(self, nums: List[int]) -> int: | ||
| # max_product = nums[0] | ||
| # for s in range(len(nums)): | ||
| # product = 1 | ||
| # for e in range(s, len(nums)): | ||
| # product *= nums[e] | ||
| # max_product = max(product, max_product) | ||
| # return max_product | ||
|
|
||
|
|
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,21 @@ | ||
| # idea : - | ||
| # Time Complexity: O(1) | ||
|
|
||
| # Solution (1) : It feels a bit like cheating though... but passed the answer lol | ||
| class Solution: | ||
| def getSum(self, a: int, b: int) -> int: | ||
| return sum([a, b]) | ||
|
|
||
| ''' | ||
| # Try and Error : Another way I tried is by using log. | ||
|
|
||
| import numpy as np | ||
|
|
||
| class Solution: | ||
| def getSum(self, a: int, b: int) -> int: | ||
| return int(np.log(np.exp(a) * np.exp(b))) | ||
|
|
||
| Ths way has a problem that it is not calculated as log(exp(a) * exp(b)) = log(exp(a+b)) = a + b like human. | ||
| ''' | ||
|
|
||
|
|
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.
해당 문제는 bit operation을 사용하는 문제라 다시 풀어보시면 좋을것 같아요!