Skip to content

[choidabom] Week 2 #715

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 4 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions climbing-stairs/choidabom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Time Complexity: O(N)
* Space Complexity: O(N)
*
* 접근: 1번째 계단을 오르는 방법은 1가지, 2번째 계단을 오르는 방법은 2가지 (계단은 한 번에 1칸 또는 2칸씩만 오를 수 있다.)
* n 번째 계단에 오르는 방법은 '한 칸 전 계단(n-1)에서 오르는 1칸 올라오는 방법' + '두 칸 전 계단(n-2)에서 2칸 올라오는 방법'
*/

function climbStairs(n: number): number {
let stairs = [1, 2];

for (let i = 2; i < n; i++) {
stairs[i] = stairs[i - 1] + stairs[i - 2];
}

return stairs[n - 1];
}

function climbStairs(n: number): number {
let dp = new Array(n + 1).fill(0);
dp[1] = 1;
dp[2] = 2;

for (let i = 3; i < n + 1; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}

return dp[n];
}

console.log(climbStairs(5));
56 changes: 56 additions & 0 deletions valid-anagram/choidabom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Runtime: 24ms, Memory: 55.95MB
*
* 접근: 문자들끼리 anagram 관계인 경우, 정렬했을 때 같은 값을 가지기에 각 문자열 정렬 후 비교.
*/

function isAnagram(s: string, t: string): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 풀이의 복잡도도 분석해서, 위아래의 풀이의 장단점을 비교해 주신다면 좀더 풍성한 논의가 오고갈 수 있을 것 같아요!

if (s.length !== t.length) {
return false;
}
return sortedString(s) === sortedString(t);
}

function sortedString(s: string): string {
return s.split("").sort().join("");
}

/**
* Runtime: 19ms, Memory: 55.02MB
*
* 접근: 문자들끼리 anagram 관계인 경우, 각 문자들의 count가 동일할 것이기에 Map 자료구조를 이용해서 합/차 계산
* Time Complexity: O(N)
* Space Complexity: O(N)
*/
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) {
return false;
}

let map = new Map<string, number>();
for (const char of s) {
if (map.has(char)) {
map.set(char, map.get(char)! + 1);
} else {
map.set(char, 1);
}
}

for (const char of t) {
if (map.has(char)) {
map.set(char, map.get(char)! - 1);
} else {
return false;
}
}

for (const value of map.values()) {
if (value !== 0) {
return false;
}
}

return true;
}

console.log(isAnagram("anagram", "nagaram"));
Loading