diff --git a/combination-sum/taewanseoul.ts b/combination-sum/taewanseoul.ts new file mode 100644 index 000000000..73c2ba67b --- /dev/null +++ b/combination-sum/taewanseoul.ts @@ -0,0 +1,37 @@ +/** + * 39. Combination Sum + * Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order. + * The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different. + * The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the given input. + * + * https://leetcode.com/problems/combination-sum/description/ + */ + +// O(n^m) time +// O(n) space +function combinationSum(candidates: number[], target: number): number[][] { + const res: number[][] = []; + dfs(candidates, 0, target, [], res); + return res; +} + +function dfs( + nums: number[], + start: number, + remaining: number, + path: number[], + res: number[][] +) { + if (remaining === 0) { + res.push([...path]); + return; + } + + for (let i = start; i < nums.length; i++) { + const num = nums[i]; + if (remaining - num < 0) continue; + path.push(num); + dfs(nums, i, remaining - num, path, res); + path.pop(); + } +} diff --git a/product-of-array-except-self/taewanseoul.ts b/product-of-array-except-self/taewanseoul.ts new file mode 100644 index 000000000..8aba4f739 --- /dev/null +++ b/product-of-array-except-self/taewanseoul.ts @@ -0,0 +1,28 @@ +/** + * 238. Product of Array Except Self + * Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. + * The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. + * You must write an algorithm that runs in O(n) time and without using the division operation. + * + * https://leetcode.com/problems/product-of-array-except-self/description/ + */ + +// O(n) time +// O(1) space +function productExceptSelf(nums: number[]): number[] { + const result = new Array(nums.length).fill(1); + + let left = 1; + for (let i = 0; i < nums.length - 1; i++) { + left *= nums[i]; + result[i + 1] *= left; + } + + let right = 1; + for (let i = nums.length - 1; i > 0; i--) { + right *= nums[i]; + result[i - 1] *= right; + } + + return result; +} diff --git a/reverse-bits/taewanseoul.ts b/reverse-bits/taewanseoul.ts new file mode 100644 index 000000000..eb454e1d1 --- /dev/null +++ b/reverse-bits/taewanseoul.ts @@ -0,0 +1,26 @@ +/** + * 190. Reverse Bits + * Reverse bits of a given 32 bits unsigned integer. + * + * https://leetcode.com/problems/reverse-bits/description/ + */ + +// O(1) time +// O(1) space +function reverseBits(n: number): number { + const bits: number[] = []; + + while (bits.length < 32) { + bits.push(n & 1); + n = n >> 1; + } + + let result = 0; + let scale = 1; + for (let i = bits.length - 1; i >= 0; i--) { + result += bits[i] * scale; + scale *= 2; + } + + return result; +} diff --git a/two-sum/taewanseoul.ts b/two-sum/taewanseoul.ts new file mode 100644 index 000000000..97674206c --- /dev/null +++ b/two-sum/taewanseoul.ts @@ -0,0 +1,21 @@ +/** + * 1. Two Sum + * Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. + * You may assume that each input would have exactly one solution, and you may not use the same element twice. + * You can return the answer in any order. + * + * https://leetcode.com/problems/two-sum/description/ + */ + +// O(n) time +// O(n) space +function twoSum(nums: number[], target: number): number[] { + const map = new Map(); + + for (let i = 0; i < nums.length; i++) { + if (map.has(target - nums[i])) { + return [i, map.get(target - nums[i])!]; + } + map.set(nums[i], i); + } +}