From 3ce4c7ce447905a396ae288a51a464f86223fef3 Mon Sep 17 00:00:00 2001 From: HoYeong Kwak Date: Sat, 12 Apr 2025 17:05:45 +0900 Subject: [PATCH 1/8] 3sum solution --- 3sum/hoyeongkwak.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 3sum/hoyeongkwak.ts diff --git a/3sum/hoyeongkwak.ts b/3sum/hoyeongkwak.ts new file mode 100644 index 000000000..da998e207 --- /dev/null +++ b/3sum/hoyeongkwak.ts @@ -0,0 +1,30 @@ +/* +time complexity : O(n^2) +space complexity : O(1) +*/ +function threeSum(nums: number[]): number[][] { + const result: number[][] = [] + const sortedNums = nums.sort((a, b) => a - b) + + for(let i = 0; i < sortedNums.length - 2; i++) { + if (i > 0 && sortedNums[i] === sortedNums[i - 1]) continue + let low = i + 1 + let high = sortedNums.length - 1 + while (low < high) { + const threeSum = sortedNums[i] + sortedNums[low] + sortedNums[high] + if (threeSum < 0) { + low += 1 + } else if (threeSum > 0) { + high -= 1 + } else { + result.push([sortedNums[i], sortedNums[low], sortedNums[high]]) + while (low < high && sortedNums[low] === sortedNums[low + 1]) low++ + while (low < high && sortedNums[high] === sortedNums[high - 1]) high-- + + low += 1 + high -= 1 + } + } + } + return result +}; \ No newline at end of file From 36e83e63fdef8094e2647110a4eb054269fc671a Mon Sep 17 00:00:00 2001 From: hoyeongkwak Date: Sat, 12 Apr 2025 17:08:51 +0900 Subject: [PATCH 2/8] climbing stairs solution --- climbing-stairs/hoyeongkwak.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 climbing-stairs/hoyeongkwak.ts diff --git a/climbing-stairs/hoyeongkwak.ts b/climbing-stairs/hoyeongkwak.ts new file mode 100644 index 000000000..2a36607aa --- /dev/null +++ b/climbing-stairs/hoyeongkwak.ts @@ -0,0 +1,15 @@ +/* +시간복잡도 : O(n) +공간복잡도 : O(1) +*/ +function climbStairs(n: number): number { + if (n < 3) return n + let prev = 1 + let curr = 2 + for (let i = 0; i < n - 2; i++) { + const tempPrev = prev + prev = curr + curr = tempPrev + curr + } + return curr +}; \ No newline at end of file From aae15b9117b9df5d6ece1a3f7847a2700c78378b Mon Sep 17 00:00:00 2001 From: hoyeongkwak Date: Sat, 12 Apr 2025 17:09:49 +0900 Subject: [PATCH 3/8] product of array expect self solution --- product-of-array-except-self/hoyeongkwak.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 product-of-array-except-self/hoyeongkwak.ts diff --git a/product-of-array-except-self/hoyeongkwak.ts b/product-of-array-except-self/hoyeongkwak.ts new file mode 100644 index 000000000..3d057f1e3 --- /dev/null +++ b/product-of-array-except-self/hoyeongkwak.ts @@ -0,0 +1,19 @@ +/* +time complexity : O(n) +space complexity : O(1) +*/ +function productExceptSelf(nums: number[]): number[] { + const results = new Array(nums.length).fill(1) + let before = 1 + let after = 1 + for (let i = 0; i < nums.length - 1; i++) { + before *= nums[i] + results[i + 1] *= before + } + for (let i = nums.length - 1; i > 0; i--) { + after *= nums[i] + results[i - 1] *= after + } + + return results +}; \ No newline at end of file From a9c199684a771b0ab06fbbb24d0fd279892613f1 Mon Sep 17 00:00:00 2001 From: hoyeongkwak Date: Sat, 12 Apr 2025 17:10:17 +0900 Subject: [PATCH 4/8] valid anagram solution --- valid-anagram/hoyeongkwak.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 valid-anagram/hoyeongkwak.ts diff --git a/valid-anagram/hoyeongkwak.ts b/valid-anagram/hoyeongkwak.ts new file mode 100644 index 000000000..0da94c624 --- /dev/null +++ b/valid-anagram/hoyeongkwak.ts @@ -0,0 +1,17 @@ +function isAnagram(s: string, t: string): boolean { + // 시간복잡도 O(nlogn), 공간복잡도 O(n) + // const sSorted = s.split('').sort().join(',') + // const tSorted = t.split('').sort().join(',') + // return sSorted === tSorted + + /* + 시간복잡도 O(n), 공간복잡도 O(1) + */ + if (s.length != t.length) return false + const count = new Array(26).fill(0) + for (let i = 0; i < s.length; i++) { + count[s.charCodeAt(i) - 97]++ + count[t.charCodeAt(i) - 97]-- + } + return count.every(c => c === 0) +}; \ No newline at end of file From 0aee6c48ba06bb371b55ab198484e40b55893501 Mon Sep 17 00:00:00 2001 From: hoyeongkwak Date: Sat, 12 Apr 2025 17:11:14 +0900 Subject: [PATCH 5/8] validate binary search tree solution --- validate-binary-search-tree/hoyeongkwak.ts | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 validate-binary-search-tree/hoyeongkwak.ts diff --git a/validate-binary-search-tree/hoyeongkwak.ts b/validate-binary-search-tree/hoyeongkwak.ts new file mode 100644 index 000000000..66a33bd99 --- /dev/null +++ b/validate-binary-search-tree/hoyeongkwak.ts @@ -0,0 +1,34 @@ +/** + * Definition for a binary tree node. + * 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) + * } + * } + */ +/* +time complexity : O(n) +space complexity : O(n) +*/ +function isValidBST(root: TreeNode | null): boolean { + let prev = -Infinity + let isValid = true + const inOrder = (node: TreeNode | null): void => { + if (!isValid || !node) return + inOrder(node.left) + + if (prev >= node.val) { + isValid = false + return + } + prev = node.val + inOrder(node.right) + } + inOrder(root) + return isValid +}; \ No newline at end of file From 67c9a25fee9219608909d59c2d050c714054ac9b Mon Sep 17 00:00:00 2001 From: hoyeongkwak Date: Sun, 13 Apr 2025 20:40:58 +0900 Subject: [PATCH 6/8] week1 solution --- contains-duplicate/hoyeongkwak.ts | 17 ++++++++++++++ house-robber/hoyeongkwak.ts | 13 +++++++++++ longest-consecutive-sequence/hoyeongkwak.ts | 25 +++++++++++++++++++++ top-k-frequent-elements/hoyeongkwak.ts | 22 ++++++++++++++++++ two-sum/hoyeongkwak.ts | 22 ++++++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 contains-duplicate/hoyeongkwak.ts create mode 100644 house-robber/hoyeongkwak.ts create mode 100644 longest-consecutive-sequence/hoyeongkwak.ts create mode 100644 top-k-frequent-elements/hoyeongkwak.ts create mode 100644 two-sum/hoyeongkwak.ts diff --git a/contains-duplicate/hoyeongkwak.ts b/contains-duplicate/hoyeongkwak.ts new file mode 100644 index 000000000..f32baa194 --- /dev/null +++ b/contains-duplicate/hoyeongkwak.ts @@ -0,0 +1,17 @@ +/* +요구사항 : 숫자 배열에서, 2개 이상 존재하는 숫자가 있는지 확인하여, 존재하면 true, 없으면 false +문제풀이 : uniqueNums라는 빈 Set선언하고, nums을 순회하면서 uniqueNums에 숫자가 존재하지 않으면, 추가해주고, +존재하는 경우에는 2개 이상의 같은 숫자가 존재하기 때문에 순회를 중단하고 true를 반환한다. +시간복잡도 : O(n) +공간복잡도 : O(n) +*/ +function containsDuplicate(nums: number[]): boolean { + const uniqueNums = new Set() + for (const num of nums) { + if (uniqueNums.has(num)) { + return true + } + uniqueNums.add(num) + } + return false +} \ No newline at end of file diff --git a/house-robber/hoyeongkwak.ts b/house-robber/hoyeongkwak.ts new file mode 100644 index 000000000..2f5db85e8 --- /dev/null +++ b/house-robber/hoyeongkwak.ts @@ -0,0 +1,13 @@ +/* +시간복잡도 : O(n) +공간복잡도 : O(n) +*/ +function rob(nums: number[]): number { + const dp: number[] = new Array(nums.length + 1) + dp[0] = 0 + dp[1] = nums[0] + for (let i = 2; i < dp.length; i++) { + dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]) + } + return dp[dp.length - 1] +} \ No newline at end of file diff --git a/longest-consecutive-sequence/hoyeongkwak.ts b/longest-consecutive-sequence/hoyeongkwak.ts new file mode 100644 index 000000000..085c5c067 --- /dev/null +++ b/longest-consecutive-sequence/hoyeongkwak.ts @@ -0,0 +1,25 @@ +/* +시간복잡도 : O(n) +공간복잡도 : O(n) +*/ +function longestConsecutive(nums: number[]): number { + const numSet = new Set() + let maxLen = 0 + nums.forEach((num) => { + numSet.add(num) + }) + + for (const num of numSet) { + if (!numSet.has(num - 1)) { + let continueCnt = 1 + let current = num + + while (numSet.has(current + 1)) { + current++ + continueCnt++ + } + maxLen = Math.max(continueCnt, maxLen) + } + } + return maxLen +}; \ No newline at end of file diff --git a/top-k-frequent-elements/hoyeongkwak.ts b/top-k-frequent-elements/hoyeongkwak.ts new file mode 100644 index 000000000..a414725e5 --- /dev/null +++ b/top-k-frequent-elements/hoyeongkwak.ts @@ -0,0 +1,22 @@ +//////////// +/* +시간복잡도 : O(n + m log m) +공간복잡도 : O(m) +*/ +function topKFrequent(nums: number[], k: number): number[] { + const numMap = new Map() + for(const num of nums) { + if (!numMap.has(num)) { + numMap.set(num, 1) + } else { + const numCnt = numMap.get(num) + numMap.set(num, numCnt + 1) + } + } + const numArray = Array.from(numMap).sort((a, b) => b[1] - a[1]) + const result = [] + for(let idx = 0; idx < k; idx++){ + result.push(numArray[idx][0]) + } + return result +}; \ No newline at end of file diff --git a/two-sum/hoyeongkwak.ts b/two-sum/hoyeongkwak.ts new file mode 100644 index 000000000..5ea9b7328 --- /dev/null +++ b/two-sum/hoyeongkwak.ts @@ -0,0 +1,22 @@ +/* +요구사항 : 숫자 배열에서, 2개의 숫자가 더했을 때. target과 같은 숫자가 되는 값을 찾아서 index를 반환 + 단, 같은 index의 숫자는 한번만 사용되어야 함 +문제풀이 : Map을 선언하여 숫자 배열의 값과 index를 추가 + 배열을 순회하면서, target에서 순회중인 index의 숫자와 뺀 값이 Map에 존재하는지 확인하여 + 조건에 맞을 경우, 2개의 숫자의 index를 반환 +시간복잡도 : O(n) +공간복잡도 : O(n) +*/ +function twoSum(nums: number[], target: number): number[] { + const sumMap = new Map() + nums.forEach((num, idx) => { + sumMap.set(num, idx) + }) + for (let i = 0; i < nums.length; i++) { + const secondNum = target - nums[i] + if (sumMap.has(secondNum) && sumMap.get(secondNum) != i) { + return [i, sumMap.get(secondNum)] + } + } + return [] +}; \ No newline at end of file From b7e5af5e0b0fd2a8aaf38243cfb23801d2ceaf53 Mon Sep 17 00:00:00 2001 From: hoyeongkwak Date: Sun, 13 Apr 2025 20:41:36 +0900 Subject: [PATCH 7/8] Revert "week1 solution" This reverts commit 67c9a25fee9219608909d59c2d050c714054ac9b. --- contains-duplicate/hoyeongkwak.ts | 17 -------------- house-robber/hoyeongkwak.ts | 13 ----------- longest-consecutive-sequence/hoyeongkwak.ts | 25 --------------------- top-k-frequent-elements/hoyeongkwak.ts | 22 ------------------ two-sum/hoyeongkwak.ts | 22 ------------------ 5 files changed, 99 deletions(-) delete mode 100644 contains-duplicate/hoyeongkwak.ts delete mode 100644 house-robber/hoyeongkwak.ts delete mode 100644 longest-consecutive-sequence/hoyeongkwak.ts delete mode 100644 top-k-frequent-elements/hoyeongkwak.ts delete mode 100644 two-sum/hoyeongkwak.ts diff --git a/contains-duplicate/hoyeongkwak.ts b/contains-duplicate/hoyeongkwak.ts deleted file mode 100644 index f32baa194..000000000 --- a/contains-duplicate/hoyeongkwak.ts +++ /dev/null @@ -1,17 +0,0 @@ -/* -요구사항 : 숫자 배열에서, 2개 이상 존재하는 숫자가 있는지 확인하여, 존재하면 true, 없으면 false -문제풀이 : uniqueNums라는 빈 Set선언하고, nums을 순회하면서 uniqueNums에 숫자가 존재하지 않으면, 추가해주고, -존재하는 경우에는 2개 이상의 같은 숫자가 존재하기 때문에 순회를 중단하고 true를 반환한다. -시간복잡도 : O(n) -공간복잡도 : O(n) -*/ -function containsDuplicate(nums: number[]): boolean { - const uniqueNums = new Set() - for (const num of nums) { - if (uniqueNums.has(num)) { - return true - } - uniqueNums.add(num) - } - return false -} \ No newline at end of file diff --git a/house-robber/hoyeongkwak.ts b/house-robber/hoyeongkwak.ts deleted file mode 100644 index 2f5db85e8..000000000 --- a/house-robber/hoyeongkwak.ts +++ /dev/null @@ -1,13 +0,0 @@ -/* -시간복잡도 : O(n) -공간복잡도 : O(n) -*/ -function rob(nums: number[]): number { - const dp: number[] = new Array(nums.length + 1) - dp[0] = 0 - dp[1] = nums[0] - for (let i = 2; i < dp.length; i++) { - dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]) - } - return dp[dp.length - 1] -} \ No newline at end of file diff --git a/longest-consecutive-sequence/hoyeongkwak.ts b/longest-consecutive-sequence/hoyeongkwak.ts deleted file mode 100644 index 085c5c067..000000000 --- a/longest-consecutive-sequence/hoyeongkwak.ts +++ /dev/null @@ -1,25 +0,0 @@ -/* -시간복잡도 : O(n) -공간복잡도 : O(n) -*/ -function longestConsecutive(nums: number[]): number { - const numSet = new Set() - let maxLen = 0 - nums.forEach((num) => { - numSet.add(num) - }) - - for (const num of numSet) { - if (!numSet.has(num - 1)) { - let continueCnt = 1 - let current = num - - while (numSet.has(current + 1)) { - current++ - continueCnt++ - } - maxLen = Math.max(continueCnt, maxLen) - } - } - return maxLen -}; \ No newline at end of file diff --git a/top-k-frequent-elements/hoyeongkwak.ts b/top-k-frequent-elements/hoyeongkwak.ts deleted file mode 100644 index a414725e5..000000000 --- a/top-k-frequent-elements/hoyeongkwak.ts +++ /dev/null @@ -1,22 +0,0 @@ -//////////// -/* -시간복잡도 : O(n + m log m) -공간복잡도 : O(m) -*/ -function topKFrequent(nums: number[], k: number): number[] { - const numMap = new Map() - for(const num of nums) { - if (!numMap.has(num)) { - numMap.set(num, 1) - } else { - const numCnt = numMap.get(num) - numMap.set(num, numCnt + 1) - } - } - const numArray = Array.from(numMap).sort((a, b) => b[1] - a[1]) - const result = [] - for(let idx = 0; idx < k; idx++){ - result.push(numArray[idx][0]) - } - return result -}; \ No newline at end of file diff --git a/two-sum/hoyeongkwak.ts b/two-sum/hoyeongkwak.ts deleted file mode 100644 index 5ea9b7328..000000000 --- a/two-sum/hoyeongkwak.ts +++ /dev/null @@ -1,22 +0,0 @@ -/* -요구사항 : 숫자 배열에서, 2개의 숫자가 더했을 때. target과 같은 숫자가 되는 값을 찾아서 index를 반환 - 단, 같은 index의 숫자는 한번만 사용되어야 함 -문제풀이 : Map을 선언하여 숫자 배열의 값과 index를 추가 - 배열을 순회하면서, target에서 순회중인 index의 숫자와 뺀 값이 Map에 존재하는지 확인하여 - 조건에 맞을 경우, 2개의 숫자의 index를 반환 -시간복잡도 : O(n) -공간복잡도 : O(n) -*/ -function twoSum(nums: number[], target: number): number[] { - const sumMap = new Map() - nums.forEach((num, idx) => { - sumMap.set(num, idx) - }) - for (let i = 0; i < nums.length; i++) { - const secondNum = target - nums[i] - if (sumMap.has(secondNum) && sumMap.get(secondNum) != i) { - return [i, sumMap.get(secondNum)] - } - } - return [] -}; \ No newline at end of file From 5a77dce484d215f00d74cea1ce5e5655036f83ff Mon Sep 17 00:00:00 2001 From: hoyeongkwak Date: Sun, 13 Apr 2025 23:59:24 +0900 Subject: [PATCH 8/8] solution add empty line --- 3sum/hoyeongkwak.ts | 2 +- climbing-stairs/hoyeongkwak.ts | 2 +- product-of-array-except-self/hoyeongkwak.ts | 2 +- valid-anagram/hoyeongkwak.ts | 2 +- validate-binary-search-tree/hoyeongkwak.ts | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/3sum/hoyeongkwak.ts b/3sum/hoyeongkwak.ts index da998e207..7a930e196 100644 --- a/3sum/hoyeongkwak.ts +++ b/3sum/hoyeongkwak.ts @@ -27,4 +27,4 @@ function threeSum(nums: number[]): number[][] { } } return result -}; \ No newline at end of file +} diff --git a/climbing-stairs/hoyeongkwak.ts b/climbing-stairs/hoyeongkwak.ts index 2a36607aa..93a5800cb 100644 --- a/climbing-stairs/hoyeongkwak.ts +++ b/climbing-stairs/hoyeongkwak.ts @@ -12,4 +12,4 @@ function climbStairs(n: number): number { curr = tempPrev + curr } return curr -}; \ No newline at end of file +} diff --git a/product-of-array-except-self/hoyeongkwak.ts b/product-of-array-except-self/hoyeongkwak.ts index 3d057f1e3..b3fd684ef 100644 --- a/product-of-array-except-self/hoyeongkwak.ts +++ b/product-of-array-except-self/hoyeongkwak.ts @@ -16,4 +16,4 @@ function productExceptSelf(nums: number[]): number[] { } return results -}; \ No newline at end of file +} diff --git a/valid-anagram/hoyeongkwak.ts b/valid-anagram/hoyeongkwak.ts index 0da94c624..65cb895de 100644 --- a/valid-anagram/hoyeongkwak.ts +++ b/valid-anagram/hoyeongkwak.ts @@ -14,4 +14,4 @@ function isAnagram(s: string, t: string): boolean { count[t.charCodeAt(i) - 97]-- } return count.every(c => c === 0) -}; \ No newline at end of file +} diff --git a/validate-binary-search-tree/hoyeongkwak.ts b/validate-binary-search-tree/hoyeongkwak.ts index 66a33bd99..ec7770cd6 100644 --- a/validate-binary-search-tree/hoyeongkwak.ts +++ b/validate-binary-search-tree/hoyeongkwak.ts @@ -31,4 +31,4 @@ function isValidBST(root: TreeNode | null): boolean { } inOrder(root) return isValid -}; \ No newline at end of file +}