-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #826 from HerrineKim/main
[HerrineKim] Week 4
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |