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
23 changes: 23 additions & 0 deletions best-time-to-buy-and-sell-stock/ZetBe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
문제: 한번의 구매와 판매로 얻을 수 있는 최대 이익을 구하라.
풀이: 주어진 가격 리스트를 순회하면서 최저가와 최고가를 갱신하며 최대 이익을 계산한다.
최저가는 현재 최저가격보다 낮은 가격이 나오면 갱신하고,
최고가는 현재 최고가격보다 높은 가격이 나오면 갱신및 정답인지 비교한다.
시간복잡도: O(n)
공간복잡도: O(1)

'''


class Solution:
def maxProfit(self, prices: List[int]) -> int:
ma, mi = prices[0], prices[0]
answ = 0
for i in range(1, len(prices)):
if mi > prices[i]:
mi, ma = prices[i], prices[i]
if ma < prices[i]:
ma = prices[i]
answ = max(answ, ma-mi)
return answ

21 changes: 21 additions & 0 deletions group-anagrams/ZetBe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'''
문제: 주어진 문자열들을 아나그램끼리 그룹화하는 코드를 작성하시오.
풀이: 각 문자열을 정렬하여 동일한 아나그램끼리 같은 키로 묶어 딕셔너리에 저장한 후, 그 값을 반환합니다.(이 때, 해당 문자열을 정렬함으로써 일종의 키 값을 만듭니다.)
시간복잡도: O(n * k log k) (n은 문자열의 개수, k는 각 문자열의 최대 길이)
공간복잡도: O(n)

'''


class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
d = {}

for i in strs:
k = ''.join(sorted(i))
if k not in d:
d[k] = []
d[k].append(i)
return list(d.values())


34 changes: 34 additions & 0 deletions implement-trie-prefix-tree/ZetBe.py
Copy link
Contributor

Choose a reason for hiding this comment

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

자료구조만 변경해도 search 시 효율이 더 나오지 않을까 하는 생각입니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'''
문제: 특정 명령이 주어졌을 때, 트라이(Trie) 자료구조를 구현하는 코드를 작성하시오.
풀이: 단순히 문자열을 저장하는 리스트를 사용하여 트라이 자료구조의 기능을 구현합니다.
'''


class Trie:

def __init__(self):
self.arr = []

def insert(self, word: str) -> None:
self.arr.append(word)

def search(self, word: str) -> bool:
for i in self.arr:
if i == word:
return True
return False

def startsWith(self, prefix: str) -> bool:
n = len(prefix)
for i in self.arr:
if i[:n] == prefix:
return True
return False


# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)

23 changes: 23 additions & 0 deletions word-break/ZetBe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
문제: 주어진 문자열이 사전에 있는 단어들로 구성될 수 있는지 여부를 판단하는 코드를 작성하시오.
풀이: 동적 프로그래밍(DP)을 사용하여 문자열의 각 위치까지 사전에 있는 단어들로 구성될 수 있는지 여부를 기록합니다.
시간복잡도: O(n*m) (n은 문자열 s의 길이, m은 단어 사전의 단어 수)
공간복잡도: O(n)
'''


class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
dp = [False for i in range(len(s))]
for i in wordDict:
Copy link
Contributor

Choose a reason for hiding this comment

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

해당 부분을 해결하지 못해서 힌트를 보고 풀었는데 이렇게 빼내서 해결할 수도 있었겠네요

if i == s[:len(i)]:
dp[len(i)-1] = True

for i in range(1, len(s)):
if dp[i-1] == True:

for j in wordDict:
if i+len(j) <= len(s) and j == s[i:i+len(j)]:
dp[i+len(j)-1] = True
return dp[len(s)-1]