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

[HerrineKim] Week 3 #797

Merged
merged 4 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
30 changes: 30 additions & 0 deletions combination-sum/HerrineKim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 시간 복잡도 : O(n^2)
// 공간 복잡도 : O(n)
Comment on lines +1 to +2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 달레코드 풀이 보고 풀었는데 재귀 함수는 시간복잡도 및 공간복잡도 계산이 다르더라구요! 한번 참고해보시면 좋을 것 같습니다


/**
* @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;
};
18 changes: 18 additions & 0 deletions maximum-subarray/HerrineKim.js
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;
};
22 changes: 22 additions & 0 deletions two-sum/HerrineKim.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nums 전체를 set로 만들어놓고 확인하는게 아니라 compliment가 없는 경우에만 set에 넣고 확인하는 방식으로 하셨군요!
좋은 풀이 배워갑니다~

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 리뷰 감사합니다!!

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);
}
};

Loading