File tree 5 files changed +74
-0
lines changed
best-time-to-buy-and-sell-stock
5 files changed +74
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
3
+ """
4
+ # - time complexity : O(n)
5
+ # - space complexity : O(n)
6
+
7
+ class Solution :
8
+ def maxProfit (self , prices : List [int ]) -> int :
9
+ now = prices [0 ]
10
+ diff = [0 ] * len (prices )
11
+
12
+ for i , v in enumerate (prices [1 :]):
13
+ if now > v :
14
+ now = v
15
+ else :
16
+ diff [i + 1 ] = v - now
17
+
18
+ return max (diff )
Original file line number Diff line number Diff line change
1
+ """
2
+ https://leetcode.com/problems/contains-duplicate/
3
+ """
4
+ # - time complexity : O(n)
5
+ # - space complexity : O(n)
6
+
7
+ class Solution :
8
+ def containsDuplicate (self , nums : List [int ]) -> bool :
9
+ visited = {}
10
+ for n in nums :
11
+ if n in visited :
12
+ return True
13
+ visited [n ] = True
14
+ return False
Original file line number Diff line number Diff line change
1
+ """
2
+ https://leetcode.com/problems/two-sum/
3
+ """
4
+ # - time complexity : O(nlogn)
5
+ # - space complexity : O(n)
6
+
7
+ class Solution :
8
+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
9
+ s , e = 0 , len (nums ) - 1
10
+ arr = [nums [i ]:= [n ,i ] for i , n in enumerate (nums )]
11
+
12
+ arr .sort (key = lambda x : x [0 ])
13
+
14
+ while s <= e :
15
+ now = arr [s ][0 ] + arr [e ][0 ]
16
+ if now == target :
17
+ return [arr [s ][1 ], arr [e ][1 ]]
18
+ if now < target :
19
+ s += 1
20
+ else :
21
+ e -= 1
Original file line number Diff line number Diff line change
1
+ """
2
+ https://leetcode.com/problems/valid-anagram/
3
+ """
4
+ # - time complexity : O(n)
5
+ # - space complexity : O(n)
6
+
7
+ from collections import Counter
8
+
9
+ class Solution :
10
+ def isAnagram (self , s : str , t : str ) -> bool :
11
+ return Counter (s ) == Counter (t )
Original file line number Diff line number Diff line change
1
+ """
2
+ https://leetcode.com/problems/valid-palindrome/
3
+ """
4
+ # - time complexity : O(n)
5
+ # - space complexity : O(n)
6
+
7
+ class Solution :
8
+ def isPalindrome (self , s : str ) -> bool :
9
+ s = "" .join ([i for i in s if i .isalnum ()]).lower ()
10
+ return s == s [::- 1 ]
You can’t perform that action at this time.
0 commit comments