From ace8fba6650d45a400c9945caa5d1e7c2a65d45a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sun, 11 Aug 2024 12:05:58 +0900 Subject: [PATCH 1/6] Add week 1 solutions: contains-duplicate --- contains-duplicate/gitsunmin.ts | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 contains-duplicate/gitsunmin.ts diff --git a/contains-duplicate/gitsunmin.ts b/contains-duplicate/gitsunmin.ts new file mode 100644 index 000000000..d6cd09b2d --- /dev/null +++ b/contains-duplicate/gitsunmin.ts @@ -0,0 +1,8 @@ +/** + * https://leetcode.com/problems/contains-duplicate/ + * time complexity : O(n) + * space complexity : O(n) + */ +function containsDuplicate(nums: number[]): boolean { + return new Set(nums).size !== nums.length +}; \ No newline at end of file From 2ae9f866b021d492806423e9fef8a1dd349359ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sun, 11 Aug 2024 18:16:08 +0900 Subject: [PATCH 2/6] Add week 1 solutions: number-of-1-bits --- number-of-1-bits/gitsunmin.ts | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 number-of-1-bits/gitsunmin.ts diff --git a/number-of-1-bits/gitsunmin.ts b/number-of-1-bits/gitsunmin.ts new file mode 100644 index 000000000..ed4d5dedd --- /dev/null +++ b/number-of-1-bits/gitsunmin.ts @@ -0,0 +1,8 @@ +/** + * https://leetcode.com/problems/number-of-1-bits/ + * time complexity : O(log n) + * space complexity : O(log n) + */ +function hammingWeight(n: number): number { + return n.toString(2).replaceAll('0', '').length +} \ No newline at end of file From e7c89546ead2f6d1d0cc6f6e8b4d44602352d9b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sun, 11 Aug 2024 18:17:27 +0900 Subject: [PATCH 3/6] Add week 1 solutions: top-k-frequent-elements --- top-k-frequent-elements/gitsunmin.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 top-k-frequent-elements/gitsunmin.ts diff --git a/top-k-frequent-elements/gitsunmin.ts b/top-k-frequent-elements/gitsunmin.ts new file mode 100644 index 000000000..e874bc516 --- /dev/null +++ b/top-k-frequent-elements/gitsunmin.ts @@ -0,0 +1,22 @@ +/** + * https://leetcode.com/problems/top-k-frequent-elements/ + * time complexity : O(n) + * space complexity : O(n) + */ +function topKFrequent(nums: number[], k: number): number[] { + const record = nums.reduce((acc, curr) => { + acc[curr] = (acc[curr] ?? 0) + 1; + return acc; + }, {}); + + const array: Array = new Array(nums.length); + for (const num in record) { + const v = record[num]; + if (!array[v]) { + array[v] = []; + } + array[v].push(Number(num)); + } + + return array.reduce((acc, curr) => [...curr, ...acc], []).slice(0, k); +}; \ No newline at end of file From 029599b505b96fce88860a41bef3747cc3212c07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Tue, 13 Aug 2024 23:55:25 +0900 Subject: [PATCH 4/6] add week 1 solutions: kth-smallest-element-in-a-bst --- kth-smallest-element-in-a-bst/gitsunmin.ts | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/gitsunmin.ts diff --git a/kth-smallest-element-in-a-bst/gitsunmin.ts b/kth-smallest-element-in-a-bst/gitsunmin.ts new file mode 100644 index 000000000..74295a680 --- /dev/null +++ b/kth-smallest-element-in-a-bst/gitsunmin.ts @@ -0,0 +1,73 @@ +/** + * https://leetcode.com/problems/kth-smallest-element-in-a-bst/ + * time complexity : O(n) + * space complexity : O(n) + */ + +/** + * * 문제에서 정의된 타입 + */ +class TreeNode { + val: number + left: TreeNode | null + right: TreeNode | null + constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + this.val = (val === undefined ? 0 : val) + this.left = (left === undefined ? null : left) + this.right = (right === undefined ? null : right) + } +} + +/** + * ! 문제에서의 Input과 실제 정의된, 사용되는 input이 다르기 때문에, 한 번 변환 작업을 거처야함. (실제 제출 시 제외한 함수 입니다.) + */ +const arrayToTreeNode = (arr: Array) => (k: number): [TreeNode | null, number] => { + let root = new TreeNode(arr[0]!); + let queue: (TreeNode | null)[] = [root]; + let i = 1; + + while (i < arr.length) { + let current = queue.shift(); + if (current !== null && current !== undefined) { + if (arr[i] !== null) { + current.left = new TreeNode(arr[i]!); + queue.push(current.left); + } + i++; + if (i < arr.length && arr[i] !== null) { + current.right = new TreeNode(arr[i]!); + queue.push(current.right); + } + i++; + } + } + + return [root, k]; +} + +// const input1 = arrayToTreeNode([3, 1, 4, null, 2]); +// const input2 = arrayToTreeNode([5, 3, 6, 2, 4, null, null, 1]); + +// const output1 = kthSmallest(...input1(1)) +// const output2 = kthSmallest(...input2(3)) + +// console.log('output1:', output1); +// console.log('output2:', output2); + + +function inOrderTraversal(node: TreeNode | null): number[] { + if (node === null) { + return []; + } + + return [ + ...inOrderTraversal(node.left), + node.val, + ...inOrderTraversal(node.right) + ]; +} + +function kthSmallest(root: TreeNode | null, k: number): number { + const result = inOrderTraversal(root); + return result[k - 1] +} \ No newline at end of file From ece065a3072c1b59b9ca78739f4938f625bd2664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Wed, 14 Aug 2024 00:39:27 +0900 Subject: [PATCH 5/6] Add week 1 solutions: palindromic-substrings --- palindromic-substrings/gitsunmin.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 palindromic-substrings/gitsunmin.ts diff --git a/palindromic-substrings/gitsunmin.ts b/palindromic-substrings/gitsunmin.ts new file mode 100644 index 000000000..87bb0d592 --- /dev/null +++ b/palindromic-substrings/gitsunmin.ts @@ -0,0 +1,21 @@ +/** + * https://leetcode.com/problems/palindromic-substrings/ + * time complexity : O(n^2) + * space complexity : O(n) + */ + +const expandAroundCenter = (s: string, left: number, right: number): number => { + if (left < 0 || right >= s.length || s[left] !== s[right]) { + return 0; + } + return 1 + expandAroundCenter(s, left - 1, right + 1); +}; + +function countSubstrings(s: string): number { + + return [...Array(s.length).keys()].reduce((totalCount, i) => { + return totalCount + + expandAroundCenter(s, i, i) + + expandAroundCenter(s, i, i + 1); + }, 0); +}; From cd5e61f8a6cd78f287a363b643e96102ee2044d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Wed, 14 Aug 2024 13:14:13 +0900 Subject: [PATCH 6/6] add line break --- contains-duplicate/gitsunmin.ts | 2 +- kth-smallest-element-in-a-bst/gitsunmin.ts | 2 +- number-of-1-bits/gitsunmin.ts | 2 +- top-k-frequent-elements/gitsunmin.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/contains-duplicate/gitsunmin.ts b/contains-duplicate/gitsunmin.ts index d6cd09b2d..b70d8c66f 100644 --- a/contains-duplicate/gitsunmin.ts +++ b/contains-duplicate/gitsunmin.ts @@ -5,4 +5,4 @@ */ function containsDuplicate(nums: number[]): boolean { return new Set(nums).size !== nums.length -}; \ No newline at end of file +}; diff --git a/kth-smallest-element-in-a-bst/gitsunmin.ts b/kth-smallest-element-in-a-bst/gitsunmin.ts index 74295a680..c38f6f5bf 100644 --- a/kth-smallest-element-in-a-bst/gitsunmin.ts +++ b/kth-smallest-element-in-a-bst/gitsunmin.ts @@ -70,4 +70,4 @@ function inOrderTraversal(node: TreeNode | null): number[] { function kthSmallest(root: TreeNode | null, k: number): number { const result = inOrderTraversal(root); return result[k - 1] -} \ No newline at end of file +} diff --git a/number-of-1-bits/gitsunmin.ts b/number-of-1-bits/gitsunmin.ts index ed4d5dedd..0a32b9c7a 100644 --- a/number-of-1-bits/gitsunmin.ts +++ b/number-of-1-bits/gitsunmin.ts @@ -5,4 +5,4 @@ */ function hammingWeight(n: number): number { return n.toString(2).replaceAll('0', '').length -} \ No newline at end of file +} diff --git a/top-k-frequent-elements/gitsunmin.ts b/top-k-frequent-elements/gitsunmin.ts index e874bc516..f8a0055a9 100644 --- a/top-k-frequent-elements/gitsunmin.ts +++ b/top-k-frequent-elements/gitsunmin.ts @@ -19,4 +19,4 @@ function topKFrequent(nums: number[], k: number): number[] { } return array.reduce((acc, curr) => [...curr, ...acc], []).slice(0, k); -}; \ No newline at end of file +};