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
39 changes: 39 additions & 0 deletions leetcode3/최원준/1202. Smallest String With Swaps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from collections import defaultdict

class Solution:
def smallestStringWithSwaps(self, s: str, pairs: list[list[int]]) -> str:
n = len(s)
s = list(s)

dic = defaultdict(list)

for i1, i2 in pairs:
dic[i1].append(i2)
dic[i2].append(i1)

visited = set()

for i in range(n):
if i in visited:
continue

stack = [i]
visited.add(i)
indexes = []

while stack:
cur = stack.pop()
indexes.append(cur)

for nxt in dic[cur]:
if nxt not in visited:
visited.add(nxt)
stack.append(nxt)

chars = sorted(s[j] for j in indexes)
indexes.sort()

for idx, char in zip(indexes, chars):
s[idx] = char

return "".join(s)
33 changes: 33 additions & 0 deletions leetcode3/최원준/2136. Earliest Possible Day of Full Bloom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution:
def earliestFullBloom(self, plantTime: list[int], growTime: list[int]) -> int:
'''
12345678901
0112
00001112
00012

12345678901
00001112
0112
00012

12345678901
00012
00001112
0112

1. growTime이 짧을 수록 마지막으로 간다.
2. growTime이 길수록 제일 앞으로 간다.
'''
n = len(plantTime)
types = [[plantTime[i], growTime[i]] for i in range(n)]
types.sort(key=lambda x: -x[1])

max_time = 0
next_day = 0
for plant_time, grow_time in types:
plant_day = next_day
next_day += plant_time

max_time = max(max_time, plant_day + plant_time + grow_time)
return max_time
Loading