Skip to content

Commit

Permalink
Merge pull request #765 from taewanseoul/main
Browse files Browse the repository at this point in the history
[Wan] Week 3
  • Loading branch information
taewanseoul authored Dec 28, 2024
2 parents 1f1cf6f + 9e5e459 commit 1ab3fe4
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
37 changes: 37 additions & 0 deletions combination-sum/taewanseoul.ts
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();
}
}
28 changes: 28 additions & 0 deletions product-of-array-except-self/taewanseoul.ts
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;
}
26 changes: 26 additions & 0 deletions reverse-bits/taewanseoul.ts
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;
}
21 changes: 21 additions & 0 deletions two-sum/taewanseoul.ts
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);
}
}

0 comments on commit 1ab3fe4

Please sign in to comment.