Skip to content

Commit 8a2f09f

Browse files
committed
[Leo] 1st Week solutions
1 parent 72dbe50 commit 8a2f09f

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int]) -> int:
3+
minPrice = prices[0]
4+
maxPro = 0
5+
6+
for i in range(1, len(prices)):
7+
maxPro = max(maxPro, prices[i] - minPrice)
8+
minPrice = min (minPrice, prices[i])
9+
10+
return maxPro
11+
## TC: O(n) SC: O(1)..?

contains-duplicate/Leo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
return len(set(nums)) != len(nums)
4+
5+
# seen = set()
6+
7+
# for i in nums: ## O(n)
8+
# if i in seen: ## O(1)
9+
# return True
10+
# else:
11+
# seen.add(i)
12+
13+
# return False

two-sum/Leo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def twoSum(self, nums: List[int], target: int) -> List[int]:
3+
seen = {}
4+
5+
for key, val in enumerate(nums):
6+
diff = target - val
7+
8+
if diff in seen:
9+
return [seen[diff], key]
10+
else:
11+
seen[val] = key
12+
13+
# TC: O(n), SC: O(n)

valid-anagram/Leo.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
4+
counter_dict = collections.defaultdict(int)
5+
6+
for i in s:
7+
counter_dict[i] += 1
8+
9+
for i in t:
10+
counter_dict[i] -= 1
11+
12+
for val in counter_dict.values():
13+
if val != 0:
14+
return False
15+
16+
return True
17+
# TC:O(n), SC: O(len(s or t))
18+
# return sorted(s) == sorted(t) ## O(nlogn)

valid-palindrome/Leo.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
ans = [i for i in s.lower() if i.isalnum()]
4+
return ans == ans[::-1]
5+
6+
# TC: O(n)
7+
# SC: O(n)

0 commit comments

Comments
 (0)