Skip to content
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
23 changes: 23 additions & 0 deletions climbing-stairs/prgmr99.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number} n
* @return {number}
*/
var climbStairs = function (n) {
const memo = {};

function fibo(num, memo) {
Copy link
Contributor

Choose a reason for hiding this comment

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

클로저 내부에서는 memo에 접근할 수 있으므로 fibo의 파라미터로 memo를 넘길 필요가 없을 것 같습니다!

if (num === 1) return 1;
if (num === 2) return 2;

if (memo[num]) {
return memo[num];
}

const result = fibo(num - 1, memo) + fibo(num - 2, memo);
Copy link
Contributor

Choose a reason for hiding this comment

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

재귀가 아닌 반복문을 이용한다면 call stack과 memo를 사용할 필요 없이 변수 2개만 사용함으로써 공간 복잡도를 O(1)로 최적화 할 수 있을 것 같아요!

memo[num] = result;

return result;
}

return fibo(n, memo);
};
33 changes: 33 additions & 0 deletions valid-anagram/prgmr99.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function (s, t) {
const stringMap = new Map();

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

for (let i = 0; i < s.length; i++) {
const currentValue = stringMap.get(s[i]);

if (currentValue) {
stringMap.set(s[i], currentValue + 1);
} else {
stringMap.set(s[i], 1);
}
}

for (let i = 0; i < t.length; i++) {
const currentValue = t[i];
const currentValueInStringMap = stringMap.get(currentValue);

if (currentValueInStringMap) {
stringMap.set(currentValue, currentValueInStringMap - 1);
} else {
return false;
}
}

return true;
};