Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kayden] Week 07 Solutions #487

Merged
merged 7 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 20 additions & 0 deletions longest-substring-without-repeating-characters/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
# 시간복잡도: O(N)
# 공간복잡도: O(N)
# set에서 제외하는 로직을 없애기 위해, map을 사용해서 idx값을 저장후, start와 비교해서 start보다 작은 idx를 가진 경우에는 중복이 아니라고 판단했습니다.
def lengthOfLongestSubstring(self, s: str) -> int:

last_idx = {}
answer = 0
start = 0

for idx, ch in enumerate(s):
# 중복 조회시 idx값과 start 값 비교를 통해, 삭제하는 로직을 없이 중복을 확인했습니다.
if ch in last_idx and last_idx[ch] >= start:
start = last_idx[ch] + 1
last_idx[ch] = idx
else:
answer = max(answer, idx - start + 1)
last_idx[ch] = idx

return answer
26 changes: 26 additions & 0 deletions reverse-linked-list/kayden.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
// 시간복잡도: O(N)
// 공간복잡도: O(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

반복문 내에서 새로운 ListNode를 만들어내기 때문에 공간 복잡도 또한 N에 비례하여 선형적으로 증가할 것 같습니다

Copy link
Contributor Author

Choose a reason for hiding this comment

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

답변 감사합니다! 최적화해서 수정 했습니다!

class Solution {
public ListNode reverseList(ListNode head) {

ListNode prev = null;

while (head != null){
ListNode curr = new ListNode(head.val, prev);
prev = curr;
head = head.next;
}

return prev;
}
}