-
-
Notifications
You must be signed in to change notification settings - Fork 245
[clara-shin] WEEK 02 solutions #1259
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* 계단을 오르는 방법은 몇 가지? | ||
* 총 n개의 계단 | ||
* 한 번에 1계단 또는 2계단을 오를 수 있다 | ||
* 계단 꼭대기에 도달하는 서로 다른 방법의 수를 구해보자 | ||
* | ||
* ✨동적 프로그래밍(Dynamic Programming)✨ | ||
* 재귀 접근법 | ||
* 메모이제이션을 사용한 재귀 | ||
* 동적 프로그래밍(바텀업) | ||
* 피보나치 수열: n번째 계단에 도달하는 방법의 수는 (n-1)번째와 (n-2)번째 계단에 도달하는 방법의 수의 합과 같음 | ||
*/ | ||
|
||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
function climbStairs(n) { | ||
// 예외 처리: n이 1 이하인 경우 빨리 1 리턴하고 탈출 | ||
if (n <= 1) { | ||
return 1; | ||
} | ||
|
||
// 피보나치 수열 계산을 위한 변수 | ||
let first = 1; // f(0) = 1 | ||
let second = 1; // f(1) = 1 | ||
let result; | ||
|
||
// 피보나치 수열 계산 | ||
for (let i = 2; i <= n; i++) { | ||
result = first + second; | ||
first = second; | ||
second = result; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
// 시간 복잡도: O(n) - n번의 반복문을 수행 | ||
// 공간 복잡도: O(1) - 상수 개의 변수만 사용 | ||
// 제약조건이 n은 최대 45까지니까 피보나치 접근법이 좋은 선택인 듯 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* 두 문자열 s와 t가 서로 애너그램(anagram)인지 확인하는 문제 | ||
* 애너그램: 글자의 배열 순서는 다르지만 구성하는 문자와 각 문자의 개수가 동일한 문자열 | ||
* | ||
* 접근 방법 몇 가지: | ||
* 1. 정렬 방식: 두 문자열을 알파벳 순으로 정렬한 후 비교하는 방법 | ||
* 2-1. 문자 카운팅 방식 (객체활용): 각 문자열에 등장하는 문자들의 빈도수를 계산하여 비교하는 방법 | ||
* 2-2. 문자 카운팅 방식 (해시맵 객체활용): 문자를 키로, 빈도수를 값으로 하는 해시맵을 만들어 비교하는 방법 | ||
* | ||
* 팔로우업 질문: 입력에 유니 코드 문자가 포함되어 있으면 어떻게 해? 그러한 경우에 솔루션을 어떻게 조정할꺼야? | ||
* JavaScript의 문자열은 기본적으로 UTF-16으로 인코딩되서 유니코드 문자도 처리 가능 | ||
* Map 객체는 어떤 타입의 키도 허용 ➡️ 유니코드 문자를 키로 사용하는 데 문제가 없음 | ||
* (방법 2-1)의 일반 객체도 유니코드 문자를 키로 지원하지만, Map을 사용하는 것이 좀 더 안전 => 해시맵으로 구현 ㄱㄱ | ||
*/ | ||
|
||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {boolean} | ||
*/ | ||
function isAnagram(s, t) { | ||
// 길이가 다르면 애너그램이 될 수 없음 (빠른 리턴) | ||
if (s.length !== t.length) { | ||
return false; | ||
} | ||
Comment on lines
+22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이렇게 빠르게 판단 가능한 경우에 바로 리턴해주는 것 좋은 것 같아요 👏 |
||
|
||
const charMap = new Map(); | ||
|
||
// 첫 번째 문자열의 각 문자 카운트 증가 | ||
for (let char of s) { | ||
charMap.set(char, (charMap.get(char) || 0) + 1); | ||
} | ||
|
||
// 두 번째 문자열의 각 문자 카운트 감소 | ||
for (let char of t) { | ||
const count = charMap.get(char); | ||
|
||
// 해당 문자가 없거나 카운트가 0이면 애너그램이 아님 | ||
if (!count) { | ||
return false; | ||
} | ||
|
||
charMap.set(char, count - 1); | ||
} | ||
|
||
// 모든 카운트가 0인지 확인 | ||
for (let count of charMap.values()) { | ||
if (count !== 0) { | ||
return false; | ||
} | ||
} | ||
Comment on lines
+47
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 동일한 방법이긴 한데 array의 내장 메서드인 |
||
|
||
return true; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 배열을 사용해서 모든 과정에서 나오는 값들을 다 저장했는데 이렇게 변수 2개만 사용하는 방법도 있네요!! 좋은 방법 배워갑니다 👍