File tree Expand file tree Collapse file tree 5 files changed +62
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 5 files changed +62
-0
lines changed Original file line number Diff line number Diff line change 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)..?
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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)
Original file line number Diff line number Diff line change 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)
Original file line number Diff line number Diff line change 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)
You can’t perform that action at this time.
0 commit comments