Skip to content

Commit

Permalink
Merge pull request #759 from Yjason-K/main
Browse files Browse the repository at this point in the history
[gomgom22] Week 2
  • Loading branch information
SamTheKorean authored Dec 22, 2024
2 parents bac89c9 + 4e9b853 commit 1a83e1d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
25 changes: 25 additions & 0 deletions climbing-stairs/Yjason-K.ts
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] λ°˜ν™˜
};

41 changes: 41 additions & 0 deletions valid-anagram/Yjason-K.ts
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;
};

0 comments on commit 1a83e1d

Please sign in to comment.