-
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 #797 from HerrineKim/main
[HerrineKim] Week 3
- Loading branch information
Showing
3 changed files
with
70 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,30 @@ | ||
// 시간 복잡도 : O(n^2) | ||
// 공간 복잡도 : O(n) | ||
|
||
/** | ||
* @param {number[]} candidates | ||
* @param {number} target | ||
* @return {number[][]} | ||
*/ | ||
|
||
var combinationSum = function(candidates, target) { | ||
const result = []; | ||
|
||
const backtrack = (remaining, combo, start) => { | ||
if (remaining === 0) { | ||
result.push([...combo]); | ||
return; | ||
} | ||
|
||
for (let i = start; i < candidates.length; i++) { | ||
if (candidates[i] <= remaining) { | ||
combo.push(candidates[i]); | ||
backtrack(remaining - candidates[i], combo, i); | ||
combo.pop(); | ||
} | ||
} | ||
}; | ||
|
||
backtrack(target, [], 0); | ||
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,18 @@ | ||
// 시간 복잡도 : O(n) | ||
// 공간 복잡도 : O(1) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var maxSubArray = function(nums) { | ||
let currentSum = nums[0]; | ||
let maxSum = nums[0]; | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
currentSum = Math.max(nums[i], currentSum + nums[i]); | ||
maxSum = Math.max(maxSum, currentSum); | ||
} | ||
|
||
return maxSum; | ||
}; |
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 @@ | ||
// 시간 복잡도 : O(n) | ||
// 공간 복잡도 : O(n) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = function(nums, target) { | ||
let numMap = new Map(); | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
let complement = target - nums[i]; | ||
|
||
if (numMap.has(complement)) { | ||
return [numMap.get(complement), i]; | ||
} | ||
|
||
numMap.set(nums[i], i); | ||
} | ||
}; | ||
|