-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #765 from taewanseoul/main
[Wan] Week 3
- Loading branch information
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<number, number>(); | ||
|
||
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); | ||
} | ||
} |