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

[highball] week3 solutions #388

Merged
merged 9 commits into from
Sep 1, 2024
11 changes: 11 additions & 0 deletions climbing-stairs/highball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const climbStairs = function (n) {
const dp = [1, 1];

for (let i = 0; i < n - 1; i++) {
const temp = dp[1];
dp[1] = temp + dp[0];
dp[0] = temp;
}

return dp[1];
};
27 changes: 27 additions & 0 deletions coin-change/highball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const coinChange = function (coins, target) {
const dp = new Array(target + 1).fill(target + 1);

dp[0] = 0;
for (let i = 1; i <= target; i++) {
for (let j = 0; j < coins.length; j++) {
if (i >= coins[j]) {
dp[i] = Math.min(dp[i], 1 + dp[i - coins[j]]);
}
}
}
return dp[target] === target + 1 ? -1 : dp[target];
};

console.log(coinChange([1, 2, 5], 11)); //3
console.log(coinChange([2], 3)); //-1
console.log(coinChange([1], 0)); //0

console.log(coinChange([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 40)); //4

console.log(coinChange([186, 419, 83, 408], 6249)); //20

console.log(
coinChange([411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422], 9864)
); //24

console.log(coinChange([1, 2], 3)); //2
28 changes: 28 additions & 0 deletions combination-sum/highball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const combinationSum = function (candidates, target) {
const result = [];
const path = [];

const dfs = function (candidate, sum) {
if (sum > target) return;

if (sum !== 0) path.push(candidate);

if (sum === target) {
result.push([...path]);
path.pop();
return;
}

for (let i = 0; i < candidates.length; i++) {
highballplz marked this conversation as resolved.
Show resolved Hide resolved
if (candidates[i] >= candidate) dfs(candidates[i], sum + candidates[i]);
}

path.pop();
};

dfs(0, 0);

return result;
};

console.log(combinationSum([2, 3, 5], 8));
56 changes: 56 additions & 0 deletions product-of-array-except-self/highball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* -------------------------------------------------------------------------- */
/* time complexitiy: O(n), space complexitiy: O(n) */
/* -------------------------------------------------------------------------- */

// const productExceptSelf = function (nums) {
// const result = [];
// const n = nums.length;

// if (n < 2) return result;

// const left = [1];
// const right = [1];

// for (let i = 1; i < n; i++) {
// left[i] = left[i - 1] * nums[i - 1];
// }

// for (let i = 1; i < n; i++) {
// right[i] = right[i - 1] * nums[n - i];
// }

// for (let i = 0; i < n; i++) {
// result[i] = left[i] * right[n - 1 - i];
// if (result[i] === 0) result[i] = Math.abs(result[i]); //0이 -0으로 표시되는 이슈 핸들
// }

// return result;
// };

/* -------------------------------------------------------------------------- */
/* time complexitiy: O(n), space complexitiy: O(1) */
/* -------------------------------------------------------------------------- */

const productExceptSelf = function (nums) {
const result = [1];
const n = nums.length;

if (n < 2) return result;

let prefix = 1;
let postfix = 1;

for (let i = 1; i < n; i++) {
prefix *= nums[i - 1];
result[i] = prefix;
if (i === n - 1 && result[i] === 0) result[i] = Math.abs(result[i]); //0이 -0으로 표시되는 이슈 핸들
}

for (let i = n - 2; i >= 0; i--) {
postfix *= nums[i + 1];
result[i] *= postfix;
if (result[i] === 0) result[i] = Math.abs(result[i]); //0이 -0으로 표시되는 이슈 핸들
}

return result;
};
44 changes: 44 additions & 0 deletions two-sum/highball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const twoSum = function (nums, target) {
/* -------------------------------------------------------------------------- */
/* target/2인 element가 2개 있을 경우 early return */
/* -------------------------------------------------------------------------- */
let indices = [];
for (let i = 0; i < nums.length; i++) {
if (nums[i] === target / 2) indices.push(i);
if (indices.length === 2) break;
}

if (indices.length === 2) return indices;

/* -------------------------------------------------------------------------- */
/* absDNums[i] === absDNums[j]인 경우 */
/* -------------------------------------------------------------------------- */

indices = []; //indices 초기화

const dNums = nums.map((num) => num - target / 2); //nums의 각 값을 target/2 값과의 편차들로 변환
const absDNums = dNums.map(Math.abs); //dNumbs의 각 값에 절댓값 취해주기

const uniqueAbsDNums = new Map(); //uniqueAbsDNums은 absDNums의 각 값을 key 값으로 하고 그 key의 부호를 boolean으로 변환한 값을 value로 갖는 map임
let j;
for (let i = 0; i < absDNums.length; i++) {
if (
uniqueAbsDNums.has(absDNums[i]) && //앞에 나온 수들 중 하나와 절댓값이 같은 수 등장 (absDNums[i])
dNums[i] > 0 != uniqueAbsDNums.get(absDNums[i]) //절댓값이 같은 이유가 앞에 나온 수들 중 하나와 아예 같은 값이어서가 아니라 부호만 반대인 수이기 때문이라는 것 체크 (두 수가 target/2로 같았다면 이 조건으로 걸러져 버리기 때문에 앞에서 early return하길 잘했음)
) {
j = i;
break;
}
uniqueAbsDNums.set(absDNums[i], dNums[i] > 0);
}

for (let i = 0; i < absDNums.length; i++) {
// j index 앞에 나오는, absDNums[j]와 같은 절댓값을 가지는 수 찾기(위 for문에서 j를 찾은 방식 덕분에 이 수는 absDNums[j]와 절댓값은 같지만 부호는 반대인 수일 것임)
if (absDNums[i] === absDNums[j]) {
indices.push(i);
indices.push(j);
break;
}
}
return indices;
};