From 4df0570373e97d596fd5a9eec6c433a4e6005bda Mon Sep 17 00:00:00 2001 From: choidabom Date: Wed, 2 Apr 2025 15:24:46 +0900 Subject: [PATCH 1/6] feat: contains-duplicate --- contains-duplicate/choidabom.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 contains-duplicate/choidabom.ts diff --git a/contains-duplicate/choidabom.ts b/contains-duplicate/choidabom.ts new file mode 100644 index 000000000..d7dd20863 --- /dev/null +++ b/contains-duplicate/choidabom.ts @@ -0,0 +1,24 @@ +// https://leetcode.com/problems/contains-duplicate/description/ + +// TC: O(n) +// SC: O(n) + +function containsDuplicate(nums: number[]): boolean { + const set = new Set(nums) + return set.size !== nums.length +}; + +function containsDuplicate(nums: number[]): boolean { + const set = new Set() + + for (const num of nums){ + if (set.has(num)){ + return true + } + set.add(num) + } + + return false +} + +console.log(containsDuplicate([1,1,1,3,3,4,3,2,4,2])) \ No newline at end of file From f9b368df543fe5a64bc4876716910c8bc40b416e Mon Sep 17 00:00:00 2001 From: choidabom Date: Wed, 2 Apr 2025 17:19:59 +0900 Subject: [PATCH 2/6] feat: two-sum --- two-sum/choidabom.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 two-sum/choidabom.ts diff --git a/two-sum/choidabom.ts b/two-sum/choidabom.ts new file mode 100644 index 000000000..cba062a44 --- /dev/null +++ b/two-sum/choidabom.ts @@ -0,0 +1,36 @@ +// https://leetcode.com/problems/two-sum/ + +// TC: O(n^2) +// SC: O(1) + +function twoSum(nums: number[], target: number): number[] { + for (let i = 0; i < nums.length; i++) { + for (let j = i + 1; j < nums.length; j++){ + if (nums[i] + nums[j] === target){ + return [i, j] + } + } + } + return [] +}; + +// TC: O(n) +// SC: O(n) + +function twoSum(nums: number[], target: number): number[] { + const map = new Map() + + for (let i = 0; i < nums.length; i++) { + const num = nums[i] + const diff = target - num + + if (map.has(diff)) { + return [i, map.get(diff)] + } else { + map.set(num, i) + } + } + return [] +}; + +console.log(twoSum([2,7,11,15], 9)) \ No newline at end of file From 00d02f221e3fc11666c3e02d04741930258dd8ee Mon Sep 17 00:00:00 2001 From: choidabom Date: Wed, 2 Apr 2025 18:08:54 +0900 Subject: [PATCH 3/6] feat: top-k-frequent-elements --- top-k-frequent-elements/choidabom.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 top-k-frequent-elements/choidabom.ts diff --git a/top-k-frequent-elements/choidabom.ts b/top-k-frequent-elements/choidabom.ts new file mode 100644 index 000000000..7ad3ff7da --- /dev/null +++ b/top-k-frequent-elements/choidabom.ts @@ -0,0 +1,19 @@ +// https://leetcode.com/problems/top-k-frequent-elements/ + +// TC: O(nlogn) +// SC: O(n) + +function topKFrequent(nums: number[], k: number): number[] { + const map = new Map() + + for (const num of nums) { + if (map.has(num)){ + map.set(num, map.get(num) + 1) + } else { + map.set(num, 1) + } + } + + const sortedMap = [...map].sort((a, b)=> b[1]- a[1]) + return sortedMap.splice(0, k).map((item)=> item[0]) +}; From bd8eb041c2a2f5c28f6da6025819448728eb910a Mon Sep 17 00:00:00 2001 From: choidabom Date: Wed, 2 Apr 2025 22:14:23 +0900 Subject: [PATCH 4/6] fix: lint --- contains-duplicate/choidabom.ts | 24 +++++++++--------- top-k-frequent-elements/choidabom.ts | 20 +++++++-------- two-sum/choidabom.ts | 38 ++++++++++++++-------------- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/contains-duplicate/choidabom.ts b/contains-duplicate/choidabom.ts index d7dd20863..04e1238c0 100644 --- a/contains-duplicate/choidabom.ts +++ b/contains-duplicate/choidabom.ts @@ -1,24 +1,24 @@ // https://leetcode.com/problems/contains-duplicate/description/ // TC: O(n) -// SC: O(n) +// SC: O(n) function containsDuplicate(nums: number[]): boolean { - const set = new Set(nums) - return set.size !== nums.length -}; + const set = new Set(nums); + return set.size !== nums.length; +} function containsDuplicate(nums: number[]): boolean { - const set = new Set() + const set = new Set(); - for (const num of nums){ - if (set.has(num)){ - return true - } - set.add(num) + for (const num of nums) { + if (set.has(num)) { + return true; } + set.add(num); + } - return false + return false; } -console.log(containsDuplicate([1,1,1,3,3,4,3,2,4,2])) \ No newline at end of file +console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); diff --git a/top-k-frequent-elements/choidabom.ts b/top-k-frequent-elements/choidabom.ts index 7ad3ff7da..1d2899f28 100644 --- a/top-k-frequent-elements/choidabom.ts +++ b/top-k-frequent-elements/choidabom.ts @@ -4,16 +4,16 @@ // SC: O(n) function topKFrequent(nums: number[], k: number): number[] { - const map = new Map() + const map = new Map(); - for (const num of nums) { - if (map.has(num)){ - map.set(num, map.get(num) + 1) - } else { - map.set(num, 1) - } + for (const num of nums) { + if (map.has(num)) { + map.set(num, map.get(num) + 1); + } else { + map.set(num, 1); } + } - const sortedMap = [...map].sort((a, b)=> b[1]- a[1]) - return sortedMap.splice(0, k).map((item)=> item[0]) -}; + const sortedMap = [...map].sort((a, b) => b[1] - a[1]); + return sortedMap.splice(0, k).map((item) => item[0]); +} diff --git a/two-sum/choidabom.ts b/two-sum/choidabom.ts index cba062a44..29601a308 100644 --- a/two-sum/choidabom.ts +++ b/two-sum/choidabom.ts @@ -4,33 +4,33 @@ // SC: O(1) function twoSum(nums: number[], target: number): number[] { - for (let i = 0; i < nums.length; i++) { - for (let j = i + 1; j < nums.length; j++){ - if (nums[i] + nums[j] === target){ - return [i, j] - } + for (let i = 0; i < nums.length; i++) { + for (let j = i + 1; j < nums.length; j++) { + if (nums[i] + nums[j] === target) { + return [i, j]; } } - return [] -}; + } + return []; +} // TC: O(n) // SC: O(n) function twoSum(nums: number[], target: number): number[] { - const map = new Map() + const map = new Map(); - for (let i = 0; i < nums.length; i++) { - const num = nums[i] - const diff = target - num + for (let i = 0; i < nums.length; i++) { + const num = nums[i]; + const diff = target - num; - if (map.has(diff)) { - return [i, map.get(diff)] - } else { - map.set(num, i) - } + if (map.has(diff)) { + return [i, map.get(diff)]; + } else { + map.set(num, i); } - return [] -}; + } + return []; +} -console.log(twoSum([2,7,11,15], 9)) \ No newline at end of file +console.log(twoSum([2, 7, 11, 15], 9)); From 8414df7f675c439f0992d30c2e0ccf9fb820a9fc Mon Sep 17 00:00:00 2001 From: choidabom Date: Sat, 5 Apr 2025 22:39:48 +0900 Subject: [PATCH 5/6] feat: house-robber --- house-robber/choidabom.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 house-robber/choidabom.ts diff --git a/house-robber/choidabom.ts b/house-robber/choidabom.ts new file mode 100644 index 000000000..a8baf5a7e --- /dev/null +++ b/house-robber/choidabom.ts @@ -0,0 +1,19 @@ +// https://leetcode.com/problems/house-robber/ + +// TC: O(n) +// SC: O(n) + +function rob(nums: number[]): number { + if (nums.length === 1) return nums[0]; + if (nums.length === 2) return Math.max(nums[0], nums[1]); + + const dp: number[] = []; + dp[0] = nums[0]; + dp[1] = Math.max(nums[0], nums[1]); + + for (let i = 2; i < nums.length; i++) { + dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); + } + + return dp[nums.length - 1]; +} From bff73b34ddf3b5ec7944a3fe8d2b8dc0db6aeb42 Mon Sep 17 00:00:00 2001 From: choidabom Date: Sat, 5 Apr 2025 22:48:17 +0900 Subject: [PATCH 6/6] feat: longest-consecutive-sequence --- longest-consecutive-sequence/choidabom.ts | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 longest-consecutive-sequence/choidabom.ts diff --git a/longest-consecutive-sequence/choidabom.ts b/longest-consecutive-sequence/choidabom.ts new file mode 100644 index 000000000..9e3617aed --- /dev/null +++ b/longest-consecutive-sequence/choidabom.ts @@ -0,0 +1,25 @@ +// https://leetcode.com/problems/longest-consecutive-sequence/ + +// TC: O(n) +// SC: O(n) + +function longestConsecutive(nums: number[]): number { + const numSet = new Set(nums); + let maxLen = 0; + + for (const num of numSet) { + if (!numSet.has(num - 1)) { + let currentNum = num; + let length = 1; + + while (numSet.has(currentNum + 1)) { + currentNum += 1; + length += 1; + } + + maxLen = Math.max(maxLen, length); + } + } + + return maxLen; +}