diff --git a/best-time-to-buy-and-sell-stock/Leo.py b/best-time-to-buy-and-sell-stock/Leo.py new file mode 100644 index 000000000..783de27da --- /dev/null +++ b/best-time-to-buy-and-sell-stock/Leo.py @@ -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)..? diff --git a/contains-duplicate/Leo.py b/contains-duplicate/Leo.py new file mode 100644 index 000000000..abeaf421b --- /dev/null +++ b/contains-duplicate/Leo.py @@ -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 diff --git a/two-sum/Leo.py b/two-sum/Leo.py new file mode 100644 index 000000000..37686a1d3 --- /dev/null +++ b/two-sum/Leo.py @@ -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) diff --git a/valid-anagram/Leo.py b/valid-anagram/Leo.py new file mode 100644 index 000000000..d5081a794 --- /dev/null +++ b/valid-anagram/Leo.py @@ -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) diff --git a/valid-palindrome/Leo.py b/valid-palindrome/Leo.py new file mode 100644 index 000000000..291113c22 --- /dev/null +++ b/valid-palindrome/Leo.py @@ -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)