From 15e1e1409ae094eb8bce12de2b42cd243050d6e5 Mon Sep 17 00:00:00 2001 From: Paik Date: Fri, 27 Dec 2024 09:15:25 +0900 Subject: [PATCH 1/8] feat: 1. Two Sum --- two-sum/gwbaik9717.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 two-sum/gwbaik9717.js diff --git a/two-sum/gwbaik9717.js b/two-sum/gwbaik9717.js new file mode 100644 index 000000000..d5f3ae7f5 --- /dev/null +++ b/two-sum/gwbaik9717.js @@ -0,0 +1,33 @@ +// Time complexity: O(nlogn) +// Space complexity: O(n) + +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function (nums, target) { + const n = nums.length; + + const mappedNums = nums.map((num, i) => [num, i]); + mappedNums.sort((a, b) => a[0] - b[0]); + + let left = 0; + let right = n - 1; + + while (left < right) { + const sum = mappedNums[left][0] + mappedNums[right][0]; + + if (sum > target) { + right--; + continue; + } + + if (sum < target) { + left++; + continue; + } + + return [mappedNums[left][1], mappedNums[right][1]]; + } +}; From 2530b0f91d4b06eaa2f80b2bda1998e2b461ce8d Mon Sep 17 00:00:00 2001 From: Paik Date: Fri, 27 Dec 2024 10:46:47 +0900 Subject: [PATCH 2/8] refactor: feat: 1. Two Sum --- two-sum/gwbaik9717.js | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/two-sum/gwbaik9717.js b/two-sum/gwbaik9717.js index d5f3ae7f5..c7b1397de 100644 --- a/two-sum/gwbaik9717.js +++ b/two-sum/gwbaik9717.js @@ -1,4 +1,4 @@ -// Time complexity: O(nlogn) +// Time complexity: O(n) // Space complexity: O(n) /** @@ -7,27 +7,16 @@ * @return {number[]} */ var twoSum = function (nums, target) { - const n = nums.length; + const map = new Map(); - const mappedNums = nums.map((num, i) => [num, i]); - mappedNums.sort((a, b) => a[0] - b[0]); + for (let i = 0; i < nums.length; i++) { + const num = nums[i]; + const diff = target - num; - let left = 0; - let right = n - 1; - - while (left < right) { - const sum = mappedNums[left][0] + mappedNums[right][0]; - - if (sum > target) { - right--; - continue; + if (map.has(diff)) { + return [i, map.get(diff)]; + } else { + map.set(num, i); } - - if (sum < target) { - left++; - continue; - } - - return [mappedNums[left][1], mappedNums[right][1]]; } }; From 79f948bd9235605302d2a0bba9e8d09c0c9b0ab1 Mon Sep 17 00:00:00 2001 From: Paik Date: Sat, 28 Dec 2024 10:10:19 +0900 Subject: [PATCH 3/8] feat: 190. Reverse Bits --- reverse-bits/gwbaik9717.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 reverse-bits/gwbaik9717.js diff --git a/reverse-bits/gwbaik9717.js b/reverse-bits/gwbaik9717.js new file mode 100644 index 000000000..723e629df --- /dev/null +++ b/reverse-bits/gwbaik9717.js @@ -0,0 +1,25 @@ +// Time complexity: O(1) +// Space complexity: O(1) + +/** + * @param {number} n - a positive integer + * @return {number} - a positive integer + */ +var reverseBits = function (n) { + const stack = []; + let current = n; + + for (let i = 0; i < 32; i++) { + stack.push(current % 2); + current = Math.floor(current / 2); + } + + let answer = 0; + + for (let i = 0; i < 32; i++) { + const popped = stack.pop(); + answer += popped * 2 ** i; + } + + return answer; +}; From ce97ccebeb8bb3651bf8fe212bddc2aacc30831b Mon Sep 17 00:00:00 2001 From: Paik Date: Sat, 28 Dec 2024 10:40:38 +0900 Subject: [PATCH 4/8] feat: 238. Product of Array Except Self --- product-of-array-except-self/gwbaik9717.js | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 product-of-array-except-self/gwbaik9717.js diff --git a/product-of-array-except-self/gwbaik9717.js b/product-of-array-except-self/gwbaik9717.js new file mode 100644 index 000000000..e779083ef --- /dev/null +++ b/product-of-array-except-self/gwbaik9717.js @@ -0,0 +1,33 @@ +// Time complexity: O(n) +// Space complexity: O(n) + +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function (nums) { + let count0 = 0; + + const totalProduct = nums.reduce((acc, num) => { + if (num === 0) { + count0++; + } + + return acc * num; + }, 1); + + return nums.map((num) => { + if (num === 0) { + // 0이 2 개 이상 존재할 때 + if (count0 > 1) { + return 0; + } + + // 0이 1 개 존재할 때 + // 0을 제외한 나머지 product 계산 (이 과정은 단 한번만 존재하기 때문에 시간 복잡도는 여전히 O(n)) + return nums.filter((num) => num !== 0).reduce((a, c) => a * c, 1); + } + + return totalProduct / num; + }); +}; From 0cd8146c232f10a757b5da395a7164575c80d308 Mon Sep 17 00:00:00 2001 From: Paik Date: Sat, 28 Dec 2024 21:32:26 +0900 Subject: [PATCH 5/8] refactor: feat: 238. Product of Array Except Self --- product-of-array-except-self/gwbaik9717.js | 31 ++++++++-------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/product-of-array-except-self/gwbaik9717.js b/product-of-array-except-self/gwbaik9717.js index e779083ef..bc9a20eea 100644 --- a/product-of-array-except-self/gwbaik9717.js +++ b/product-of-array-except-self/gwbaik9717.js @@ -6,28 +6,19 @@ * @return {number[]} */ var productExceptSelf = function (nums) { - let count0 = 0; + const n = nums.length; + const fromLeft = Array.from({ length: n + 1 }, () => 1); + const fromRight = Array.from({ length: n + 1 }, () => 1); - const totalProduct = nums.reduce((acc, num) => { - if (num === 0) { - count0++; - } + for (let i = 1; i <= n; i++) { + fromLeft[i] = fromLeft[i - 1] * nums[i - 1]; + } - return acc * num; - }, 1); + for (let i = n - 1; i >= 0; i--) { + fromRight[i] = fromRight[i + 1] * nums[i]; + } - return nums.map((num) => { - if (num === 0) { - // 0이 2 개 이상 존재할 때 - if (count0 > 1) { - return 0; - } - - // 0이 1 개 존재할 때 - // 0을 제외한 나머지 product 계산 (이 과정은 단 한번만 존재하기 때문에 시간 복잡도는 여전히 O(n)) - return nums.filter((num) => num !== 0).reduce((a, c) => a * c, 1); - } - - return totalProduct / num; + return nums.map((num, i) => { + return fromLeft[i] * fromRight[i + 1]; }); }; From c787f3368362b3cbdc60c64fce748aa123df58b9 Mon Sep 17 00:00:00 2001 From: Paik Date: Sat, 28 Dec 2024 22:20:19 +0900 Subject: [PATCH 6/8] feat: 39. Combination Sum --- combination-sum/gwbaik9717.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 combination-sum/gwbaik9717.js diff --git a/combination-sum/gwbaik9717.js b/combination-sum/gwbaik9717.js new file mode 100644 index 000000000..e5675acf5 --- /dev/null +++ b/combination-sum/gwbaik9717.js @@ -0,0 +1,32 @@ +/** + * @param {number[]} candidates + * @param {number} target + * @return {number[][]} + */ +var combinationSum = function (candidates, target) { + const answer = []; + const n = candidates.length; + const combi = (i, sum, arr) => { + for (let j = i; j < n; j++) { + const candidate = candidates[j]; + const newSum = sum + candidate; + + if (newSum === target) { + answer.push([...arr, candidate]); + continue; + } + + if (newSum > target) { + continue; + } + + if (newSum < target) { + combi(j, newSum, [...arr, candidate]); + } + } + }; + + combi(0, 0, []); + + return answer; +}; From 778ae3e55e7c09590c1af26966568ad3dea8122d Mon Sep 17 00:00:00 2001 From: Paik Date: Sat, 28 Dec 2024 22:32:46 +0900 Subject: [PATCH 7/8] feat: 53. Maximum Subarray --- maximum-subarray/gwbaik9717.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 maximum-subarray/gwbaik9717.js diff --git a/maximum-subarray/gwbaik9717.js b/maximum-subarray/gwbaik9717.js new file mode 100644 index 000000000..a4aa473a6 --- /dev/null +++ b/maximum-subarray/gwbaik9717.js @@ -0,0 +1,18 @@ +// Time complexity: O(n) +// Space complexity: O(n) + +/** + * @param {number[]} nums + * @return {number} + */ +var maxSubArray = function (nums) { + const n = nums.length; + const dp = Array.from({ length: n + 1 }, () => Number.MIN_SAFE_INTEGER); + dp[0] = 0; + + for (let i = 1; i <= n; i++) { + dp[i] = Math.max(dp[i - 1] + nums[i - 1], nums[i - 1]); + } + + return Math.max(...dp.slice(1)); +}; From b3cd465058cb51da31627ceb41b49dc0c87b8eb7 Mon Sep 17 00:00:00 2001 From: Paik Date: Sat, 28 Dec 2024 22:46:38 +0900 Subject: [PATCH 8/8] refactor: feat: 53. Maximum Subarray --- maximum-subarray/gwbaik9717.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/maximum-subarray/gwbaik9717.js b/maximum-subarray/gwbaik9717.js index a4aa473a6..e54d84a52 100644 --- a/maximum-subarray/gwbaik9717.js +++ b/maximum-subarray/gwbaik9717.js @@ -1,5 +1,5 @@ // Time complexity: O(n) -// Space complexity: O(n) +// Space complexity: O(1) /** * @param {number[]} nums @@ -7,12 +7,19 @@ */ var maxSubArray = function (nums) { const n = nums.length; - const dp = Array.from({ length: n + 1 }, () => Number.MIN_SAFE_INTEGER); - dp[0] = 0; + const dp = [0, 0]; + + let answer = Number.MIN_SAFE_INTEGER; for (let i = 1; i <= n; i++) { - dp[i] = Math.max(dp[i - 1] + nums[i - 1], nums[i - 1]); + if (i % 2 !== 0) { + dp[1] = Math.max(dp[0] + nums[i - 1], nums[i - 1]); + answer = Math.max(answer, dp[1]); + } else { + dp[0] = Math.max(dp[1] + nums[i - 1], nums[i - 1]); + answer = Math.max(answer, dp[0]); + } } - return Math.max(...dp.slice(1)); + return answer; };