Skip to content

Commit

Permalink
Merge pull request #791 from gwbaik9717/main
Browse files Browse the repository at this point in the history
  • Loading branch information
gwbaik9717 authored Dec 28, 2024
2 parents e09f615 + b3cd465 commit ca1839f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
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);
}
}
};

0 comments on commit ca1839f

Please sign in to comment.