Skip to content
Open
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/sangbeenmoon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# TC : O(n)
# SC : O(1)

class Solution:
def maxProfit(self, prices: List[int]) -> int:
answer = 0
mm = 10001

for price in prices:
mm = min(mm, price)
answer = max(answer, price - mm)

return answer
43 changes: 43 additions & 0 deletions encode-and-decode-strings/sangbeenmoon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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
result = ""

delimiter = ":;"

for i, str in enumerate(strs):
result = result + str
if i != len(strs):
result = result + delimiter

print(result)

return result

"""
@param: str: A string
@return: decodes a single string to a list of strings
"""
def decode(self, str):
result = []

i = 0
delimiter = ":;"

while i < len(str):

j = i
while j < len(str) - 1:
if str[j:j+2] == delimiter:
result.append(str[i:j])
break
else:
j = j + 1

i = j + 2

return result
35 changes: 35 additions & 0 deletions group-anagrams/sangbeenmoon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# TC : O(m^2 * n) m : len(strs), n : len(strs[0])
# SC : O(m * n)

class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
sorted_strs = []
for str in strs:
sorted_strs.append(sorted(str))

answer = []

i = 0

visited = [False] * 10001

while i < len(strs):
if visited[i]:
i = i + 1
continue

sub_answer = []
target = sorted_strs[i]
sub_answer.append(strs[i])

for j in range(i+1, len(strs)):
if sorted_strs[j] == target:
visited[j] = True
sub_answer.append(strs[j])

i = i + 1
answer.append(sub_answer)

return answer


57 changes: 57 additions & 0 deletions implement-trie-prefix-tree/sangbeenmoon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.HashMap;
import java.util.Map;

class Trie {

Map<Character, Trie> map;
boolean isEnd = false;

public Trie() {
map = new HashMap<>();
}

public void insert(String word) {
Trie trie;
if (map.containsKey(word.charAt(0))) {
trie = map.get(word.charAt(0));
} else {
trie = new Trie();
}
map.put(word.charAt(0), trie);

if (word.length() == 1) {
map.get(word.charAt(0)).isEnd = true;
return;
}

trie.insert(word.substring(1));
}

public boolean search(String word) {
if (map.containsKey(word.charAt(0))) {
if (word.length() == 1) {
return map.get(word.charAt(0)).isEnd;
}
return map.get(word.charAt(0)).search(word.substring(1));
}
return false;
}

public boolean startsWith(String prefix) {
if (map.containsKey(prefix.charAt(0))) {
if (prefix.length() == 1) {
return true;
}
return map.get(prefix.charAt(0)).startsWith(prefix.substring(1));
}
return false;
}
}

/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/
29 changes: 29 additions & 0 deletions word-break/sangbeenmoon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 실패한 풀이.
# AC 를 받기는 했으나 test case 가 좀 더 촘촘했다면 TLE 에 걸렸을 것임.
# string 이 아닌 index 로 memoization 을 하는 걸 떠올려보자.

class Solution:
answer = False
visited = {}
def wordBreak(self, s: str, wordDict: List[str]) -> bool:

self.answer = False
self.visited = {}
self.go(0,s,wordDict)

return self.answer

def go(self, i:int, s: str, wordDict: List[str]):
if i >= (len(s)):
self.answer = True
return

for word in wordDict:
if i + len(word) > len(s):
continue

if word == s[i:i + len(word)]:
if not s[i + len(word) : ] in self.visited:
self.go(i + len(word), s, wordDict)

self.visited[s[i:]] = False
Loading