Compare commits
10
Commits
4e95701e61
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33af13cb50 | ||
|
|
2d2ccc70bd | ||
|
|
98d07c8e61 | ||
|
|
9c65fc3bcf | ||
|
|
059776458a | ||
|
|
d8665c4e6a | ||
|
|
36611fddaa | ||
|
|
c70da65d0f | ||
|
|
1bf5d2ab15 | ||
|
|
5daed67768 |
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,13 @@
|
||||
class Solution:
|
||||
def maxArea(self, height: list[int]) -> int:
|
||||
l = 0
|
||||
r = len(height) - 1
|
||||
max_area = (r-l) * min(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
|
||||
@@ -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]))
|
||||
@@ -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]))
|
||||
@@ -0,0 +1,3 @@
|
||||
[tool.ruff.lint]
|
||||
select = ["ALL"]
|
||||
ignore = ["D", "ANN"]
|
||||
Reference in New Issue
Block a user