File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # idea : -
2+ # Time complxity : O(n)
3+
4+ # Definition for singly-linked list.
5+ # class ListNode:
6+ # def __init__(self, x):
7+ # self.val = x
8+ # self.next = None
9+
10+ class Solution :
11+ def hasCycle (self , head : Optional [ListNode ]) -> bool :
12+ visited = set ()
13+ while head :
14+ if head in visited :
15+ return True
16+ else :
17+ visited .add (head )
18+ head = head .next
19+ return False
20+
21+ # Floyd (cycle detection)
22+ # class Solution:
23+ # def hasCycle(self, head: Optional[ListNode]) -> bool:
24+ # slow, fast = head, head
25+ # while fast and fast.next:
26+ # slow = slow.next
27+ # fast = fast.next.next
28+ # if slow == fast:
29+ # return True
30+ # return False
31+
Original file line number Diff line number Diff line change 1+ #idea : DP
2+ # Time Complexity: O(n)
3+ class Solution :
4+ def maxProduct (self , nums : List [int ]) -> int :
5+ ans = 0
6+ max_prod = min_prod = nums [0 ]
7+
8+ for i in range (1 , len (nums )):
9+ if nums [i ] < 0 :
10+ max_prod , min_prod = min_prod , max_prod
11+ max_prod = max (nums [i ], max_prod * nums [i ])
12+ min_prod = min (nums [i ], min_prod * nums [i ])
13+
14+ ans = max (ans , max_prod )
15+
16+ return ans
17+
18+
19+ # Time Complxity: O(N^2)
20+ # class Solution:
21+ # def maxProduct(self, nums: List[int]) -> int:
22+ # max_product = nums[0]
23+ # for s in range(len(nums)):
24+ # product = 1
25+ # for e in range(s, len(nums)):
26+ # product *= nums[e]
27+ # max_product = max(product, max_product)
28+ # return max_product
29+
30+
Original file line number Diff line number Diff line change 1+ # idea : -
2+ # Time Complexity: O(1)
3+
4+ # Solution (1) : It feels a bit like cheating though... but passed the answer lol
5+ class Solution :
6+ def getSum (self , a : int , b : int ) -> int :
7+ return sum ([a , b ])
8+
9+ '''
10+ # Try and Error : Another way I tried is by using log.
11+
12+ import numpy as np
13+
14+ class Solution:
15+ def getSum(self, a: int, b: int) -> int:
16+ return int(np.log(np.exp(a) * np.exp(b)))
17+
18+ Ths way has a problem that it is not calculated as log(exp(a) * exp(b)) = log(exp(a+b)) = a + b like human.
19+ '''
20+
21+
You can’t perform that action at this time.
0 commit comments