Skip to content
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
27 changes: 27 additions & 0 deletions best-time-to-buy-and-sell-stock/jylee2033.py
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Greedy
  • 설명: 이 코드는 매번 최적의 선택(최저 구매가와 최대 이익)을 하는 그리디 알고리즘을 사용하여 최대 수익을 계산합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# When length is 1, no profit possible
if len(prices) == 1:
return 0

buy = 10 ** 5
profit = 0

# Iterate through prices
for price in prices:
# if price < buy:
# buy = price

# for j in range(i + 1, len(prices)):
# if prices[j] <= buy:
# continue

# if prices[j] - buy > profit:
# profit = prices[j] - buy
buy = min(buy, price)
profit = max(profit, price - buy)

return profit

# Time Complexity: O(n)
# Space Complexity: O(1)
58 changes: 58 additions & 0 deletions encode-and-decode-strings/jylee2033.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Solution:
"""
@param: strs: a list of strings
@return: encodes a list of strings to a single string.
"""
def encode(self, strs):
# write your code here
# Encode each word with its length prefix and a "#"
# ["C#", "&"] -> "2#C#1#&"
encoded_str = ""

for word in strs:
encoded_str += f"{len(word)}#{word}"

return encoded_str

"""
@param: str: A string
@return: decodes a single string to a list of strings
"""
def decode(self, str):
# write your code here
# "2#C#1#&" -> ["C#", "&"]
decoded_lst = []
char_count = 0
reading_word = False
word = ""
length_str = ""

if str == "":
return [""]

for ch in str:
if ch == "#" and not reading_word:
# Finished reading the length prefix
# Switch to word-reading mode
char_count = int(length_str)
length_str = ""
reading_word = True

elif not reading_word:
# Accumulate digits for the length prefix
length_str += ch

else:
# reading_word is True
word += ch
char_count -= 1

if char_count == 0:
reading_word = False
decoded_lst.append(word)
word = ""

return decoded_lst

# Time Complexity: O(N)
# Space Complexity: O(N)
14 changes: 14 additions & 0 deletions group-anagrams/jylee2033.py
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set, Sorting
  • 설명: 이 코드는 문자열을 정렬하여 그룹화하는데 해시 맵을 사용하며, 정렬이 핵심입니다. 정렬된 문자열을 키로 하여 anagram 그룹을 구분합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagram_map = {}
for word in strs:
key = "".join(sorted(word))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

sorted한 키에 대해 단어를 추가하는 로직으로 깔끔하게 잘 짜신 것 같습니다!

if key not in anagram_map:
anagram_map[key] = [word]
else:
anagram_map[key].append(word)

return list(anagram_map.values())

# Time Complexity: O(N * K log K), N - number of strings, K - maximum length of a string (for sorting)
# Space Complexity: O(N * K)
Loading