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
19 changes: 19 additions & 0 deletions climbing-stairs/robinyoon-dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param {number} n
* @return {number}
*/
var climbStairs = function(n) {
//(n) = f(n - 1) + f(n - 2)
let tempArray = [];

for(let i = 0; i <= n; i++){
if(i === 0 || i === 1){
tempArray.push(1);
}else{
let tempSum = 0;
tempSum = tempArray[i - 2] + tempArray[i - 1];
Copy link
Contributor

Choose a reason for hiding this comment

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

이렇게 DP 테이블의 가장 최근 n개의 원소만 사용하는 경우에는 O(n) space의 DP 테이블 대신 O(1)space의 변수를 사용해서 공간 복잡도를 한 단계 최적화 할 수 있을 것 같아요~!

tempArray.push(tempSum);
}
}
return tempArray[n];
};
43 changes: 43 additions & 0 deletions product-of-array-except-self/robinyoon-dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function (nums) {

const NUMS_LENGTH = nums.length;
const isZeroArray = []; //boolean
Copy link
Contributor

Choose a reason for hiding this comment

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

O(1) extra space를 사용하라는 follow up 조건을 위해서 isZeroArray 리스트는 제거해도 될 것 같아요~!

for 문 내에서 검사할 때는 nums[i] === 0으로 검사하면 되고, 이미 zeroCount로 전체 0 개수를 알고 있기 때문입니다!

let zeroCount = 0;

let totalProduct = nums.reduce((acc, item) => {
if (item === 0) {
zeroCount++;
isZeroArray.push(true);
return acc;
} else {
isZeroArray.push(false);
return acc * item;
}
}, 1);

// 엣지 케이스 대비 1: nums의 요소 중 0이 두 개 이상인 경우
if (zeroCount >= 2) {
totalProduct = 0;
}

const tempArray = [];

for (let i = 0; i < NUMS_LENGTH; i++) {
if (isZeroArray[i] === true) {
tempArray.push(totalProduct);
} else if (zeroCount >= 1) {
// 엣지 케이스 대비 2: isZeroArray[i]가 false 더라도 nums의 요소 중 zero가 하나라도 있는 경우
// (지금 보니 이 부분은 zeroCount === 1로 했어도 될 것 같네요...)
tempArray.push(0);
} else {
tempArray.push(totalProduct / nums[i]);
Copy link
Contributor

Choose a reason for hiding this comment

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

문제의 without using the division operation 조건이 위배되는 부분인 것 같습니다! prefix sum을 응용해서 풀어보시는 걸 추천드려요~!

}
}

return tempArray;
};

21 changes: 21 additions & 0 deletions valid-anagram/robinyoon-dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function (s, t) {
//1. s와 t를 Array로 만든다.
const sArray = s.split("");
const tArray = t.split("");

//2. sArray와 tArray의 sort를 같게 만든다.
const sortedSArray = sArray.sort();
const sortedTArray = tArray.sort();
Copy link
Contributor

Choose a reason for hiding this comment

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

정렬을 이용해서 풀이하셨네요! 정렬을 하게 되면 시간 복잡도가 O(nlogn)이 되는데요, 카운팅을 이용하면 시간 복잡도를 약간 더 최적화 할 수 있어 이렇게도 한 번 풀어보시는 걸 추천드려요~!


//3. sArray와 tArray가 같은지 판별한다.
const result = JSON.stringify(sortedSArray) === JSON.stringify(sortedTArray);

//4. 같으면 true를, 다르면 false를 반환한다.
return result;

};