Compare commits

...
8 Commits
Author SHA1 Message Date
izatwin 33af13cb50 238 Product of Array except itself 2024-08-04 21:02:39 -04:00
izatwin 2d2ccc70bd 53 Maximum Subarray 2024-08-04 21:02:24 -04:00
izatwin 98d07c8e61 20 Validate Parentheses 2024-07-14 08:55:32 -04:00
izatwin 9c65fc3bcf 242 Valid Anagram 2024-07-14 08:34:30 -04:00
izatwin 059776458a 121 Best Time to Buy and Sell Stocks 2024-06-29 10:18:18 -04:00
izatwin d8665c4e6a 217 Contains Duplicate 2024-05-11 16:07:26 -04:00
izatwin 36611fddaa 133 clone graph 2024-05-10 12:37:42 -04:00
izatwin c70da65d0f lint 2024-05-07 12:56:35 -04:00
8 changed files with 128 additions and 3 deletions
@@ -0,0 +1,14 @@
class Solution:
def maxProfit(self, prices: List[int]) -> int:
max_profit = 0
buy_index = 0
if len(prices) == 1:
return 0
for i, cur_price in enumerate(prices):
profit = cur_price - prices[buy_index]
if profit > max_profit:
max_profit = profit
elif cur_price < prices[buy_index]:
buy_index = i
return max_profit
+21
View File
@@ -0,0 +1,21 @@
from collections import deque
class Solution:
def isValid(self, s: str) -> bool:
b_queue = deque()
for c in s:
if c == "[":
b_queue.append("]")
elif c == "(":
b_queue.append(")")
elif c == "{":
b_queue.append("}")
elif c in ["}", ")", "]"]:
if not len(b_queue) > 0:
return False
if c != b_queue.pop():
return False
return not len(b_queue) > 0
+12
View File
@@ -0,0 +1,12 @@
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
count = {}
for num in nums:
if num in count:
return True
else:
count[num] = 1
return False
+13
View File
@@ -0,0 +1,13 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
s_dict = {}
for c in s:
s_dict[c] = s_dict.get(c, 0) + 1
for c in t:
s_dict[c] = s_dict.get(c, 0) - 1
if s_dict[c] < 0:
return False
return True
@@ -4,10 +4,10 @@ class Solution:
r = len(height) - 1
max_area = (r-l) * min(height[l], height[r])
while (r-l > 2):
if (height[l] <= height[r]):
while r-l > 2:
if height[l] <= height[r]:
l += 1
else:
r -= 1
max_area = max(max_area, (r-l) * min(height[l], height[r]))
return max_area
return max_area
+31
View File
@@ -0,0 +1,31 @@
# Definition for a Node.
class Node:
def __init__(self, val = 0, neighbors = None):
self.val = val
self.neighbors = neighbors if neighbors is not None else []
from typing import Optional
from collections import deque
class Solution:
def cloneGraph(self, node: Optional["Node"]) -> Optional["Node"]:
if not node:
return None
graph_map = {}
q = deque()
q.append(node)
root = Node(node.val)
graph_map[node.val] = root
while q:
cur_node = q.popleft()
for neighbor in cur_node.neighbors:
if neighbor.val not in graph_map:
graph_map[neighbor.val] = Node(val=neighbor.val)
q.append(neighbor)
graph_map[cur_node.val].neighbors.append(graph_map[neighbor.val])
return root
@@ -0,0 +1,18 @@
class Solution:
def productExceptSelf(self, nums: list[int]) -> list[int]:
ans = [1] * len(nums)
curr = 1
for i in range(len(nums)):
ans[i] *= curr
curr *= nums[i]
curr = 1
for i in range(len(nums) - 1, -1, -1):
ans[i] *= curr
curr *= nums[i]
return ans
if __name__ == "__main__":
test = Solution()
print(test.productExceptSelf([-1,1,0,-3,3]))
+16
View File
@@ -0,0 +1,16 @@
from math import inf
def maxSubArray(nums):
max_sum = nums[0]
current_sum = nums[0]
for n in nums[1:]:
if current_sum < 0:
current_sum = 0
current_sum += n
if current_sum > max_sum:
max_sum = current_sum
return max_sum
if __name__ == "__main__":
print(maxSubArray([-2,1]))