Skip to content

[Jeehay28] Week 4 #812

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

Merged
merged 5 commits into from
Jan 4, 2025
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
31 changes: 31 additions & 0 deletions coin-change/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @param {number[]} coins
* @param {number} amount
* @return {number}
*/

// TC : O(c*a), where c is the number of coins, and a is amount
// SC : O(a) // dp array requires O(a) space

var coinChange = function (coins, amount) {
// dynamic programming approach

// dp[amount] : the minimum number of coins
// as a default, dp[0] = 0, for other amounts, dp[amount] = amount + 1 ()
// [0, amount+1, amount+1, ...]
const dp = [0, ...new Array(amount).fill(amount + 1)];

// start from coin because i - coin >= 0
for (const coin of coins) {
for (let i = coin; i <= amount; i++) {
// dp[i] : not using the current coin
// dp[i - coin] + 1 : using the current coin
dp[i] = Math.min(dp[i - coin] + 1, dp[i]);
}
}

// dp[amount] === amount + 1 : that amount of money cannot be made up by any combination of the coins
return dp[amount] < amount + 1 ? dp[amount] : -1;
};


34 changes: 34 additions & 0 deletions merge-two-sorted-lists/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/

// Time Complexity: O(m + n)
// Space Complexity: O(m + n)

var mergeTwoLists = function(list1, list2) {


if(!(list1 && list2)) {
return list1 || list2;
}

if(list1.val < list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
} else {
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}

};


68 changes: 68 additions & 0 deletions missing-number/Jeehay28.js
Copy link
Member

@dusunax dusunax Jan 4, 2025

Choose a reason for hiding this comment

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

My own approach부분도 잘 봤습니다
문제의 전제가 중복된 숫자가 없는 배열이라
let distinctNums = new Set(nums);으로 작성해도 동작하네요

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @param {number[]} nums
* @return {number}
*/

// *** Guided approach 2: bitwise operations and avoids potential overflow issues with very large sums
// XOR method
// Time complexity: O(n)(two loops: one for numbers 0 to n and one for array elements)
// Space complexity: O(1)

var missingNumber = function (nums) {
Copy link
Contributor

Choose a reason for hiding this comment

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

오.. XOR 풀이도 신기하네요!
이미 복잡도는 최대치이긴 하지만, 아래와 같이 순회하면 순회를 절반으로 줄여서 성능을 거의 두배로 올릴 수 있을것 같은데요?!

for (let i = 0; i < n; i++) {
        xor = xor ^ i ^ nums[i];
    }

// XOR with itself results in 0 : a xor a = 0
// XOR with 0 results in the number itself : a xor 0 = a
// XOR is commutative and associative

const n = nums.length;

let xor = 0;

for (let i = 0; i <= n; i++) {
xor ^= i;
}

for (any of nums) {
xor ^= any;
}

return xor;
};

// *** Guided approach 1: simplicity and clarity
// Gauss' Formula (Sum of First n Numbers): n*(n+1) / 2
// Time complexity: O(n)
// Space complexity: O(1)
// var missingNumber = function (nums) {
// const n = nums.length;
// const expectedSum = (n * (n + 1)) / 2;
// const actualSum = nums.reduce((acc, cur) => acc + cur, 0); // O(n)

// const missingNum = expectedSum - actualSum;

// return missingNum;
// };

// *** My own approach
// Time complexity: O(n^2)
// Space complexity: O(n)
// var missingNumber = function (nums) {

// let distinctNums = new Set([]);

// for (any of nums) {
// if (distinctNums.has(any)) {
// return
// } else {
// distinctNums.add(any)
// }
// }

// const n = distinctNums.size;

// for (let i = 0; i <= n; i++) {
// if (!nums.includes(i)) {
// return i;
// }
// }

// };
37 changes: 37 additions & 0 deletions palindromic-substrings/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @param {string} s
* @return {number}
*/

// TC : O(n^2)
// SC : O(1)

var countSubstrings = function (s) {
// For each character in the string, treat it as the center of a potential palindrome.

// 'Count Palindromic Substrings' helper function
const countPS = (left, right) => {
let cnt = 0;
while (left >= 0 && right < s.length && s[left] === s[right]) {
cnt += 1;
left -= 1;
right += 1;
}
return cnt;
};

let totCnt = 0;

for (let i = 0; i < s.length; i++) {
// left === right : 1 center point, odd-length palindromic
totCnt += countPS(i, i);

// left !== right : 2 center points, even-length palindromic
totCnt += countPS(i, i + 1);
}

return totCnt;
};



67 changes: 67 additions & 0 deletions word-search/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @param {character[][]} board
* @param {string} word
* @return {boolean}
*/

// board(N * M), where N is the number of row and M is the number of columns
// L, the length of the word
// TC : O(N * M * 4^L)

// recursion depth : the length of the word (L)
// each recursion call requires constant space.
// SC : O(L)

var exist = function (board, word) {
let row = board.length;
let col = board[0].length;

const dfs = (r, c, idx) => {
// search done
if (idx === word.length) {
return true;
}

// row and column are out of range
if (r < 0 || r >= row || c < 0 || c >= col) {
return false;
}

if (board[r][c] !== word[idx]) {
return false;
}

// word[idx] === board[r][c]
// continue searching for word[idx + 1] in adjacent cells on the board
const temp = board[r][c];
board[r][c] = "visited";

const arr = [
[1, 0], // Move down
[-1, 0], // Move up
[0, 1], // Move right
[0, -1], // Move left
];
for (const [up, right] of arr) {
if (dfs(r + up, c + right, idx + 1)) {
return true;
}
}

board[r][c] = temp;
return false;
};

for (let i = 0; i < row; i++) {
for (let j = 0; j < col; j++) {
if (dfs(i, j, 0)) {
return true;
}
}
}

return false;
};



Loading