From 7ee5ca16d70ba244821b7b8d816524541c443b85 Mon Sep 17 00:00:00 2001 From: JangAyeon Date: Thu, 6 Nov 2025 22:34:01 +0900 Subject: [PATCH 1/7] two sum solution --- two-sum/JangAyeon.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 two-sum/JangAyeon.js diff --git a/two-sum/JangAyeon.js b/two-sum/JangAyeon.js new file mode 100644 index 0000000000..85c7df72e9 --- /dev/null +++ b/two-sum/JangAyeon.js @@ -0,0 +1,29 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ + +// 첫번째 통과 풀이 - 브루트포스 +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]; + } + } + } +}; + +// 두번째 통과 풀이 - 해시맵 + 효율성 고려 +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]; // 이전에 complement가 있었으면 바로 반환 + } + map.set(nums[i], i); + } +}; From 7074bf40d3b56a10a1b2704266206ffe1afa6226 Mon Sep 17 00:00:00 2001 From: JangAyeon Date: Thu, 6 Nov 2025 22:41:53 +0900 Subject: [PATCH 2/7] contains duplicate solution --- contains-duplicate/JangAyeon.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 contains-duplicate/JangAyeon.js diff --git a/contains-duplicate/JangAyeon.js b/contains-duplicate/JangAyeon.js new file mode 100644 index 0000000000..c73d10b362 --- /dev/null +++ b/contains-duplicate/JangAyeon.js @@ -0,0 +1,19 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ + +// 첫번째 제출 +var containsDuplicate = function (nums) { + const counter = new Map(); + for (let e of nums) { + const v = counter.has(e) ? counter.get(e) + 1 : 1; + counter.set(e, v); + } + const answer = [...counter.values()].some((item) => item >= 2); + return answer; +}; +// 두번째 제출 +var containsDuplicate = function (nums) { + return new Set(nums).size !== nums.length; +}; From 85c0c332092d51f2ba322750b53120d44f902741 Mon Sep 17 00:00:00 2001 From: JangAyeon Date: Mon, 10 Nov 2025 07:23:41 +0900 Subject: [PATCH 3/7] top-k-frequent-elements solution --- top-k-frequent-elements/JangAyeon.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 top-k-frequent-elements/JangAyeon.js diff --git a/top-k-frequent-elements/JangAyeon.js b/top-k-frequent-elements/JangAyeon.js new file mode 100644 index 0000000000..c7a5829ab9 --- /dev/null +++ b/top-k-frequent-elements/JangAyeon.js @@ -0,0 +1,18 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var topKFrequent = function (nums, k) { + nums = nums.sort((a, b) => a - b); + const counter = new Map(); + for (let num of nums) { + const value = counter.has(num) ? counter.get(num) + 1 : 1; + counter.set(num, value); + } + const sorted = [...counter.entries()].sort((a, b) => b[1] - a[1]); + const answer = sorted.slice(0, k).map((item) => item[0]); + // console.log(counter, sorted, answer) + + return answer; +}; From 2a7b029a7bec686e951897f311d16737fbf10c4d Mon Sep 17 00:00:00 2001 From: JangAyeon Date: Mon, 10 Nov 2025 07:26:12 +0900 Subject: [PATCH 4/7] longest-consecutive-sequence time out --- longest-consecutive-sequence/JangAyeon.js | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 longest-consecutive-sequence/JangAyeon.js diff --git a/longest-consecutive-sequence/JangAyeon.js b/longest-consecutive-sequence/JangAyeon.js new file mode 100644 index 0000000000..ba9a45c3aa --- /dev/null +++ b/longest-consecutive-sequence/JangAyeon.js @@ -0,0 +1,25 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var longestConsecutive = function (nums) { + if (nums.length == 0) return 0; + let answer = 0; + let numsSet = new Set(nums); + const N = nums.length; + + for (let i = 0; i < N; i++) { + let temp = nums[i]; + let length = 1; + if (!numsSet.has(temp - 1)) { + while (numsSet.has(temp + 1)) { + length += 1; + temp += 1; + } + // console.log(length) + answer = Math.max(length, answer); + } + } + + return answer; +}; From dfe88cfbe1013808c16083717c0bb4b1b5129946 Mon Sep 17 00:00:00 2001 From: JangAyeon Date: Mon, 10 Nov 2025 07:26:44 +0900 Subject: [PATCH 5/7] longest-consecutive-sequence solution --- longest-consecutive-sequence/JangAyeon.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/longest-consecutive-sequence/JangAyeon.js b/longest-consecutive-sequence/JangAyeon.js index ba9a45c3aa..b99e68771a 100644 --- a/longest-consecutive-sequence/JangAyeon.js +++ b/longest-consecutive-sequence/JangAyeon.js @@ -8,10 +8,11 @@ var longestConsecutive = function (nums) { let numsSet = new Set(nums); const N = nums.length; - for (let i = 0; i < N; i++) { - let temp = nums[i]; - let length = 1; - if (!numsSet.has(temp - 1)) { + for (let num of numsSet) { + if (!numsSet.has(num - 1)) { + let temp = num; + let length = 1; + while (numsSet.has(temp + 1)) { length += 1; temp += 1; From 5f89ea1936c0468e1d5cb807621efaeddea2c4eb Mon Sep 17 00:00:00 2001 From: JangAyeon Date: Tue, 11 Nov 2025 07:21:05 +0900 Subject: [PATCH 6/7] house robber time over DFS --- house-robber/JangAyeon.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 house-robber/JangAyeon.js diff --git a/house-robber/JangAyeon.js b/house-robber/JangAyeon.js new file mode 100644 index 0000000000..ccdf086af5 --- /dev/null +++ b/house-robber/JangAyeon.js @@ -0,0 +1,23 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var rob = function (nums) { + const N = nums.length; + let answer = 0; + function dfs(lastVisited, curr, t) { + if (curr == N) { + console.log(t); + answer = Math.max(answer, t); + return; + } + for (let idx = curr; idx < N; idx++) { + if (idx != lastVisited + 1) { + dfs(idx, idx + 1, t + nums[idx]); + } + dfs(lastVisited, idx + 1, t); + } + } + dfs(-2, 0, 0); + return answer; +}; From 5304e3976ad7156e700d6acb7f1d74103ba37eb5 Mon Sep 17 00:00:00 2001 From: JangAyeon Date: Tue, 11 Nov 2025 07:21:50 +0900 Subject: [PATCH 7/7] house robber solution DP --- house-robber/JangAyeon.js | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/house-robber/JangAyeon.js b/house-robber/JangAyeon.js index ccdf086af5..cbe7289623 100644 --- a/house-robber/JangAyeon.js +++ b/house-robber/JangAyeon.js @@ -5,19 +5,11 @@ var rob = function (nums) { const N = nums.length; let answer = 0; - function dfs(lastVisited, curr, t) { - if (curr == N) { - console.log(t); - answer = Math.max(answer, t); - return; - } - for (let idx = curr; idx < N; idx++) { - if (idx != lastVisited + 1) { - dfs(idx, idx + 1, t + nums[idx]); - } - dfs(lastVisited, idx + 1, t); - } + let [rob1, rob2] = [0, 0]; + for (let num of nums) { + let temp = Math.max(num + rob1, rob2); + rob1 = rob2; + rob2 = temp; } - dfs(-2, 0, 0); - return answer; + return rob2; };