Skip to content

Commit b69d13e

Browse files
authored
Merge pull request #447 from obzva/main
2 parents c7afa4d + a3b72bd commit b69d13e

File tree

5 files changed

+249
-0
lines changed

5 files changed

+249
-0
lines changed

โ€Ž3sum/flynn.pyโ€Ž

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'''
2+
ํ’€์ด
3+
- ์ค‘๋ณต๋˜๋Š” triplet์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฐ์—ด nums๋ฅผ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค
4+
- nums๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜์—ฌ res ๋ฐฐ์—ด์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค
5+
- ์ค‘๋ณต๋˜๋Š” triplet์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด appended set์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค
6+
7+
Big O
8+
- N: ๋ฐฐ์—ด nums์˜ ํฌ๊ธฐ
9+
10+
- Time complexity: O(N^2)
11+
- nums๋ฅผ ์ •๋ ฌํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์€ NlogN ํ˜•ํƒœ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
12+
- ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์€ N^2 ํ˜•ํƒœ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
13+
- O(NlogN + N^2)์—์„œ ์ฆ๊ฐ€์œจ์ด ๊ฐ€์žฅ ํฐ ํ•ญ์€ N^2์ด๋ฏ€๋กœ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N^2)์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
14+
15+
- Space complexity: O(N)
16+
- nums๋ฅผ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•˜์—ฌ sorted_nums์— ์ €์žฅํ•˜์˜€๊ณ  ์ด์— ํ•„์š”ํ•œ ๊ณต๊ฐ„์€ N์˜ ํ˜•ํƒœ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
17+
- ์ฒซ๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ ์•ˆ์˜ store์€ ์ตœ๋Œ€ N๋งŒํผ ์ปค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
18+
- appended ์ง‘ํ•ฉ์€ nums์˜ ๋ชจ๋“  ์›์†Œ๊ฐ€ ๊ณ ์œ ํ•˜๋”๋ผ๋„ N๋ณด๋‹ค ์ปค์งˆ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค
19+
'''
20+
21+
class Solution:
22+
def threeSum(self, nums: List[int]) -> List[List[int]]:
23+
n = len(nums)
24+
25+
sorted_nums = sorted(nums)
26+
27+
res = []
28+
29+
for i in range(n - 2):
30+
first = sorted_nums[i]
31+
32+
if i > 0 and first == sorted_nums[i - 1]:
33+
continue
34+
35+
store = {}
36+
store[-first - sorted_nums[i + 1]] = sorted_nums[i + 1]
37+
38+
appended = set()
39+
40+
for j in range(i + 2, n):
41+
second = sorted_nums[j]
42+
43+
if second in store and second not in appended:
44+
res.append([first, store[second], second])
45+
appended.add(second)
46+
store[-first - second] = second
47+
48+
return res
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
ํ’€์ด
3+
- ์ฃผ์–ด์ง„ ๋ฐฐ์—ด prices๋ฅผ ํ•œ ๋ฒˆ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
4+
5+
- prices[i]๊ฐ€ ์ด์ „์— ์ €์žฅํ•œ buy_price๋ณด๋‹ค ๋‚ฎ์„ ๊ฒฝ์šฐ,
6+
maximum profit์ด ์ฆ๊ฐ€ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
7+
๋”ฐ๋ผ์„œ buy_price์™€ sell_price๋ฅผ prices[i]๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค.
8+
9+
- prices[i]๊ฐ€ ์ด์ „์— ์ €์žฅํ•œ sell_price๋ณด๋‹ค ๋†’์€ ๊ฒฝ์šฐ,
10+
ํ˜„์žฌ buy_price๋กœ ์ง€๊ธˆ๊นŒ์ง€ ๊ธฐ๋กํ•œ profit๋ณด๋‹ค ๋” ๋†’์€ ์ด์ต์„ ๋‚ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
11+
๋”ฐ๋ผ์„œ sell_price๋ฅผ prices[i]๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ๊ฒฐ๊ณผ๊ฐ’์„ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.
12+
13+
Big O
14+
- N: ๋ฐฐ์—ด prices์˜ ํฌ๊ธฐ
15+
16+
- Time complexity: O(N)
17+
- ๋ฐฐ์—ด์˜ ํฌ๊ธฐ N์ด ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ์‹คํ–‰ ์‹œ๊ฐ„๋„ ์„ ํ˜•์ ์œผ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
18+
19+
- Space complexity: O(1)
20+
- ๋ฐฐ์—ด์˜ ํฌ๊ธฐ N์ด ์ฆ๊ฐ€ํ•˜์—ฌ๋„ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์€ ์ผ์ •ํ•ฉ๋‹ˆ๋‹ค.
21+
'''
22+
23+
class Solution:
24+
def maxProfit(self, prices: List[int]) -> int:
25+
res = 0
26+
27+
buy_price = prices[0]
28+
sell_price = prices[0]
29+
30+
for i in range(1, len(prices)):
31+
curr_price = prices[i]
32+
33+
if buy_price > curr_price:
34+
buy_price = curr_price
35+
sell_price = curr_price
36+
elif sell_price < curr_price:
37+
sell_price = curr_price
38+
res = max(res, sell_price - buy_price)
39+
40+
return res

โ€Žgroup-anagrams/flynn.pyโ€Ž

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
ํ’€์ด
3+
- ๋‘ ๋‹จ์–ด๊ฐ€ anagram ๊ด€๊ณ„๋ผ๋ฉด ๊ฐ ๋‹จ์–ด์˜ ์•ŒํŒŒ๋ฒณ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค
4+
- get_key ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๋‹จ์–ด๋ฅผ ์ด๋ฃจ๋Š” ์•ŒํŒŒ๋ฒณ ์ˆ˜์— ๋”ฐ๋ผ ๊ณ ์œ ํ•œ key๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค
5+
- key๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ํ•ด์‹œ๋งต์ธ key_map์„ ์ด์šฉํ•˜์—ฌ ํ˜„์žฌ ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ๋Š” ๋‹จ์–ด์™€ anagram์ธ ๋‹จ์–ด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค
6+
7+
Big O
8+
- N: ๋ฐฐ์—ด strs์˜ ํฌ๊ธฐ
9+
- K: ๋ฐฐ์—ด strs์˜ ์›์†Œ ์ค‘ ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด
10+
11+
- Time complexity: O(N * K)
12+
- ๋ฐฐ์—ด strs๋ฅผ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค -> N
13+
- ๊ฐ ๋ฌธ์ž์—ด๋งˆ๋‹ค ์•ŒํŒŒ๋ฒณ์˜ ์ˆ˜๋ฅผ ์„ธ๊ธฐ ์œ„ํ•ด ํ•œ ๋ฒˆ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค -> K
14+
15+
- Space complexity: O(N)
16+
- ๋ฐฐ์—ด strs์˜ ์›์†Œ ๋ชจ๋‘ ๊ณ ์œ ํ•œ key๋ฅผ ์ง€๋‹ˆ๊ณ  ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
17+
์ด ๊ฒฝ์šฐ key_map์˜ ํฌ๊ธฐ๋Š” N์— ๋น„๋ก€ํ•˜์—ฌ ์„ ํ˜•์ ์œผ๋กœ ์ฆ๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค -> N
18+
'''
19+
20+
class Solution:
21+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
22+
def get_key(s: str) -> str:
23+
count = [0] * 26
24+
for char in s:
25+
count[ord(char) - ord('a')] += 1
26+
res = ""
27+
for c in count:
28+
res += str(c) + ","
29+
return res
30+
31+
idx = 0
32+
key_map = {}
33+
res = []
34+
for s in strs:
35+
key = get_key(s)
36+
if key not in key_map:
37+
key_map[key] = idx
38+
idx += 1
39+
res.append([])
40+
res[key_map[key]].append(s)
41+
42+
return res
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'''
2+
ํ’€์ด
3+
- Trie ์ž๋ฃŒ๊ตฌ์กฐ ๊ตฌํ˜„์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค
4+
5+
Big O
6+
- N: ์ด์ „๊นŒ์ง€ insertํ•œ word์˜ ์ˆ˜
7+
- W: ํ˜„์žฌ insertํ•˜๊ฑฐ๋‚˜ searchํ•˜๋ ค๋Š” word์˜ ๊ธธ์ด
8+
- P: ํ˜„์žฌ startsWithํ•˜๋ ค๋Š” prefix์˜ ๊ธธ์ด
9+
10+
- insert
11+
- Time complexity: O(W)
12+
- word๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์— ๋Œ€ํ•ด ํ˜„์žฌ Trie์— ์ €์žฅ ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค
13+
ํ•ด์‹œ๋งต ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ์— ๊ฒ€์ƒ‰๊ณผ ์‚ฝ์ž… ๋ชจ๋‘ constant ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค
14+
- Space complexity: O(W)
15+
- ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ Trie์— ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
16+
- search
17+
- Time complexity: O(W)
18+
- insert์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค
19+
- Space complexity: O(1)
20+
- startsWith
21+
- Time complexity: O(P)
22+
- search์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค
23+
- Space complexity: O(1)
24+
'''
25+
26+
class Trie:
27+
28+
def __init__(self):
29+
self.root = {}
30+
31+
def insert(self, word: str) -> None:
32+
parent = self.root
33+
34+
for c in word:
35+
if c not in parent:
36+
parent[c] = {}
37+
parent = parent[c]
38+
39+
parent["word"] = word
40+
41+
def search(self, word: str) -> bool:
42+
parent = self.root
43+
44+
for c in word:
45+
if c not in parent:
46+
return False
47+
parent = parent[c]
48+
49+
return "word" in parent and parent["word"] == word
50+
51+
52+
def startsWith(self, prefix: str) -> bool:
53+
parent = self.root
54+
55+
for c in prefix:
56+
if c not in parent:
57+
return False
58+
parent = parent[c]
59+
60+
return True
61+
62+
63+
64+
# Your Trie object will be instantiated and called as such:
65+
# obj = Trie()
66+
# obj.insert(word)
67+
# param_2 = obj.search(word)
68+
# param_3 = obj.startsWith(prefix)

โ€Žword-break/flynn.pyโ€Ž

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
ํ’€์ด
3+
- BFS์™€ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๋ฐฉ์‹์„ ์ด์šฉํ•˜์—ฌ ํ’€์ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
4+
- ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s์˜ ๊ธธ์ด n๋ณด๋‹ค 1 ๋” ํฐ ํฌ๊ธฐ์˜ ๋ฐฐ์—ด visit์„ False๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ค๋‹ˆ๋‹ค
5+
n + 1๋กœ ์„ค์ •ํ•˜๋Š” ์ด์œ ๋Š” BFS ์„ค๊ณ„๋ฅผ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค
6+
- queue ๊ธฐ๋Šฅ์„ ํ•ด์ค„ deque์ธ dq๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๊ณ  ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ 0์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค
7+
dq์˜ ์›์†Œ curr๊ฐ€ ๊ฐ–๋Š” ์˜๋ฏธ๋Š” `ํ˜„์žฌ ํƒ์ƒ‰ ์ค‘์ธ substring์˜ ์‹œ์ž‘ index, ์ฆ‰ s[curr:]์ž…๋‹ˆ๋‹ค`
8+
- while๋ฌธ์„ ์ง„ํ–‰ํ•˜๋ฉฐ dq์—์„œ curr๋ฅผ popํ•ด์ค๋‹ˆ๋‹ค
9+
๋งŒ์•ฝ curr๋ฅผ ํƒ์ƒ‰ํ•œ ์ ์ด ์žˆ๋‹ค๋ฉด ์ง€๋‚˜์น˜๊ณ , ํƒ์ƒ‰ํ•œ ์ ์ด ์—†๋‹ค๋ฉด starts_with ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ 
10+
๊ทธ ์—ฌ๋ถ€์— ๋”ฐ๋ผ dq์— curr๋ฅผ ์ถ”๊ฐ€ ๋ฐ visit์„ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค
11+
12+
Big O
13+
- N: ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s์˜ ๊ธธ์ด
14+
- M: ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด ๋ฐฐ์—ด wordDict์˜ ๋ชจ๋“  ๋ฌธ์ž์—ด์˜ ๊ธธ์ด์˜ ํ•ฉ
15+
16+
- Time complexity: O(N * M)
17+
- visit ๋ฐฐ์—ด๋กœ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํƒ์ƒ‰์€ ์ตœ๋Œ€ N๋ฒˆ ์ด๋ค„์ง‘๋‹ˆ๋‹ค
18+
- ๊ฐ ํƒ์ƒ‰๋งˆ๋‹ค wordDict๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ starts_with๋ฅผ ์‹คํ–‰ํ•˜๋Š”๋ฐ ์ด ์‹คํ–‰ ์‹œ๊ฐ„์€ M์ด ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ์„ ํ˜•์ ์œผ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
19+
20+
- Space complexity: O(N)
21+
- visit ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” N์ด ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ์„ ํ˜•์ ์œผ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
22+
- deque์— ๋‹ด๊ธด ์›์†Œ์˜ ์ˆ˜๋Š” ์ตœ๋Œ€ N๊นŒ์ง€ ์ฆ๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
23+
'''
24+
25+
class Solution:
26+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
27+
n = len(s)
28+
visit = [False] * (n + 1)
29+
30+
def starts_with(idx: int, word: str) -> bool:
31+
m = len(word)
32+
for i in range(m):
33+
if s[idx + i] != word[i]:
34+
return False
35+
return True
36+
37+
dq = deque([0])
38+
39+
while dq:
40+
curr = dq.popleft()
41+
42+
if curr == len(s):
43+
return True
44+
45+
for word in wordDict:
46+
m = len(word)
47+
if curr + m <= n and not visit[curr + m] and starts_with(curr, word):
48+
dq.append(curr + m)
49+
visit[curr + m] = True
50+
51+
return False

0 commit comments

Comments
ย (0)