Skip to content

Commit

Permalink
Merge pull request #826 from HerrineKim/main
Browse files Browse the repository at this point in the history
[HerrineKim] Week 4
  • Loading branch information
HerrineKim authored Jan 4, 2025
2 parents 4583f0e + e051a94 commit 41a2c03
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
21 changes: 21 additions & 0 deletions coin-change/HerrineKim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 시간 복잡도: O(n * m)
// 공간 복잡도: O(n)

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

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

return dp[amount] === Infinity ? -1 : dp[amount];
};

17 changes: 17 additions & 0 deletions missing-number/HerrineKim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 시간 복잡도: O(n)
// 공간 복잡도: O(1)

/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function(nums) {
const n = nums.length;
// 0부터 n까지의 합 공식: n * (n + 1) / 2
const expectedSum = (n * (n + 1)) / 2;
// 실제 배열의 합
const actualSum = nums.reduce((sum, num) => sum + num, 0);

return expectedSum - actualSum;
};

25 changes: 25 additions & 0 deletions palindromic-substrings/HerrineKim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 시간 복잡도: O(n^2)
// 공간 복잡도: O(1)

/**
* @param {string} s
* @return {number}
*/
var countSubstrings = function(s) {
let count = 0;

const countPalindrome = (left, right) => {
while (left >= 0 && right < s.length && s[left] === s[right]) {
count++;
left--;
right++;
}
};

for (let i = 0; i < s.length; i++) {
countPalindrome(i, i);
countPalindrome(i, i + 1);
}

return count;
};

0 comments on commit 41a2c03

Please sign in to comment.