Skip to content

Commit 1355050

Browse files
authored
Merge pull request DaleStudy#22 from SamTheKorean/solution1
Sam's week 1 solutions
2 parents 414820a + da98fa4 commit 1355050

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
"""
22
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
33
"""
4+
5+
6+
class Solution:
7+
def maxProfit(self, prices: List[int]) -> int:
8+
max_profit = 0
9+
min_price = prices[0]
10+
11+
for price in prices:
12+
profit = price - min_price
13+
min_price = min(min_price, price)
14+
max_profit = max(profit, max_profit)
15+
16+
return max_profit

contains-duplicate/Bumsu-Yi.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
"""
22
https://leetcode.com/problems/contains-duplicate/
33
"""
4+
5+
6+
class Solution:
7+
def containsDuplicate(self, nums: List[int]) -> bool:
8+
my_dict = {}
9+
10+
for num in nums:
11+
if num in my_dict:
12+
return True
13+
my_dict[num] = 0
14+
15+
return False

two-sum/Bumsu-Yi.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
"""
22
https://leetcode.com/problems/two-sum/
33
"""
4-
#
54

5+
6+
class Solution:
7+
def twoSum(self, nums: List[int], target: int) -> List[int]:
8+
numMap = {}
9+
10+
for i in range(len(nums)):
11+
complement = target - nums[i]
12+
if complement in numMap:
13+
return [numMap[complement], i]
14+
15+
numMap[nums[i]] = i

valid-anagram/Bumsu-Yi.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
"""
22
https://leetcode.com/problems/valid-anagram/
33
"""
4+
5+
6+
class Solution:
7+
def isAnagram(self, s: str, t: str) -> bool:
8+
my_dict1 = {}
9+
my_dict2 = {}
10+
11+
for char in s:
12+
my_dict1[char] = my_dict1.get(char, 0) + 1
13+
14+
for char in t:
15+
my_dict2[char] = my_dict2.get(char, 0) + 1
16+
17+
return my_dict1 == my_dict2

valid-palindrome/Bumsu-Yi.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
"""
22
https://leetcode.com/problems/valid-palindrome/
33
"""
4+
5+
6+
class Solution:
7+
def isPalindrome(self, s: str) -> bool:
8+
alphanumeric_chars = []
9+
for char in s:
10+
if char.isalnum():
11+
lowercase_letter = char.lower()
12+
alphanumeric_chars.append(lowercase_letter)
13+
14+
return alphanumeric_chars == alphanumeric_chars[::-1]

0 commit comments

Comments
 (0)