-
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 #791 from gwbaik9717/main
- Loading branch information
Showing
5 changed files
with
128 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,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; | ||
}; |
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,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; | ||
}; |
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,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]; | ||
}); | ||
}; |
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,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; | ||
}; |
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,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); | ||
} | ||
} | ||
}; |