Skip to content

Commit ec9da38

Browse files
authored
Merge pull request #391 from Sunjae95/main
[선재] WEEK 03
2 parents a4ee01e + b4f9649 commit ec9da38

File tree

5 files changed

+211
-0
lines changed

5 files changed

+211
-0
lines changed

climbing-stairs/sunjae95.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* Dynamic Programming
5+
*
6+
* time complexity: O(n)
7+
* space complexity: O(n)
8+
*/
9+
10+
var climbStairs = function (n) {
11+
const dp = Array.from({ length: n + 1 }, () => 0);
12+
dp[1] = 1;
13+
dp[2] = 2;
14+
15+
for (let i = 3; i < n + 1; i++) dp[i] = dp[i - 1] + dp[i - 2];
16+
17+
return dp[n];
18+
};

coin-change/sunjae95.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* 1. asc sort + division calculate
5+
* 2. bfs + memoization
6+
*
7+
* strategy:
8+
* bfs + memoization
9+
*
10+
* reason:
11+
* Tried with brainstorming 1 but test case is false
12+
*
13+
* time complexity: O(n^k)
14+
* space complexity: O(n)
15+
*/
16+
class Node {
17+
constructor(val) {
18+
this.value = val;
19+
this.next = null;
20+
}
21+
}
22+
23+
class CustomQueue {
24+
constructor() {
25+
this.front = null;
26+
this.rear = null;
27+
this.size = 0;
28+
}
29+
30+
push(val) {
31+
const node = new Node(val);
32+
33+
if (this.size === 0) {
34+
this.front = node;
35+
this.rear = node;
36+
} else {
37+
this.rear.next = node;
38+
this.rear = node;
39+
}
40+
41+
this.size++;
42+
}
43+
44+
pop() {
45+
if (this.size === 0) return null;
46+
const node = this.front;
47+
this.front = this.front.next;
48+
this.size--;
49+
if (this.size === 0) this.rear = null;
50+
51+
return node.value;
52+
}
53+
}
54+
55+
var coinChange = function (coins, amount) {
56+
const queue = new CustomQueue();
57+
const memoSet = new Set();
58+
59+
if (amount === 0) return 0;
60+
61+
for (const coin of coins) {
62+
if (amount === coin) return 1;
63+
64+
queue.push(coin);
65+
memoSet.add(coin);
66+
}
67+
68+
let count = 1;
69+
70+
while (queue.size) {
71+
count++;
72+
let depthSize = queue.size;
73+
74+
while (depthSize--) {
75+
const sum = queue.pop();
76+
77+
for (const coin of coins) {
78+
const nextSum = sum + coin;
79+
80+
if (memoSet.has(nextSum)) continue;
81+
if (amount === nextSum) return count;
82+
if (amount > nextSum) queue.push(nextSum);
83+
84+
memoSet.add(nextSum);
85+
}
86+
}
87+
}
88+
89+
return -1;
90+
};

combination-sum/sunjae95.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* dfs
5+
*
6+
* time complexity: O(n^k)
7+
* space complexity: O(n)
8+
*/
9+
var combinationSum = function (candidates, target) {
10+
const answer = [];
11+
12+
const dfs = (array, sum, index) => {
13+
if (sum > target) return;
14+
if (sum === target) return answer.push(array);
15+
16+
for (let i = index; i < candidates.length; i++) {
17+
const nextArray = array.concat(candidates[i]);
18+
const nextSum = sum + candidates[i];
19+
20+
dfs(nextArray, nextSum, i);
21+
}
22+
};
23+
24+
candidates.forEach((value, i) => dfs([value], value, i));
25+
26+
return answer;
27+
};
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* recursive function
5+
*
6+
* time complexity: O(n)
7+
* space complexity: O(n)
8+
*/
9+
var productExceptSelf = function (nums) {
10+
const answer = Array.from({ length: nums.length }, () => 0);
11+
12+
const search = (left, right, i) => {
13+
if (i === nums.length - 1) {
14+
answer[i] = left * right;
15+
return nums[i];
16+
}
17+
18+
const productLeft = left * nums[i];
19+
const productRight = search(productLeft, right, i + 1);
20+
21+
answer[i] = left * productRight;
22+
23+
return productRight * nums[i];
24+
};
25+
26+
search(1, 1, 0);
27+
28+
return answer;
29+
};

two-sum/sunjae95.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* 1. O(n^2) brute force
5+
* 2. hash table
6+
*/
7+
8+
/**
9+
* @description brainstorming 1 solve
10+
* time complexity: O(n^2)
11+
* space complexity: O(1)
12+
*/
13+
var twoSum = function (nums, target) {
14+
for (let i = 0; i < nums.length; i++) {
15+
for (let j = i + 1; j < nums.length; j++) {
16+
if (nums[i] + nums[j] === target) return [i, j];
17+
}
18+
}
19+
};
20+
21+
/**
22+
* @description brainstorming 2 solve
23+
* time complexity: O(n^2)
24+
* space complexity: O(n)
25+
*/
26+
var twoSum = function (nums, target) {
27+
const map = new Map();
28+
29+
nums.forEach((num, index) => {
30+
if (!map.get(num)) return map.set(num, [index]);
31+
32+
map.set(num, map.get(num).concat(index));
33+
});
34+
35+
for (let i = 0; i < nums.length; i++) {
36+
const rest = target - nums[i];
37+
38+
if (!map.get(rest)) continue;
39+
40+
const indexList = map.get(rest);
41+
for (let j = 0; j < indexList.length; j++) {
42+
if (i === indexList[j]) continue;
43+
44+
return [i, indexList[j]];
45+
}
46+
}
47+
};

0 commit comments

Comments
 (0)