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
38 changes: 38 additions & 0 deletions longest-substring-without-repeating-characters/prograsshopper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
# sol 1
# time complexity: O(n^2) / memory complexity: O(n)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주석으로 시간/공간 복잡도를 잘 명시해주셔서 이해에 도움이 되네요!

if len(s) in [0, 1]:
return len(s)

from collections import defaultdict
strings = []
for i in range(0, len(s) - 1):
char_dict = defaultdict(int)
char_dict[s[i]] += 1
for j in range(i + 1, len(s)):
char_dict[s[j]] += 1
if char_dict[s[j]] > 1:
strings.append(s[i:j])
break
else:
strings.append(s[i:])

max_len = len(strings[0])
for elem in strings:
max_len = max(max_len, len(elem))
return max_len

# sol 2
# time complexity: O(n) / memory complexity: O(n)
str_set = set()
left = 0
max_len = 0

for right in range(len(s)):
while s[right] in str_set:
str_set.remove(s[left])
left += 1
str_set.add(s[right])
max_len = max(max_len, right - left + 1)
return max_len
20 changes: 20 additions & 0 deletions reverse-linked-list/prograsshopper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
# time complexity: O(n) / memory complexity: O(n)
stack = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

스택을 활용한 방식도 좋지만, 추가 메모리 없이 in-place로 처리하는 방법도 많이 사용되니 참고 바랍니다.

current = head
while current:
stack.append(current.val)
current = current.next

dummy_head = ListNode()
current = dummy_head
while stack:
current.next = ListNode(val=stack.pop(), next=None)
current = current.next
return dummy_head.next