From 818e279d6b03c5c242448dda6959f4ba371ec906 Mon Sep 17 00:00:00 2001 From: tolluset Date: Mon, 26 Aug 2024 04:52:28 +0900 Subject: [PATCH 1/6] solve: two sum --- two-sum/tolluset.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 two-sum/tolluset.ts diff --git a/two-sum/tolluset.ts b/two-sum/tolluset.ts new file mode 100644 index 000000000..586cab0a8 --- /dev/null +++ b/two-sum/tolluset.ts @@ -0,0 +1,21 @@ +/* + * TC: O(n) + * SC: O(n) + * */ +function twoSum(nums: number[], target: number): number[] { + const n = nums.length; + const answer = new Map(); + + for (let i = 0; i < n; i++) { + const diff = target - nums[i]; + const before = answer.get(diff); + + if (before) { + return [before, i]; + } + + answer.set(nums[i], i); + } + + return []; +} From 3bfe29bebe00dd3032a2d062e53cf6417daef084 Mon Sep 17 00:00:00 2001 From: tolluset Date: Thu, 29 Aug 2024 01:10:45 +0900 Subject: [PATCH 2/6] solve: climbing stairs --- climbing-stairs/tolluset.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 climbing-stairs/tolluset.ts diff --git a/climbing-stairs/tolluset.ts b/climbing-stairs/tolluset.ts new file mode 100644 index 000000000..e84df2f82 --- /dev/null +++ b/climbing-stairs/tolluset.ts @@ -0,0 +1,16 @@ +/** + * TC: O(n) + * SC: O(n) + * */ +function climbStairs(n: number): number { + const dp = new Array(n + 1).fill(0); + + dp[1] = 1; + dp[2] = 2; + + for (let i = 3; i <= n; i++) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + + return dp[n]; +} From d9fd1565d6eb09ed33edfc83199f3f603a4b8247 Mon Sep 17 00:00:00 2001 From: tolluset Date: Thu, 29 Aug 2024 01:40:00 +0900 Subject: [PATCH 3/6] solve: product of array except self --- product-of-array-except-self/tolluset.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 product-of-array-except-self/tolluset.ts diff --git a/product-of-array-except-self/tolluset.ts b/product-of-array-except-self/tolluset.ts new file mode 100644 index 000000000..aa7f2dba9 --- /dev/null +++ b/product-of-array-except-self/tolluset.ts @@ -0,0 +1,23 @@ +/* + * TC: O(n) + * SC: O(n) + * */ + +function productExceptSelf(nums: number[]): number[] { + const n = nums.length; + const answer = new Array(n).fill(1); + + let left = 1; + nums.forEach((num, i) => { + answer[i] = left; + left *= num; + }); + + let right = 1; + nums.reverse().forEach((num, i) => { + answer[n - 1 - i] *= right; + right *= num; + }); + + return answer; +} From aba1f66aec578b018869383d224444e6d6f98ba0 Mon Sep 17 00:00:00 2001 From: tolluset Date: Thu, 29 Aug 2024 02:18:59 +0900 Subject: [PATCH 4/6] solve: coin change --- coin-change/tolluset.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 coin-change/tolluset.ts diff --git a/coin-change/tolluset.ts b/coin-change/tolluset.ts new file mode 100644 index 000000000..fc139ad21 --- /dev/null +++ b/coin-change/tolluset.ts @@ -0,0 +1,22 @@ +/* + * TC: O(amount * coins.length) + * SC: O(amount) + * */ +function coinChange(coins: number[], amount: number): number { + if (amount === 0) { + return 0; + } + + const dp = new Array(amount + 1).fill(Infinity); + dp[0] = 0; + + for (let i = 1; i <= amount; i++) { + coins.forEach((coin) => { + if (coin <= i) { + dp[i] = Math.min(dp[i], dp[i - coin] + 1); + } + }); + } + + return dp[amount] === Infinity ? -1 : dp[amount]; +} From c60f537d15e3c4faf89fadd6dc328a68f0ea4999 Mon Sep 17 00:00:00 2001 From: tolluset Date: Thu, 29 Aug 2024 02:57:03 +0900 Subject: [PATCH 5/6] solve: combination sum --- combination-sum/tolluset.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 combination-sum/tolluset.ts diff --git a/combination-sum/tolluset.ts b/combination-sum/tolluset.ts new file mode 100644 index 000000000..9aa51a5a5 --- /dev/null +++ b/combination-sum/tolluset.ts @@ -0,0 +1,24 @@ +/** + * TC: O(candidates.length ^ target / min(candidates)) + * SC: O(target / min(candidates) + */ +function combinationSum(candidates: number[], target: number): number[][] { + const result: number[][] = []; + const dfs = (start: number, spot: number, path: number[]) => { + if (spot === 0) { + result.push([...path]); + return; + } + + for (let i = start; i < candidates.length; i++) { + if (candidates[i] <= spot) { + path.push(candidates[i]); + dfs(i, spot - candidates[i], path); + path.pop(); + } + } + }; + dfs(0, target, []); + + return result; +} From 43f4a6d8fa3757827056644e4980bf8d301b1cbe Mon Sep 17 00:00:00 2001 From: tolluset Date: Thu, 29 Aug 2024 09:39:02 +0900 Subject: [PATCH 6/6] chore: rename termination flag variable --- combination-sum/tolluset.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/combination-sum/tolluset.ts b/combination-sum/tolluset.ts index 9aa51a5a5..5f91dab67 100644 --- a/combination-sum/tolluset.ts +++ b/combination-sum/tolluset.ts @@ -4,16 +4,16 @@ */ function combinationSum(candidates: number[], target: number): number[][] { const result: number[][] = []; - const dfs = (start: number, spot: number, path: number[]) => { - if (spot === 0) { + const dfs = (start: number, stop: number, path: number[]) => { + if (stop === 0) { result.push([...path]); return; } for (let i = start; i < candidates.length; i++) { - if (candidates[i] <= spot) { + if (candidates[i] <= stop) { path.push(candidates[i]); - dfs(i, spot - candidates[i], path); + dfs(i, stop - candidates[i], path); path.pop(); } }