From 2c5b297377f9ecfe03260b73ac058bb1eb957a8c Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sun, 25 Aug 2024 16:23:12 +0900 Subject: [PATCH 1/8] solve: two sum --- two-sum/wogha95.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 two-sum/wogha95.js diff --git a/two-sum/wogha95.js b/two-sum/wogha95.js new file mode 100644 index 000000000..88c5b6feb --- /dev/null +++ b/two-sum/wogha95.js @@ -0,0 +1,51 @@ +// 2차: index를 값으로 갖는 Map을 활용하여 한번의 순회로 답안 도출 +// TC: O(N) +// SC: O(N) + +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function (nums, target) { + const valueIndexMap = new Map(); + + for (let index = 0; index < nums.length; index++) { + const value = nums[index]; + const anotherValue = target - value; + + if (valueIndexMap.has(anotherValue)) { + return [index, valueIndexMap.get(anotherValue)]; + } + + valueIndexMap.set(value, index); + } +}; + +// 1차: 정렬 후 투포인터 활용 +// TC: O(N * logN) +// SC: O(N) + +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function (nums, target) { + const sortedNums = nums + .map((value, index) => ({ value, index })) + .sort((a, b) => a.value - b.value); + let left = 0; + let right = sortedNums.length - 1; + + while (left < right) { + const sum = sortedNums[left].value + sortedNums[right].value; + if (sum < target) { + left += 1; + } else if (sum > target) { + right -= 1; + } else { + return [sortedNums[left].index, sortedNums[right].index]; + } + } +}; From ed2b469d3fefcec14e034b1d3a15c26b3a751fa9 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Mon, 26 Aug 2024 13:46:41 +0900 Subject: [PATCH 2/8] solve: climbing stairs --- climbing-stairs/wogha95.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 climbing-stairs/wogha95.js diff --git a/climbing-stairs/wogha95.js b/climbing-stairs/wogha95.js new file mode 100644 index 000000000..99ce97a3c --- /dev/null +++ b/climbing-stairs/wogha95.js @@ -0,0 +1,20 @@ +// TC: O(N) +// SC: O(1) + +/** + * @param {number} n + * @return {number} + */ +var climbStairs = function (n) { + let previousStep = 0; + let currentStep = 1; + + while (n > 0) { + n -= 1; + const nextStep = previousStep + currentStep; + previousStep = currentStep; + currentStep = nextStep; + } + + return currentStep; +}; From d21692fc598ed7c222342298c7389e9107d585c3 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Mon, 26 Aug 2024 13:49:09 +0900 Subject: [PATCH 3/8] solve: climbing stairs --- climbing-stairs/wogha95.js | 1 + 1 file changed, 1 insertion(+) diff --git a/climbing-stairs/wogha95.js b/climbing-stairs/wogha95.js index 99ce97a3c..fb2e9b2da 100644 --- a/climbing-stairs/wogha95.js +++ b/climbing-stairs/wogha95.js @@ -1,3 +1,4 @@ +// DP 활용하였고 메모리 축소를 위해 현재와 직전의 경우의 수만 관리하였습니다. // TC: O(N) // SC: O(1) From b47d55b04d5ed2031e938dcd29ff4b5a75cb2ade Mon Sep 17 00:00:00 2001 From: wogha95 Date: Mon, 26 Aug 2024 14:40:17 +0900 Subject: [PATCH 4/8] solve: product of array except self --- product-of-array-except-self/wogha95.js | 59 +++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 product-of-array-except-self/wogha95.js diff --git a/product-of-array-except-self/wogha95.js b/product-of-array-except-self/wogha95.js new file mode 100644 index 000000000..412afb513 --- /dev/null +++ b/product-of-array-except-self/wogha95.js @@ -0,0 +1,59 @@ +// 좌->우 방향의 누적곱과 우->좌 방향의 누적곱 활용 +// TC: O(N) +// SC: O(N) (답안을 제외하면 O(1)) + +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function (nums) { + const result = new Array(nums.length).fill(1); + + let leftToRight = 1; + for (let index = 1; index < nums.length; index++) { + leftToRight *= nums[index - 1]; + result[index] *= leftToRight; + } + + let rightToLeft = 1; + for (let index = nums.length - 2; index >= 0; index--) { + rightToLeft *= nums[index + 1]; + result[index] *= rightToLeft; + } + + return result; +}; + +// 1차 풀이 +// 0의 갯수가 0개, 1개, 2개인 경우로 나눠서 풀이 +// 0개인 경우, 답안 배열의 원소는 '모든 원소 곱 / 현재 원소' +// 1개인 경우, 0의 위치한 원소만 '0을 제외한 모든 원소 곱' 이고 그 외 '0' +// 2개인 경우, 답안 배열의 모든 원소가 '0' + +// TC: O(N) +// SC: O(N) + +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function (nums) { + const zeroCount = nums.filter((num) => num === 0).length; + if (zeroCount > 1) { + return new Array(nums.length).fill(0); + } + + const multipled = nums.reduce( + (total, current) => (current === 0 ? total : total * current), + 1 + ); + + if (zeroCount === 1) { + const zeroIndex = nums.findIndex((num) => num === 0); + const result = new Array(nums.length).fill(0); + result[zeroIndex] = multipled; + return result; + } + + return nums.map((num) => multipled / num); +}; From 9e90decb9b7541489c17b9f19cc08ddc5328bb3c Mon Sep 17 00:00:00 2001 From: wogha95 Date: Mon, 26 Aug 2024 21:30:24 +0900 Subject: [PATCH 5/8] solve: combination sum --- combination-sum/wogha95.js | 84 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 combination-sum/wogha95.js diff --git a/combination-sum/wogha95.js b/combination-sum/wogha95.js new file mode 100644 index 000000000..2fa4c5ab5 --- /dev/null +++ b/combination-sum/wogha95.js @@ -0,0 +1,84 @@ +// 2차 +// 몫을 활용하여 시간복잡도를 줄이고자 하였으나 generateSubResult에서 추가 시간발생으로 유의미한 결과를 발견하지 못했습니다. +// TC: O(C^2 * T) +// SC: O(T) +// C: candidates.length, T: target + +/** + * @param {number[]} candidates + * @param {number} target + * @return {number[][]} + */ +var combinationSum = function (candidates, target) { + const result = []; + // 'q' means 'quotient'. + const qList = candidates.map((candidate) => Math.floor(target / candidate)); + + dfs([], 0); + + return result; + + function dfs(selectedQList, total) { + if (total > target) { + return; + } + + if (total === target) { + result.push(generateSubResult(selectedQList)); + return; + } + + const currentIndex = selectedQList.length; + for (let q = qList[currentIndex]; q >= 0; q--) { + selectedQList.push(q); + dfs(selectedQList, total + candidates[currentIndex] * q); + selectedQList.pop(); + } + } + + function generateSubResult(selectedQList) { + return selectedQList + .map((q, index) => new Array(q).fill(candidates[index])) + .flat(); + } +}; + +// 1차 +// dfs를 활용하여 각 요소를 추가 -> 재귀 -> 제거로 순회합니다. +// TC: O(C^T) +// SC: O(T) +// C: candidates.length, T: target + +/** + * @param {number[]} candidates + * @param {number} target + * @return {number[][]} + */ +var combinationSum = function (candidates, target) { + const result = []; + + dfs([], 0, 0); + + return result; + + function dfs(subResult, total, currentIndex) { + if (total > target) { + return; + } + + if (total === target) { + result.push([...subResult]); + return; + } + + if (currentIndex === candidates.length) { + return; + } + + const candidate = candidates[currentIndex]; + subResult.push(candidate); + dfs(subResult, total + candidate, currentIndex); + subResult.pop(); + dfs(subResult, total, currentIndex + 1); + } +}; From 6e8760c00f99cb3e95460abe2701376e11bf7cf6 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Wed, 28 Aug 2024 21:58:32 +0900 Subject: [PATCH 6/8] solve: coin change --- coin-change/wogha95.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 coin-change/wogha95.js diff --git a/coin-change/wogha95.js b/coin-change/wogha95.js new file mode 100644 index 000000000..9b83ce948 --- /dev/null +++ b/coin-change/wogha95.js @@ -0,0 +1,23 @@ +// TC: O(C * A) +// SC: O(C) +// C: coins.length, A: amount + +/** + * @param {number[]} coins + * @param {number} amount + * @return {number} + */ +var coinChange = function (coins, amount) { + const dp = new Array(amount + 1).fill(Number.MAX_SAFE_INTEGER); + dp[0] = 0; + + for (let index = 1; index < amount + 1; index++) { + for (const coin of coins) { + if (index - coin >= 0) { + dp[index] = Math.min(dp[index - coin] + 1, dp[index]); + } + } + } + + return dp[amount] === Number.MAX_SAFE_INTEGER ? -1 : dp[amount]; +}; From 5264f19d71741d0e6e0730e7d844ce7f02ddba28 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sat, 31 Aug 2024 21:16:48 +0900 Subject: [PATCH 7/8] solve: combination sum --- combination-sum/wogha95.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/combination-sum/wogha95.js b/combination-sum/wogha95.js index 2fa4c5ab5..c98a9cb99 100644 --- a/combination-sum/wogha95.js +++ b/combination-sum/wogha95.js @@ -45,7 +45,7 @@ var combinationSum = function (candidates, target) { // 1차 // dfs를 활용하여 각 요소를 추가 -> 재귀 -> 제거로 순회합니다. -// TC: O(C^T) +// TC: O(2^C) // SC: O(T) // C: candidates.length, T: target From 7788008dcccf6626f50f9960891447eb282a3e1c Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sun, 1 Sep 2024 12:48:27 +0900 Subject: [PATCH 8/8] solve: combination sum --- combination-sum/wogha95.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/combination-sum/wogha95.js b/combination-sum/wogha95.js index c98a9cb99..6c55212eb 100644 --- a/combination-sum/wogha95.js +++ b/combination-sum/wogha95.js @@ -44,9 +44,9 @@ var combinationSum = function (candidates, target) { }; // 1차 -// dfs를 활용하여 각 요소를 추가 -> 재귀 -> 제거로 순회합니다. -// TC: O(2^C) -// SC: O(T) +// dfs를 활용하여 각 요소를 추가 -> 재귀 -> 제거 -> 재귀로 순회합니다. +// TC: O(C^T) +// SC: O(C) // C: candidates.length, T: target /**