-
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 #759 from Yjason-K/main
[gomgom22] Week 2
- Loading branch information
Showing
2 changed files
with
66 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,25 @@ | ||
/** | ||
* @description | ||
* λμ νλ‘κ·Έλλ°(Dynamic Programming, DP)μ μ¬μ©νμ¬ κ³λ¨μ μ€λ₯΄λ λ°©λ² μλ₯Ό κ³μ°ν©λλ€. | ||
* - μ νμ: dp[i] = dp[i-1] + dp[i-2] | ||
* - dp[i-1]: μ΄μ κ³λ¨μμ 1λ¨κ³ μ¬λΌμ¨ κ²½μ° | ||
* - dp[i-2]: λ κ³λ¨ μλμμ 2λ¨κ³ μ¬λΌμ¨ κ²½μ° | ||
* - κ³΅κ° μ΅μ νλ₯Ό ν΅ν΄ λ°°μ΄ λμ λ λ³μ(prev1, prev2)λ₯Ό μ¬μ©νμ¬ λ©λͺ¨λ¦¬ μ¬μ©λμ μ€μ λλ€. | ||
* @param {number}n step μ | ||
* @returns {number} κ³λ¨ λλ¬ λ°©λ² μ | ||
*/ | ||
function climbStairs(n: number): number { | ||
if (n <=2) return n; | ||
|
||
let prev2 = 1; // dp[i-2] | ||
let prev1 = 2; // dp[i-1] | ||
|
||
for (let i = 3; i <= n; i++) { | ||
const cur = prev1 + prev2; // dp[i] κ³μ° | ||
prev2 = prev1; // dp[i-2] κ°±μ | ||
prev1 = cur; // dp[i-1] κ°±μ | ||
} | ||
|
||
return prev1; // dp[n] λ°ν | ||
}; | ||
|
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,41 @@ | ||
/** | ||
* λ λ¬Έμμ΄μ λΉκ΅ν΄μ Anagram μ¬λΆ νμΈ | ||
* - μκ° λ³΅μ‘λ: O(n) | ||
* - λ¬Έμμ΄ μνμ λΉκ΅ κ³Όμ μ ν¬ν¨νμ¬ nμ λ¬Έμμ΄μ κΈΈμ΄ | ||
* - κ³΅κ° λ³΅μ‘λ: 0(1) | ||
* - μνλ²³ κ°μκ° 26κ°λ‘ κ³ μ μμ κ³΅κ° | ||
* @param {string} s - λ¬Έμμ΄ s | ||
* @param {string} t - λ¬Έμμ΄ t | ||
* @returns {boolean} - Anagram μ¬λΆ | ||
*/ | ||
function isAnagram(s: string, t: string): boolean { | ||
// λ λ¬Έμ΄μ κΈΈμ΄κ° λ€λ₯Έκ²½μ° false λ°ν | ||
if (s.length !== t.length) { | ||
return false; | ||
} | ||
|
||
// λ¬Έμμ΄ μνλ²³ μ¬μ μ μν κ°μ²΄ μ μΈ | ||
let vocabS = {}; | ||
let vocabT = {}; | ||
|
||
// s λ¬Έμμ΄μ λν μνλ²³ μ¬μ μμ± | ||
for (const char in s) { | ||
vocabS = vocabS[char] ? vocabS[char] + 1 : 1; | ||
}; | ||
|
||
// t λ¬Έμμ΄μ λν μνλ²³ μ¬μ μμ± | ||
for (const char in t) { | ||
vocabT = vocabT[char] ? vocabT[char] + 1 : 1; | ||
} | ||
|
||
|
||
// λ λ¬Έμμ΄ μ¬μ μ λΉκ΅νλ©° count κ° μΌμΉ νμ§ μμ κ²½μ° false λ°ν | ||
for (const char in vocabS) { | ||
if (vocabS[char] !== vocabT[char]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
|