Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions linked-list-cycle/ppxyn1.py
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

30 changes: 30 additions & 0 deletions maximum-product-subarray/ppxyn1.py
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


21 changes: 21 additions & 0 deletions sum-of-two-integers/ppxyn1.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 문제는 bit operation을 사용하는 문제라 다시 풀어보시면 좋을것 같아요!

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.
'''