From f1e8a696e845870e5a03c63545da4e145dcaf039 Mon Sep 17 00:00:00 2001 From: "hyeonchang.shin" Date: Fri, 26 Jun 2026 13:48:58 +0900 Subject: [PATCH 01/11] contains-duplicate solution --- contains-duplicate/togo26.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 contains-duplicate/togo26.js diff --git a/contains-duplicate/togo26.js b/contains-duplicate/togo26.js new file mode 100644 index 0000000000..5d52ca3901 --- /dev/null +++ b/contains-duplicate/togo26.js @@ -0,0 +1,13 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function (nums) { + const numsSet = new Set(nums); + return numsSet.size !== nums.length; +}; + +/* +TC: O(n) -> nums 개수 만큼 +SC: O(n) -> 중복 삭제 후 요소 수 만큼 메모리 사용 +*/ From 8d84daff04235be10de4bca629ce30a0cfe35f1a Mon Sep 17 00:00:00 2001 From: "hyeonchang.shin" Date: Fri, 26 Jun 2026 15:04:04 +0900 Subject: [PATCH 02/11] two-sum solution --- two-sum/togo26.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 two-sum/togo26.js diff --git a/two-sum/togo26.js b/two-sum/togo26.js new file mode 100644 index 0000000000..37b8fd83a4 --- /dev/null +++ b/two-sum/togo26.js @@ -0,0 +1,67 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ + +/* +TC: O(n) +SC: O(n) -> Map 사용 +*/ + +var twoSum = function (nums, target) { + const map = new Map(); + for (let i = 0; i < nums.length; i++) { + const complement = target - nums[i]; + if (map.has(complement)) return [map.get(complement), i]; + map.set(nums[i], i); + } +}; + +/* +With two points +TC: O(nlogn) -> sort 사용 +SC: O(n) -> origin index 배열 생성 +*/ + +var twoSum = function (nums, target) { + const numsWithOriginIndex = nums.map((num, i) => [num, i]); + numsWithOriginIndex.sort(([a], [b]) => a - b); + + let left = 0; + let right = nums.length - 1; + let result; + + while (left < right) { + const [leftValue, leftIndex] = numsWithOriginIndex[left]; + const [rightValue, rightIndex] = numsWithOriginIndex[right]; + const sum = leftValue + rightValue; + + if (sum === target) { + result = [leftIndex, rightIndex]; + break; + } + + if (sum > target) right--; + else left++; + } + + return result; +}; + +/* +Brute force +TC: O(n^2) +SC: O(1) +*/ + +var twoSum = function (nums, target) { + 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 result; +}; From 858598f896b091d41eff6cbfefe511dd47801268 Mon Sep 17 00:00:00 2001 From: "hyeonchang.shin" Date: Fri, 26 Jun 2026 15:33:17 +0900 Subject: [PATCH 03/11] top-k-frequent-elements solution --- top-k-frequent-elements/togo26.js | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 top-k-frequent-elements/togo26.js diff --git a/top-k-frequent-elements/togo26.js b/top-k-frequent-elements/togo26.js new file mode 100644 index 0000000000..3f05031a43 --- /dev/null +++ b/top-k-frequent-elements/togo26.js @@ -0,0 +1,52 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ + +/* +TC: O(nlogn) -> sort +SC: O(n) => numsCount 생성 +*/ +var topKFrequent = function (nums, k) { + const numsCount = nums.reduce((acc, cur) => { + acc[cur] = acc[cur] + 1 || 1; + return acc; + }, {}); + + const frequecyOrder = Object.entries(numsCount) + .sort(([, a], [, b]) => b - a) + .map(([k, v]) => [Number(k), v]); + + const result = []; + + for (let i = 0; i < k; i++) result.push(frequecyOrder[i][0]); + + return result; +}; + +/* +TC: O(n) -> frequency bucket index로 활용 +SC: O(n) +*/ +var topKFrequent = function (nums, k) { + const numsCount = nums.reduce((acc, cur) => { + acc[cur] = acc[cur] + 1 || 1; + return acc; + }, {}); + + const bucket = []; + Object.entries(numsCount).forEach(([num, freq]) => { + if (!bucket[freq]) bucket[freq] = []; + bucket[freq].push(Number(num)); + }); + + const result = []; + for (let i = bucket.length - 1; i >= 0 && result.length < k; i--) { + if (Array.isArray(bucket[i])) { + result.push(...bucket[i]); + } + } + + return result; +}; From de255d3d8465f99df7fc4227e0afb2ec46203303 Mon Sep 17 00:00:00 2001 From: "hyeonchang.shin" Date: Fri, 26 Jun 2026 17:27:17 +0900 Subject: [PATCH 04/11] longest-consecutive-sequence solution --- longest-consecutive-sequence/togo26.js | 63 ++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 longest-consecutive-sequence/togo26.js diff --git a/longest-consecutive-sequence/togo26.js b/longest-consecutive-sequence/togo26.js new file mode 100644 index 0000000000..a3116cb084 --- /dev/null +++ b/longest-consecutive-sequence/togo26.js @@ -0,0 +1,63 @@ +/** + * @param {number[]} nums + * @return {number} + */ + +/* +TC: O(n) -> for의 skip 부분을 while loop 분으로 채워짐 +SC: O(n) => Set 사용 +*/ +var longestConsecutive = function (nums) { + const numsSet = new Set(nums); + let seq = 0; + + for (num of numsSet) { + if (numsSet.has(num - 1)) continue; + + let next = num + 1; + let currentSeq = 1; + + while (numsSet.has(next)) { + currentSeq++; + next++; + } + + seq = Math.max(seq, currentSeq); + } + + return seq; +}; + +/* +memory out +*/ +// var longestConsecutive = function(nums) { +// if (nums.length <= 1) return nums.length; + +// const dedupeNums = new Set(nums); + +// let maxValue = -Infinity; +// let minValue = Infinity; +// dedupeNums.forEach((num) => { +// maxValue = Math.max(maxValue, num); +// minValue = Math.min(minValue, num); +// }); + +// const markers = Array.from({ length: maxValue - minValue + 1 }, () => false); +// let maxSequence = 0; +// let currentSequence = 0; + +// dedupeNums.forEach((num) => { +// markers[num - minValue] = true; +// }); + +// for (let i = 0; i < markers.length; i++) { +// if (markers[i]) currentSequence++; +// else { +// maxSequence = Math.max(maxSequence, currentSequence); +// currentSequence = 0; +// } +// } + +// return Math.max(maxSequence, currentSequence); +// }; From 1f68a573aa4a27ab9227cf1ff11f094bf28a24d3 Mon Sep 17 00:00:00 2001 From: "hyeonchang.shin" Date: Fri, 26 Jun 2026 18:54:54 +0900 Subject: [PATCH 05/11] house-robber solution --- house-robber/togo26.js | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 house-robber/togo26.js diff --git a/house-robber/togo26.js b/house-robber/togo26.js new file mode 100644 index 0000000000..5245260fe0 --- /dev/null +++ b/house-robber/togo26.js @@ -0,0 +1,43 @@ +/** + * @param {number[]} nums + * @return {number} + */ +/* +TC: O(n) +SC: O(n) +*/ +var rob = function (nums) { + const pocket = {}; + + for (let i = 0; i < nums.length; i++) { + if (i === 0) { + pocket[i] = nums[i]; + continue; + } + if (i === 1) { + pocket[i] = Math.max(nums[i - 1], nums[i]); + continue; + } + + pocket[i] = Math.max(pocket[i - 2] + nums[i], pocket[i - 1]); + } + + return Object.values(pocket).reduce((acc, cur) => Math.max(acc, cur), 0); +}; + +/* +TC: O(n) +SC: O(1) -> variable used +*/ +var rob = function (nums) { + let prev2 = 0; + let prev1 = 0; + + for (let i = 0; i < nums.length; i++) { + let temp = prev1; + prev1 = Math.max(prev2 + nums[i], prev1); + prev2 = temp; + } + + return prev1; +}; From 54577e590a8887d092d326c5e09413f1e7f54a7e Mon Sep 17 00:00:00 2001 From: "hyeonchang.shin" Date: Fri, 3 Jul 2026 16:05:49 +0900 Subject: [PATCH 06/11] valid-anagram solution --- valid-anagram/togo26.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 valid-anagram/togo26.js diff --git a/valid-anagram/togo26.js b/valid-anagram/togo26.js new file mode 100644 index 0000000000..ca5a817934 --- /dev/null +++ b/valid-anagram/togo26.js @@ -0,0 +1,25 @@ +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +// TC: O(nlogn) / SC: O(n) +var isAnagram = function (s, t) { + const sortedS = [...s].sort().join(''); + const sortedT = [...t].sort().join(''); + return sortedS === sortedT; +}; + +// TC: O(n) / SC: O(n) +var isAnagram = function (s, t) { + if (s.length !== t.length) return false; + const sCount = [...s].reduce((acc, cur) => { + acc[cur] = acc[cur] + 1 || 1; + return acc; + }, {}); + const tCount = [...t].reduce((acc, cur) => { + acc[cur] = acc[cur] + 1 || 1; + return acc; + }, {}); + return Object.keys(sCount).every(key => sCount[key] === tCount[key]); +}; From 245a1b1cc8f05703debbe1e898dab91aa4baa4b3 Mon Sep 17 00:00:00 2001 From: "hyeonchang.shin" Date: Fri, 3 Jul 2026 17:06:58 +0900 Subject: [PATCH 07/11] climbing-stairs solution --- climbing-stairs/togo26.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 climbing-stairs/togo26.js diff --git a/climbing-stairs/togo26.js b/climbing-stairs/togo26.js new file mode 100644 index 0000000000..37e31e414e --- /dev/null +++ b/climbing-stairs/togo26.js @@ -0,0 +1,28 @@ +/** + * @param {number} n + * @return {number} + */ + +// TC: O(n) / SC: O(n) +var climbStairs = function (n) { + if (n <= 3) return n; + const dp = [1, 2, 3]; + for (let i = 3; i < n; i++) { + dp[i] = dp[i - 2] + dp[i - 1]; + } + + return dp[dp.length - 1]; +}; + +// TC: O(n) / SC: O(1) +var climbStairs = function (n) { + let temp, + pre1 = 1, + pre2 = 1; + for (let i = 1; i < n; i++) { + temp = pre2; + pre2 = pre1 + pre2; + pre1 = temp; + } + return pre2; +}; From b71c9fa95d53cacfff03d21b65410d8e21454d83 Mon Sep 17 00:00:00 2001 From: "hyeonchang.shin" Date: Fri, 3 Jul 2026 18:19:10 +0900 Subject: [PATCH 08/11] product-of-array-except-self solution --- product-of-array-except-self/togo26.js | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 product-of-array-except-self/togo26.js diff --git a/product-of-array-except-self/togo26.js b/product-of-array-except-self/togo26.js new file mode 100644 index 0000000000..4bde2b0240 --- /dev/null +++ b/product-of-array-except-self/togo26.js @@ -0,0 +1,37 @@ +// TC: O(n) / SC: O(n) +var productExceptSelf = function (nums) { + const left = [1]; + const right = [1]; + const result = []; + + for (let i = 1; i < nums.length; i++) { + left.push(left[i - 1] * nums[i - 1]); + } + + for (let i = nums.length - 1; i > 0; i--) { + right.push(right[nums.length - 1 - i] * nums[i]); + } + + for (let i = 0, j = nums.length - 1; i < nums.length; i++, j--) { + result[i] = left[i] * right[j]; + } + + return result; +}; + +// TC: O(n) / SC: O(1) +var productExceptSelf = function (nums) { + const left = new Array(nums.length).fill(1); + + for (let i = 1; i < nums.length; i++) { + left[i] = left[i - 1] * nums[i - 1]; + } + + let rightProduct = 1; + for (let i = nums.length - 1; i >= 0; i--) { + left[i] *= rightProduct; + rightProduct *= nums[i]; + } + + return left; +}; From b7af1f8fdf1feba5f8fe9dc315eb4b6cddbe1056 Mon Sep 17 00:00:00 2001 From: "hyeonchang.shin" Date: Fri, 3 Jul 2026 20:27:33 +0900 Subject: [PATCH 09/11] 3sum solution --- 3sum/togo26.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 3sum/togo26.js diff --git a/3sum/togo26.js b/3sum/togo26.js new file mode 100644 index 0000000000..72cad1034b --- /dev/null +++ b/3sum/togo26.js @@ -0,0 +1,57 @@ +/** + * @param {number[]} nums + * @return {number[][]} + */ +/// TC: TLE / SC: O(n) +// var threeSum = function(nums) { +// const result = []; +// const memo = {}; + +// for (let i = 0; i < nums.length; i++) { +// for (let j = 0; j < nums.length; j++) { +// for (let k = 0; k < nums.length; k++) { +// if (i === k || i === j || j === k) continue; +// const comb = [nums[i], nums[j], nums[k]].sort((a, b) => a - b).join(","); + +// if (memo[comb]) continue; +// if (!memo[comb]) memo[comb] = true; +// const sum = nums[i] + nums[j] + nums[k]; +// if (sum === 0) result.push([nums[i], nums[j], nums[k]]); +// } +// } +// } + +// return result; +// }; + +// TC: O(n^2) / SC: O(1) +var threeSum = function (nums) { + const result = []; + + nums.sort((a, b) => a - b); + + for (let i = 0; i < nums.length; i++) { + if (i > 0 && nums[i] === nums[i - 1]) continue; + if (nums[i] > 0) break; + + let left = i + 1; + let right = nums.length - 1; + + while (left < right) { + const sum = nums[left] + nums[right]; + if (sum === -nums[i]) { + result.push([nums[i], nums[left], nums[right]]); + while (left < right && nums[left] === nums[left + 1]) left++; + while (left < right && nums[right] === nums[right - 1]) right--; + left++; + right--; + } else if (sum < -nums[i]) { + left++; + } else { + right--; + } + } + } + + return result; +}; From 399b3a3c25535f011103373398e2d3637678cb29 Mon Sep 17 00:00:00 2001 From: "hyeonchang.shin" Date: Sat, 4 Jul 2026 00:51:51 +0900 Subject: [PATCH 10/11] validate-binary-search-tree solution --- validate-binary-search-tree/togo26.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 validate-binary-search-tree/togo26.js diff --git a/validate-binary-search-tree/togo26.js b/validate-binary-search-tree/togo26.js new file mode 100644 index 0000000000..30e3de0758 --- /dev/null +++ b/validate-binary-search-tree/togo26.js @@ -0,0 +1,14 @@ +/** + * @param {TreeNode} root + * @return {boolean} + */ +// TC: O(n) / SC: Best O(height) - Worst O(n) +var isValidBST = function (root) { + function traverse(node, min, max) { + if (!node) return true; + if (!(min < node.val && node.val < max)) return false; + return traverse(node.left, min, node.val) && traverse(node.right, node.val, max); + } + + return traverse(root, -Infinity, Infinity); +}; From ff613e812d3e3c3d0ca0b4252797f23bd04d1ea9 Mon Sep 17 00:00:00 2001 From: Hyeonchang Shin Date: Sat, 4 Jul 2026 18:20:20 +0900 Subject: [PATCH 11/11] Apply suggestion from @parkhojeong Co-authored-by: Hojeong Park --- valid-anagram/togo26.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/valid-anagram/togo26.js b/valid-anagram/togo26.js index ca5a817934..18fb5c0b93 100644 --- a/valid-anagram/togo26.js +++ b/valid-anagram/togo26.js @@ -14,11 +14,11 @@ var isAnagram = function (s, t) { var isAnagram = function (s, t) { if (s.length !== t.length) return false; const sCount = [...s].reduce((acc, cur) => { - acc[cur] = acc[cur] + 1 || 1; + acc[cur] = (acc[cur] ?? 0) + 1; return acc; }, {}); const tCount = [...t].reduce((acc, cur) => { - acc[cur] = acc[cur] + 1 || 1; + acc[cur] = (acc[cur] ?? 0) + 1 ; return acc; }, {}); return Object.keys(sCount).every(key => sCount[key] === tCount[key]);