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
30 changes: 30 additions & 0 deletions longest-substring-without-repeating-characters/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'''
set_arr(현재 윈도우)에 중복 없는 부분 문자열을 유지하며 가장 긴 길이를 갱신한다.

예) s="dvdf"
1. ['d']
2. ['d','v']
3. 중복 'd' 발생 → 앞에 있던 중복 문자(d)까지는 버리고, 그 뒤에 있는 문자(v)부터 다시 윈도우로 유지한다
4. 'f' 추가 → ['v','d','f']
5. 최종 max_length=3

Time Complexity: O(n^2)
- 문자열을 한 번 순회하지만, 매 단계마다 리스트에서 in, index, 슬라이싱이 선형 시간이어서 최악 O(n^2)

Space Complexity: O(n)
- 최악의 경우 윈도우가 길이 n까지 커질 수 있음
'''
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
set_arr = []
max_length = 0
for char in s:
if char not in set_arr:
set_arr.append(char)
else:
# 중복 문자가 발견되면 처음 중복 문자의 인덱스를 찾아 그 이전까지 자르고 현재 문자 추가
dup_idx = set_arr.index(char)
set_arr= set_arr[dup_idx+1:]
set_arr.append(char)
max_length = max(max_length, len(set_arr))
return max_length
23 changes: 23 additions & 0 deletions reverse-linked-list/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
Linked List의 노드는 value와 next 포인터를 가지고 있다는 특성을 이용해 문제를 풀었음

Time Complexity: O(n)
- Linked List의 모든 노드를 한 번씩 방문

Space Complexity: O(1)
- 두개 의 포인터만 사용하여 추가적인 공간 없음

'''

class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = None
cur = head

while cur:
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt

return prev
31 changes: 31 additions & 0 deletions set-matrix-zeroes/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'''
Set 자료구조를 사용해서 0이 있는 행과 열을 기록한 후, 해당 행과 열을 0으로 변경

Time Complexity: O(m*n)
- 행렬의 모든 원소를 한 번씩 방문
Space Complexity: O(m+n)
- 행과 열의 index를 저장하는데 각각 최대 m, n 크기가 필요함

참고: 문제에서 요구한 in-place 변경을 지키지 못함, 행과 열을 저장하는데 추가 공간이 필요하기 때문
'''
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
zero_rows = set()
zero_cols = set()

for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j] == 0:
zero_rows.add(i)
zero_cols.add(j)

for row in zero_rows:
for j in range(len(matrix[row])):
matrix[row][j] = 0

for col in zero_cols:
for i in range(len(matrix)):
matrix[i][col] = 0