diff --git a/contains-duplicate/gitsunmin.ts b/contains-duplicate/gitsunmin.ts new file mode 100644 index 000000000..b70d8c66f --- /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 +}; 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..c38f6f5bf --- /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] +} diff --git a/number-of-1-bits/gitsunmin.ts b/number-of-1-bits/gitsunmin.ts new file mode 100644 index 000000000..0a32b9c7a --- /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 +} 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); +}; diff --git a/top-k-frequent-elements/gitsunmin.ts b/top-k-frequent-elements/gitsunmin.ts new file mode 100644 index 000000000..f8a0055a9 --- /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); +};