Skip to content

[ganu] Week4 #809

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 10 commits into from
Jan 4, 2025
23 changes: 23 additions & 0 deletions coin-change/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// n: amount m: length of coins
// Time complexity: O(n * m) + O(mlogm)
// Space complexity: O(n)

/**
* @param {number[]} coins
* @param {number} amount
* @return {number}
*/
var coinChange = function (coins, amount) {
const dp = Array.from({ length: amount + 1 }, () => Infinity);
dp[0] = 0;

coins.sort((a, b) => a - b);

for (const coin of coins) {
for (let i = coin; i <= amount; i++) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}

return dp.at(-1) === Infinity ? -1 : dp.at(-1);
};
42 changes: 42 additions & 0 deletions merge-two-sorted-lists/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// m: list1, n: list2
// Time complexity: O(m+n)
// Space complexity: O(m+n)

/**
* 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}
*/
var mergeTwoLists = function (list1, list2) {
const answer = new ListNode();
let current = answer;

while (list1 && list2) {
if (list1.val < list2.val) {
current.next = list1;
list1 = list1.next;
} else {
current.next = list2;
list2 = list2.next;
}

current = current.next;
}

if (list1) {
current.next = list1;
}

if (list2) {
current.next = list2;
}

return answer.next;
};
15 changes: 15 additions & 0 deletions missing-number/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Time complexity: O(n)
// Space complexity: O(1)

/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function (nums) {
const n = nums.length;
const target = (n * (n + 1)) / 2;

const sum = nums.reduce((a, c) => a + c, 0);

return target - sum;
};
40 changes: 40 additions & 0 deletions palindromic-substrings/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time complexity: O(n^2)
// Space complexity: O(n^2)

/**
* @param {string} s
* @return {number}
*/
var countSubstrings = function (s) {
const n = s.length;
const dp = Array.from({ length: n }, () =>
Array.from({ length: n }, () => false)
);
let answer = 0;

for (let end = 0; end < n; end++) {
for (let start = end; start >= 0; start--) {
if (start === end) {
dp[start][end] = true;
answer++;
continue;
}

if (start + 1 === end) {
if (s[start] === s[end]) {
dp[start][end] = true;
answer++;
}
continue;
}

if (s[start] === s[end] && dp[start + 1][end - 1]) {
dp[start][end] = true;
answer++;
continue;
}
}
}

return answer;
};
59 changes: 59 additions & 0 deletions word-search/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// h: height of the board, w: width of the board, n: length of the word
// Time complexity: O(h * w * 4**n)
// Space complexity: O(n)

/**
* @param {character[][]} board
* @param {string} word
* @return {boolean}
*/
var exist = function (board, word) {
const n = word.length;
const h = board.length;
const w = board[0].length;

const dy = [1, 0, -1, 0];
const dx = [0, 1, 0, -1];

let answer = false;

const dfs = (current, index) => {
if (index === n - 1) {
answer = true;
return;
}

const [cy, cx] = current;
const value = board[cy][cx];
board[cy][cx] = "";

for (let i = 0; i < dy.length; i++) {
const ny = cy + dy[i];
const nx = cx + dx[i];
const ni = index + 1;

if (
ny >= 0 &&
ny < h &&
nx >= 0 &&
nx < w &&
board[ny][nx] &&
word[ni] === board[ny][nx]
) {
dfs([ny, nx], ni);
}
}

board[cy][cx] = value;
};

for (let i = 0; i < h; i++) {
for (let j = 0; j < w; j++) {
if (board[i][j] === word[0] && !answer) {
dfs([i, j], 0);
}
}
}

return answer;
};
Loading