Skip to content

Commit adfae98

Browse files
authored
Merge pull request #1485 from hi-rachel/main
[hi-rachel] WEEK 07 Solutions
2 parents 2b5d653 + 7f329f5 commit adfae98

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
문자열 s가 주어졌을 때, 중복된 문자를 제거하고 가장 긴 연속된 substring을 찾아라
3+
4+
1. 첫번째 풀이
5+
6+
Hint!
7+
Generate all possible substrings & check for each substring if it's valid and keep updating maxLen accordingly.
8+
9+
TC: O(N^3)
10+
SC: O(N)
11+
=> Time Limit Exceeded
12+
13+
2. 최적화 - 슬라이딩 윈도우 + 해시셋
14+
- 슬라이딩 윈도우: 문자열 내에서 left, right 포인터로 범위를 정하고 점진적으로 이동
15+
- 해시셋: 현재 윈도우에 어떤 문자가 있는지 빠르게 체크 가능
16+
- 중복 문자가 등장하면 왼쪽 포인터를 이동시켜서 중복을 제거
17+
18+
TC: O(N)
19+
SC: O(N)
20+
"""
21+
22+
# Time Limit Exceeded 풀이
23+
class Solution:
24+
def lengthOfLongestSubstring(self, s: str) -> int:
25+
max_len = 0
26+
27+
# Generate all possible substrings without duplicate characters
28+
def make_all_substrings(string):
29+
nonlocal max_len
30+
n = len(string)
31+
for i in range(n):
32+
for j in range(i + 1, n + 1):
33+
k = len(string[i:j])
34+
if k == len(list(set(string[i:j]))): # if it's valid
35+
max_len = max(k, max_len) # keep updating maxLen accordingly
36+
37+
make_all_substrings(s)
38+
return max_len
39+
40+
41+
# 최적화 - 슬라이딩 윈도우 + 해시셋 풀이
42+
class Solution:
43+
def lengthOfLongestSubstring(self, s: str) -> int:
44+
char_set = set() # 현재 윈도우에 있는 문자들
45+
left = 0
46+
max_len = 0
47+
48+
for right in range(len(s)):
49+
# 중복 문자가 나오면 왼쪽 포인터를 이동시켜 중복 제거
50+
while s[right] in char_set:
51+
char_set.remove(s[left])
52+
left += 1
53+
54+
# 중복이 없으면 윈도우에 추가
55+
char_set.add(s[right])
56+
57+
# 최대 길이 갱신
58+
max_len = max(max_len, right - left + 1)
59+
60+
return max_len

reverse-linked-list/hi-rachel.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
7+
"""
8+
1. Stack 활용 (LIFO)
9+
- LinkedList의 모든 원소를 Stack에 넣고 꺼냄
10+
TC: O(n) time
11+
SC: O(n) space
12+
13+
2. 최적화 풀이
14+
- 현재 LinkedList를 거꾸로 뒤짚기
15+
TC: O(n) -> LinkedList를 딱 한 번 순회
16+
SC: O(1) -> 변수를 포인터 2개만 사용
17+
"""
18+
19+
# Stack 풀이
20+
class Solution:
21+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
22+
stack = []
23+
node = head
24+
while node:
25+
stack.append(node)
26+
node= node.next
27+
28+
dummy = ListNode(-1)
29+
node = dummy
30+
while stack:
31+
node.next = stack.pop()
32+
node = node.next
33+
node.next = None
34+
return dummy.next
35+
36+
# 최적화
37+
class Solution:
38+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
39+
prev, curr = None, head
40+
while curr:
41+
temp_next = curr.next
42+
curr.next = prev
43+
prev, curr = curr, temp_next
44+
return prev

0 commit comments

Comments
 (0)