diff --git a/Python/Problem1.py b/Python/Problem1.py index e69de29..bc1f33f 100644 --- a/Python/Problem1.py +++ b/Python/Problem1.py @@ -0,0 +1,11 @@ +class Solution: + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + merged_array = list (heapq.merge(nums1, nums2)) + + n = len(merged_array) + + if n % 2 == 1: + return merged_array[n // 2] + else: + center = merged_array[n // 2 - 1] + merged_array[n // 2] + return (center / 2) \ No newline at end of file diff --git a/Python/Problem2.py b/Python/Problem2.py index e69de29..4977864 100644 --- a/Python/Problem2.py +++ b/Python/Problem2.py @@ -0,0 +1,48 @@ +class Node: + def __init__(self, key, val): + self.key, self.val = key, val + self.prev = self.next = None + +class LRUCache: + + def __init__(self, capacity: int): + self.capacity = capacity + self.cache_map = {} + + self.least_recent, self.most_recent = Node(0,0), Node(0,0) + self.least_recent.next, self.most_recent.prev = self.most_recent, self.least_recent + + def remove(self, node): + prev, nxt = node.prev, node.next + prev.next, nxt.prev = nxt, prev + + def insert(self, node): + prev, nxt = self.most_recent.prev, self.most_recent + prev.next = nxt.prev = node + node.next, node.prev = nxt, prev + + def get(self, key: int) -> int: + if key in self.cache_map: + #update the most recent use + self.remove(self.cache_map[key]) + self.insert(self.cache_map[key]) + return self.cache_map[key].val + else: + return -1 + def put(self, key: int, value: int) -> None: + if key in self.cache_map: + self.remove(self.cache_map[key]) + self.cache_map[key] = Node(key, value) + self.insert(self.cache_map[key]) + + if len(self.cache_map) > self.capacity: + #Evict LRU + lru = self.least_recent.next + self.remove(lru) + del self.cache_map[lru.key] + + +# Your LRUCache object will be instantiated and called as such: +# obj = LRUCache(capacity) +# param_1 = obj.get(key) +# obj.put(key,value) \ No newline at end of file diff --git a/Python/Problem3.py b/Python/Problem3.py index e69de29..bd6c068 100644 --- a/Python/Problem3.py +++ b/Python/Problem3.py @@ -0,0 +1,14 @@ +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + char_index = {} + left = 0 + max_length = 0 + + for right in range(len(s)): + if s[right] in char_index: # If the character has been seen before, move left pointer + left = max(left, char_index[s[right]] + 1) + + char_index[s[right]] = right # Store or update character index + max_length = max(max_length, right - left + 1) + + return max_length \ No newline at end of file diff --git a/Python/Problem4.py b/Python/Problem4.py index e69de29..8a6ace6 100644 --- a/Python/Problem4.py +++ b/Python/Problem4.py @@ -0,0 +1,14 @@ +class Solution: + def isMatch(self, s: str, p: str) -> bool: + if not p: + return not s # In case it is empty + + # Check first match + first_match = bool(s) and (s[0] == p[0] or p[0] == '.') + + # Handle '*' both Zero or More occurences + if len(p) > 1 and p[1] == '*': + return (self.isMatch(s, p[2:])) or (first_match and self.isMatch(s[1:], p)) + + # Regular case: move to the next character + return first_match and self.isMatch(s[1:], p[1:]) \ No newline at end of file