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

[선재] WEEK 03 #391

Merged
merged 6 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
18 changes: 18 additions & 0 deletions climbing-stairs/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @description
* brainstorming:
* Dynamic Programming
*
* time complexity: O(n)
* space complexity: O(n)
*/

var climbStairs = function (n) {
const dp = Array.from({ length: n + 1 }, () => 0);
dp[1] = 1;
dp[2] = 2;

for (let i = 3; i < n + 1; i++) dp[i] = dp[i - 1] + dp[i - 2];

return dp[n];
};
90 changes: 90 additions & 0 deletions coin-change/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* @description
* brainstorming:
* 1. asc sort + division calculate
* 2. bfs + memoization
*
* strategy:
* bfs + memoization
*
* reason:
* Tried with brainstorming 1 but test case is false
Comment on lines +3 to +11
Copy link
Member

Choose a reason for hiding this comment

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

이런 내용 정말 좋은 것 같네요 ㅎㅎ 저도 작성해봐야겠네요

*
* time complexity: O(n^k)
* space complexity: O(n)
*/
class Node {
constructor(val) {
this.value = val;
this.next = null;
}
}

class CustomQueue {
constructor() {
this.front = null;
this.rear = null;
this.size = 0;
}

push(val) {
const node = new Node(val);

if (this.size === 0) {
this.front = node;
this.rear = node;
} else {
this.rear.next = node;
this.rear = node;
}

this.size++;
}

pop() {
if (this.size === 0) return null;
const node = this.front;
this.front = this.front.next;
this.size--;
if (this.size === 0) this.rear = null;

return node.value;
}
}
Comment on lines +23 to +53
Copy link
Contributor

Choose a reason for hiding this comment

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

자바스크립트에 내장 큐 자료구조가 없어서 이렇게 직접 짜셔야하는 거 겠죠? 🥲

Copy link
Contributor Author

Choose a reason for hiding this comment

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

맞아요 ㅠㅠ
자바스크립트는 자료구조가 많이 빈약한 언어라 따로 구현할수 밖에 없더라구요...
그래도 이렇게 구현�하니 자료구조 경험치가 쌓이는거 같아서 좋네요 :)


var coinChange = function (coins, amount) {
const queue = new CustomQueue();
Copy link
Member

Choose a reason for hiding this comment

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

저는 개인적으로 DP를 굉장히 못 풀어서 DP로 풀 수 있는 문제같으면 답안을 꼭 참고하는 편입니다 ㅎㅎ
이 문제는 DP에서 베이직한 문제라고 소개되고 있어서 괜찮으시면 해설 영상 추천드려요 혹시 DP에 익숙하시면 신경안쓰셔도 됩니당 ㅎㅎ

Copy link
Contributor Author

Choose a reason for hiding this comment

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

DP 초보인데 레퍼런스 감사합니다!
다양한 방법으로 생각하면서 풀어봐야겠네요!

const memoSet = new Set();

if (amount === 0) return 0;

for (const coin of coins) {
if (amount === coin) return 1;

queue.push(coin);
memoSet.add(coin);
}

let count = 1;

while (queue.size) {
count++;
let depthSize = queue.size;

while (depthSize--) {
const sum = queue.pop();

for (const coin of coins) {
const nextSum = sum + coin;

if (memoSet.has(nextSum)) continue;
if (amount === nextSum) return count;
if (amount > nextSum) queue.push(nextSum);

memoSet.add(nextSum);
}
}
}

return -1;
};
27 changes: 27 additions & 0 deletions combination-sum/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @description
* brainstorming:
* dfs
*
* time complexity: O(n^k)
* space complexity: O(n)
*/
var combinationSum = function (candidates, target) {
const answer = [];

const dfs = (array, sum, index) => {
if (sum > target) return;
if (sum === target) return answer.push(array);

for (let i = index; i < candidates.length; i++) {
const nextArray = array.concat(candidates[i]);
const nextSum = sum + candidates[i];

dfs(nextArray, nextSum, i);
}
};

candidates.forEach((value, i) => dfs([value], value, i));

return answer;
};
29 changes: 29 additions & 0 deletions product-of-array-except-self/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @description
* brainstorming:
* recursive function
*
* time complexity: O(n)
* space complexity: O(n)
*/
var productExceptSelf = function (nums) {
const answer = Array.from({ length: nums.length }, () => 0);

const search = (left, right, i) => {
if (i === nums.length - 1) {
answer[i] = left * right;
return nums[i];
}

const productLeft = left * nums[i];
const productRight = search(productLeft, right, i + 1);

answer[i] = left * productRight;

return productRight * nums[i];
};

search(1, 1, 0);

return answer;
};
47 changes: 47 additions & 0 deletions two-sum/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @description
* brainstorming:
* 1. O(n^2) brute force
* 2. hash table
*/

/**
* @description brainstorming 1 solve
* time complexity: O(n^2)
* space complexity: O(n)
Copy link
Contributor

Choose a reason for hiding this comment

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

입력 크기에 비례해서 메모리를 쓰는 부분이 없는 것 같은데요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nums 자체가 계산에서 존재해서 공간복잡도가 O(n)라고 착각했네요. 계산중 사용하는 공간은 공간이 2인 배열이기에 공간복잡도는 O(1)이 되겠네요!

*/
var twoSum = function (nums, target) {
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) return [i, j];
}
}
};

/**
* @description brainstorming 2 solve
* time complexity: O(n^2)
* space complexity: O(n)
*/
var twoSum = function (nums, target) {
const map = new Map();

nums.forEach((num, index) => {
if (!map.get(num)) return map.set(num, [index]);

map.set(num, map.get(num).concat(index));
});

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

if (!map.get(rest)) continue;

const indexList = map.get(rest);
for (let j = 0; j < indexList.length; j++) {
if (i === indexList[j]) continue;

return [i, indexList[j]];
}
}
};