From 406ef4cc7579c5c6d13f78f5d6cbd3a278d97c17 Mon Sep 17 00:00:00 2001 From: jinsoo Date: Tue, 17 Mar 2026 23:35:01 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20week=2001=20=ED=92=80=EC=9D=B4=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20(two-sum,=20top-k-frequent-elements,=20lon?= =?UTF-8?q?gest-consecutive-sequence)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- longest-consecutive-sequence/01-binary.js | 19 +++++++++++++++++++ top-k-frequent-elements/01-binary.js | 20 ++++++++++++++++++++ two-sum/01-binary.js | 14 ++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 longest-consecutive-sequence/01-binary.js create mode 100644 top-k-frequent-elements/01-binary.js create mode 100644 two-sum/01-binary.js diff --git a/longest-consecutive-sequence/01-binary.js b/longest-consecutive-sequence/01-binary.js new file mode 100644 index 000000000..cad4f6a69 --- /dev/null +++ b/longest-consecutive-sequence/01-binary.js @@ -0,0 +1,19 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var longestConsecutive = function (nums) { + const unique_arr = Array.from(new Set(nums)); + const temp = unique_arr.sort((a, b) => a - b); + let a = 1; + let max = 0; + for (let i = 0; i < temp.length; i++) { + if (temp[i] + 1 === temp[i + 1]) { + a++; + } else { + max = Math.max(a, max); + a = 1; + } + } + return max; +}; diff --git a/top-k-frequent-elements/01-binary.js b/top-k-frequent-elements/01-binary.js new file mode 100644 index 000000000..e15067559 --- /dev/null +++ b/top-k-frequent-elements/01-binary.js @@ -0,0 +1,20 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var topKFrequent = function (nums, k) { + const map = new Map(); + for (let i = 0; i < nums.length; i++) { + const temp = map.get(nums[i]); + map.set(nums[i], temp !== undefined ? temp + 1 : 1); + } + const sortedMap = new Map([...map.entries()].sort((a, b) => b[1] - a[1])); + + const iter = sortedMap.keys(); + const result = []; + for (let i = 0; i < k; i++) { + result.push(iter.next().value); + } + return result; +}; diff --git a/two-sum/01-binary.js b/two-sum/01-binary.js new file mode 100644 index 000000000..2c66447d4 --- /dev/null +++ b/two-sum/01-binary.js @@ -0,0 +1,14 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function (nums, target) { + for (let i = 0; i < nums.length - 1; i++) { + for (let j = i + 1; j < nums.length; j++) { + if (nums[i] + nums[j] === target) { + return [i, j]; + } + } + } +}; From 2927ba55a54f3ef0ee2fd73bf931a9781960b258 Mon Sep 17 00:00:00 2001 From: jinsoo Date: Sat, 21 Mar 2026 00:15:18 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20week=2002-03=20=ED=92=80=EC=9D=B4?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80=20(climbing-stairs,=20house-robber,=20pro?= =?UTF-8?q?duct-of-array-except-self,=20valid-palindrome,=20number-of-1-bi?= =?UTF-8?q?ts)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- climbing-stairs/01-binary.js | 18 +++++++++++++++++ house-robber/01-binary.js | 19 ++++++++++++++++++ number-of-1-bits/01-binary.js | 13 ++++++++++++ product-of-array-except-self/01-binary.js | 24 +++++++++++++++++++++++ valid-palindrome/01-binary.js | 8 ++++++++ 5 files changed, 82 insertions(+) create mode 100644 climbing-stairs/01-binary.js create mode 100644 house-robber/01-binary.js create mode 100644 number-of-1-bits/01-binary.js create mode 100644 product-of-array-except-self/01-binary.js create mode 100644 valid-palindrome/01-binary.js diff --git a/climbing-stairs/01-binary.js b/climbing-stairs/01-binary.js new file mode 100644 index 000000000..0a159ef6e --- /dev/null +++ b/climbing-stairs/01-binary.js @@ -0,0 +1,18 @@ +/** + * @param {number} n + * @return {number} + */ + +var climbStairs = function (n) { + if (n === 1) return 1; + if (n === 2) return 2; + const arr = [0, 1, 2]; + let i = 3; + while (i <= n) { + const temp = arr[i - 1] + arr[i - 2]; + arr.push(temp); + i++; + } + + return arr[n]; +}; diff --git a/house-robber/01-binary.js b/house-robber/01-binary.js new file mode 100644 index 000000000..f3ab318a3 --- /dev/null +++ b/house-robber/01-binary.js @@ -0,0 +1,19 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var rob = function (nums) { + const memo = new Map(); + return dfs(nums, 0, memo); +}; + +const dfs = (nums, i, memo) => { + if (i >= nums.length) return 0; + if (memo.has(i)) return memo.get(i); + const result = Math.max( + nums[i] + dfs(nums, i + 2, memo), + dfs(nums, i + 1, memo), + ); + memo.set(i, result); + return result; +}; diff --git a/number-of-1-bits/01-binary.js b/number-of-1-bits/01-binary.js new file mode 100644 index 000000000..71e3253d6 --- /dev/null +++ b/number-of-1-bits/01-binary.js @@ -0,0 +1,13 @@ +/** + * @param {number} n + * @return {number} + */ +var hammingWeight = function (n) { + let cnt = 1; + while (n >= 2) { + const r = n % 2; + n = Math.floor(n / 2); + if (r === 1) cnt++; + } + return cnt; +}; diff --git a/product-of-array-except-self/01-binary.js b/product-of-array-except-self/01-binary.js new file mode 100644 index 000000000..7a8d51e48 --- /dev/null +++ b/product-of-array-except-self/01-binary.js @@ -0,0 +1,24 @@ +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function (nums) { + const r = Array.from(nums.length); + const l = Array.from(nums.length); + const a = Array.from(nums.length); + for (let i = 0; i < nums.length; i++) { + const temp = r[i - 1] ?? 1; + const temp2 = nums[i - 1] ?? 1; + r[i] = temp * temp2; + } + for (let i = nums.length - 1; i > -1; i--) { + const temp = l[i + 1] ?? 1; + const temp2 = nums[i + 1] ?? 1; + l[i] = temp * temp2; + } + for (let i = 0; i < nums.length; i++) { + a[i] = l[i] * r[i]; + } + + return a; +}; diff --git a/valid-palindrome/01-binary.js b/valid-palindrome/01-binary.js new file mode 100644 index 000000000..5da7bf1fa --- /dev/null +++ b/valid-palindrome/01-binary.js @@ -0,0 +1,8 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isPalindrome = function (s) { + const result = s.toLowerCase().replace(/[^a-z0-9]/g, ''); + return result === result.split('').reverse().join(''); +};