Skip to content

Sam's week 1 solutions #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions best-time-to-buy-and-sell-stock/Bumsu-Yi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
"""
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
"""


class Solution:
def maxProfit(self, prices: List[int]) -> int:
max_profit = 0
min_price = prices[0]

for price in prices:
profit = price - min_price
min_price = min(min_price, price)
max_profit = max(profit, max_profit)

return max_profit
12 changes: 12 additions & 0 deletions contains-duplicate/Bumsu-Yi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
"""
https://leetcode.com/problems/contains-duplicate/
"""


class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
my_dict = {}

for num in nums:
if num in my_dict:
return True
my_dict[num] = 0

return False
12 changes: 11 additions & 1 deletion two-sum/Bumsu-Yi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
"""
https://leetcode.com/problems/two-sum/
"""
#


class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
numMap = {}

for i in range(len(nums)):
complement = target - nums[i]
if complement in numMap:
return [numMap[complement], i]

numMap[nums[i]] = i
14 changes: 14 additions & 0 deletions valid-anagram/Bumsu-Yi.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ํ’€์ด๋Š” ์ผ๋ถ€๋Ÿฌ defaultdict ๋ฅผ ์•ˆ์“ฐ๊ณ  ํ’€์œผ์‹ ๊ฑด๊ฐ€์š”? ์ง€๋‚œ๋ฒˆ์— ์งˆ๋ฌธ ์˜ฌ๋ฆฌ์‹ ๊ฑธ ๋ณธ ๊ฑฐ๊ฐ™์€๋ฐ ๊ธฐ์–ต์ด ์ž˜ ์•ˆ๋‚˜๋„ค์š” :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜๋„ํ•˜๊ณ  defaultdict์„ ์•ˆ์“ด ๊ฑด ์•„๋‹ˆ์ง€๋งŒ ์ฒ˜์Œ์—” defaultdict์„ ๊ณ ๋ คํ•˜์ง€ ์•Š์•˜์ง€๋งŒ ํ›„์— defaultdict๊ณผ ์ผ๋ฐ˜ ๋”•์…”๋„ˆ๋ฆฌ์˜ ์„ฑ๋Šฅ์ฐจ์ด๋Š” ์—†๋Š” ๊ฒƒ ๊ฐ™์•„ ์ฒ˜์Œ ์†”๋ฃจ์…˜์„ ๊ทธ๋Œ€๋กœ ์ปค๋ฐ‹ํ–ˆ์Šต๋‹ˆ๋‹ค! ํ˜น์‹œ my_dict1.get(char, 0) + 1 ์—ฐ์‚ฐ์„ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค defaultdict์ด ๋” ํšจ์œจ์ ์ด๋ผ ๋ง์”€ํ•˜์‹œ๋Š” ๊ฑธ๊นŒ์š”?

์งˆ๋ฌธ์€ ๋””์ฝ”์ฑ„๋„ ๋„์™€์ฃผ์„ธ์š” - ์ฝ”๋”ฉํ…Œ์ŠคํŠธ - Valid anagram์— ์žˆ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•„ ์•„๋‡จ ใ…‹ใ…‹ ์งˆ๋ฌธํ•˜์‹ค ๋•Œ ํ•œ ๊ฐœ๋งŒ ์“ด๊ฒŒ ๋‚ซ๋ƒ๊ณ  ์—ฌ์ญค๋ณด์‹ ๊ฑฐ ๊ฐ™์€๋ฐ ๊ทธ๋Œ€๋กœ ์˜ฌ๋ฆฌ์…จ๊ธธ๋ž˜ ๋ญ”๊ฐ€ ๋‹ค๋ฅธ ์ด์œ ๊ฐ€ ์žˆ๋‚˜ ์‹ถ์–ด์„œ ์—ฌ์ญค๋ดค๋˜๊ฑฐ์˜ˆ์š” ใ…‹ใ…‹ใ…‹ ์–ด์ฐจํ”ผ ๋‘ ๋ฐฉ๋ฒ• ๋‹ค ํฐ ์ฐจ์ด๋Š” ์—†์–ด๋ณด์ž…๋‹ˆ๋‹ค!

Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
"""
https://leetcode.com/problems/valid-anagram/
"""


class Solution:
def isAnagram(self, s: str, t: str) -> bool:
my_dict1 = {}
my_dict2 = {}

for char in s:
my_dict1[char] = my_dict1.get(char, 0) + 1

for char in t:
my_dict2[char] = my_dict2.get(char, 0) + 1

return my_dict1 == my_dict2
11 changes: 11 additions & 0 deletions valid-palindrome/Bumsu-Yi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
"""
https://leetcode.com/problems/valid-palindrome/
"""


class Solution:
def isPalindrome(self, s: str) -> bool:
alphanumeric_chars = []
for char in s:
if char.isalnum():
lowercase_letter = char.lower()
alphanumeric_chars.append(lowercase_letter)

return alphanumeric_chars == alphanumeric_chars[::-1]