Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ganu] Week3 #791

Merged
merged 8 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions combination-sum/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum = function (candidates, target) {
const answer = [];
const n = candidates.length;
const combi = (i, sum, arr) => {
for (let j = i; j < n; j++) {
const candidate = candidates[j];
const newSum = sum + candidate;

if (newSum === target) {
answer.push([...arr, candidate]);
continue;
}

if (newSum > target) {
continue;
}

if (newSum < target) {
combi(j, newSum, [...arr, candidate]);
}
}
};

combi(0, 0, []);

return answer;
};
25 changes: 25 additions & 0 deletions maximum-subarray/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time complexity: O(n)
// Space complexity: O(1)

/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function (nums) {
const n = nums.length;
const dp = [0, 0];

let answer = Number.MIN_SAFE_INTEGER;

for (let i = 1; i <= n; i++) {
if (i % 2 !== 0) {
dp[1] = Math.max(dp[0] + nums[i - 1], nums[i - 1]);
answer = Math.max(answer, dp[1]);
} else {
dp[0] = Math.max(dp[1] + nums[i - 1], nums[i - 1]);
answer = Math.max(answer, dp[0]);
}
}

return answer;
};
24 changes: 24 additions & 0 deletions product-of-array-except-self/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Time complexity: O(n)
// Space complexity: O(n)

/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function (nums) {
const n = nums.length;
const fromLeft = Array.from({ length: n + 1 }, () => 1);
const fromRight = Array.from({ length: n + 1 }, () => 1);

for (let i = 1; i <= n; i++) {
fromLeft[i] = fromLeft[i - 1] * nums[i - 1];
}

for (let i = n - 1; i >= 0; i--) {
fromRight[i] = fromRight[i + 1] * nums[i];
}

return nums.map((num, i) => {
return fromLeft[i] * fromRight[i + 1];
});
};
25 changes: 25 additions & 0 deletions reverse-bits/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time complexity: O(1)
// Space complexity: O(1)

/**
* @param {number} n - a positive integer
* @return {number} - a positive integer
*/
var reverseBits = function (n) {
const stack = [];
let current = n;

for (let i = 0; i < 32; i++) {
stack.push(current % 2);
current = Math.floor(current / 2);
}

let answer = 0;

for (let i = 0; i < 32; i++) {
const popped = stack.pop();
answer += popped * 2 ** i;
}

return answer;
};
22 changes: 22 additions & 0 deletions two-sum/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Time complexity: O(n)
// Space complexity: O(n)

/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
const map = new Map();

for (let i = 0; i < nums.length; i++) {
const num = nums[i];
const diff = target - num;

if (map.has(diff)) {
return [i, map.get(diff)];
} else {
map.set(num, i);
}
}
};
Loading