Skip to content

[yeoju] Week 5 #865

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 7 commits into from
Jan 12, 2025
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
19 changes: 19 additions & 0 deletions best-time-to-buy-and-sell-stock/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 시간복잡도 O(n)
# 공간복잡도 O(1)
class Solution:
def maxProfit(self, prices: list[int]) -> int:
min_p = prices[0] # 최소 가격 설정 : 배열의 첫 번째 가격
cur = 0
max_p = 0
for n in prices:
if n < min_p: # 현재 가격이 최소 가격보다 작다면 최소가격 갱신
min_p = n
cur = n - min_p # 현재 이익 계산
if max_p < cur: # 현재 이익과 최대로 얻을 수 있는 이익 비교
max_p = cur # 최대 이익 갱신신

return max_p




19 changes: 19 additions & 0 deletions group-anagrams/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#시간복잡도 O(n * klogk)
#공간복잡도 O(n * k)
# => n개의 단어 * 각 단어의 k만큼의 길이
class Solution:
def groupAnagrams(self, strs: list[str]) -> list[list[str]]:
ans = {}

for word in strs:
# 단어의 문자를 정렬 후 키로 사용
sortedWord = ''.join(sorted(word)) # sorted()의 시간복잡도 : O(klogk)
# 초기 리스트 생성
if sortedWord not in ans:
ans[sortedWord] = []
ans[sortedWord].append(word)

# 딕셔너리의 value를 list로 변환
ansLst = list(ans.values())
return ansLst

17 changes: 17 additions & 0 deletions word-break/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#시간복잡도 O(N * M)
#공간복잡도 O(N)
class Solution:
def wordBreak(self, s: str, wordDict: list[str]) -> bool:
# 문자열 s의 0번째부터 i번째까지의 문자열이 wordDict의 단어로 분할될 수 있으면 True
# 빈 문자열 s[0:0]은 항상 분할이 가능하므로 dp[0] = True
dp = [False] * (len(s) + 1)
dp[0] = True
for i in range(1, len(s) + 1):
for word in wordDict:
# 현재 word가 i - len(word)부터 i번째까지의 s의 부분문자열과 일치한다면 True
# 바로 이전의 지점이 True이고 word만큼의 s 부분문자열이 wordDict 내에 존재한다면 dp[i] = True
if i >= len(word) and dp[i - len(word)] and s[i - len(word):i] == word:
dp[i] = True
break
return dp[-1]

Loading