Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Leo] 1st Week solutions #35

Merged
merged 1 commit into from
Apr 30, 2024
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
11 changes: 11 additions & 0 deletions best-time-to-buy-and-sell-stock/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def maxProfit(self, prices: List[int]) -> int:
minPrice = prices[0]
maxPro = 0

for i in range(1, len(prices)):
maxPro = max(maxPro, prices[i] - minPrice)
minPrice = min (minPrice, prices[i])

return maxPro
## TC: O(n) SC: O(1)..?
13 changes: 13 additions & 0 deletions contains-duplicate/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(set(nums)) != len(nums)

# seen = set()

# for i in nums: ## O(n)
# if i in seen: ## O(1)
# return True
# else:
# seen.add(i)

# return False
13 changes: 13 additions & 0 deletions two-sum/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
seen = {}

for key, val in enumerate(nums):
diff = target - val

if diff in seen:
return [seen[diff], key]
else:
seen[val] = key

# TC: O(n), SC: O(n)
18 changes: 18 additions & 0 deletions valid-anagram/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:

counter_dict = collections.defaultdict(int)

for i in s:
counter_dict[i] += 1

for i in t:
counter_dict[i] -= 1

for val in counter_dict.values():
if val != 0:
return False

return True
# TC:O(n), SC: O(len(s or t))
# return sorted(s) == sorted(t) ## O(nlogn)
7 changes: 7 additions & 0 deletions valid-palindrome/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
def isPalindrome(self, s: str) -> bool:
ans = [i for i in s.lower() if i.isalnum()]
return ans == ans[::-1]

# TC: O(n)
# SC: O(n)