From 52823f8b3f58c284b68b0287913bb624c6fb130a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Tue, 24 Sep 2024 22:55:43 +0900 Subject: [PATCH 1/3] Add week 7 solutions: reverse-linked-list --- reverse-linked-list/gitsunmin.ts | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 reverse-linked-list/gitsunmin.ts diff --git a/reverse-linked-list/gitsunmin.ts b/reverse-linked-list/gitsunmin.ts new file mode 100644 index 000000000..963439fd3 --- /dev/null +++ b/reverse-linked-list/gitsunmin.ts @@ -0,0 +1,50 @@ +/** + * https://leetcode.com/problems/reverse-linked-list + * time complexity : O(n) + * space complexity : O(1) + */ + +class ListNode { + val: number + next: ListNode | null + constructor(val?: number, next?: ListNode | null) { + this.val = (val === undefined ? 0 : val) + this.next = (next === undefined ? null : next) + } +} + +/** Not used in real problem */ +const arrayToListNode = (arr: number[]): ListNode | null => { + if (arr.length === 0) return null; + let head = new ListNode(arr[0]); + let curr = head; + for (let i = 1; i < arr.length; i++) { + curr.next = new ListNode(arr[i]); + curr = curr.next; + } + return head; +} + +/* Main function */ +function reverseList(head: ListNode | null): ListNode | null { + let prev: ListNode | null = null; + let current = head; + + while (current !== null) { + const next = current.next; + current.next = prev; + prev = current; + current = next; + } + + return prev; +}; + +/* Examples */ +const input1 = [1, 2, 3, 4, 5]; +const input2 = [1, 2]; +const input3 = []; + +console.log('output1:', reverseList(arrayToListNode(input1))); +console.log('output2:', reverseList(arrayToListNode(input2))); +console.log('output3:', reverseList(arrayToListNode(input3))); From 6e15e646f0e0ed1aa8f3c3081ae26d57a05e7a72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Tue, 24 Sep 2024 23:20:15 +0900 Subject: [PATCH 2/3] Add week 7 solutions: longest-substring-without-repeating-characters --- .../gitsunmin.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 longest-substring-without-repeating-characters/gitsunmin.ts diff --git a/longest-substring-without-repeating-characters/gitsunmin.ts b/longest-substring-without-repeating-characters/gitsunmin.ts new file mode 100644 index 000000000..8487705da --- /dev/null +++ b/longest-substring-without-repeating-characters/gitsunmin.ts @@ -0,0 +1,26 @@ +/** + * https://leetcode.com/problems/longest-substring-without-repeating-characters/ + * time complexity : O(n) + * space complexity : O(n) + */ +function lengthOfLongestSubstring(s: string): number { + let n = s.length, ans = 0; + const map = new Map(); + for (let j = 0, i = 0; j < n; j++) { + if (map.has(s[j])) { + i = Math.max((map.get(s[j]) ?? 0) + 1, i); + } + ans = Math.max(ans, j - i + 1); + map.set(s[j], j); + } + console.log('map:', map); + return ans; +}; + +const testInput1 = "abcabcbb"; +const testInput2 = "bbbbb"; +const testInput3 = "pwwkew"; + +console.log('output1:', lengthOfLongestSubstring(testInput1), 'expected:', 3); +console.log('output2:', lengthOfLongestSubstring(testInput2), 'expected:', 1); +console.log('output3:', lengthOfLongestSubstring(testInput3), 'expected:', 3); From efadacbc158d3f9d7bef032bc9b8b62ee6c977d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 28 Sep 2024 19:43:04 +0900 Subject: [PATCH 3/3] remove log --- .../gitsunmin.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/longest-substring-without-repeating-characters/gitsunmin.ts b/longest-substring-without-repeating-characters/gitsunmin.ts index 8487705da..371739b3b 100644 --- a/longest-substring-without-repeating-characters/gitsunmin.ts +++ b/longest-substring-without-repeating-characters/gitsunmin.ts @@ -13,14 +13,5 @@ function lengthOfLongestSubstring(s: string): number { ans = Math.max(ans, j - i + 1); map.set(s[j], j); } - console.log('map:', map); return ans; }; - -const testInput1 = "abcabcbb"; -const testInput2 = "bbbbb"; -const testInput3 = "pwwkew"; - -console.log('output1:', lengthOfLongestSubstring(testInput1), 'expected:', 3); -console.log('output2:', lengthOfLongestSubstring(testInput2), 'expected:', 1); -console.log('output3:', lengthOfLongestSubstring(testInput3), 'expected:', 3);