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
45 changes: 45 additions & 0 deletions 3sum/jdy8739.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
const answer = [];

const sorted = nums.sort((a, b) => a - b);

for (let i=0; i<sorted.length; i++) {
if (i > 0 && sorted[i] === sorted[i - 1]) {
// 위 조건으로 중복 숫자 필터
continue;
}

let l = i + 1;
let h = sorted.length - 1;

while (l < h) {
const sum = sorted[i] + sorted[l] + sorted[h];

if (sum === 0) {
const arr = [sorted[i], sorted[l], sorted[h]];
answer.push(arr);

// 아래 반복문으로 중복 숫자 필터
while (l < h && sorted[l] === sorted[l + 1]) l++;
while (l < h && sorted[h] === sorted[h - 1]) h--;
Comment on lines +26 to +28
Copy link
Member

Choose a reason for hiding this comment

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

중복을 제거하기 위해 set을 사용하지 않고 while로 사용하신 이유가 공간복잡도 때문이실까요 ?


h--;
l++;
}

if (sum > 0) h--;
if (sum < 0) l++;
}
}

return answer;
};

// TC: O(n2)
// SC: O(1)


22 changes: 22 additions & 0 deletions climbing-stairs/jdy8739.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {number} n
* @return {number}
*/
var climbStairs = function(n) {
let first = 1;
let second = 2;

if (n <= 2) {
return n;
}

for (let i=2; i<n; i++) {
let tmp = second;
second = first + second;
first = tmp;
}

return second;
};


49 changes: 49 additions & 0 deletions valid-anagram/jdy8739.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const getDictionary = (s) => {
const arr = s.split('');

const dict = {};

for (let i=0; i<arr.length; i++) {
const key = arr[i];
const value = dict[key];

if (value === undefined) {
dict[key] = 1;
} else {
dict[key] = dict[key] + 1;
}
}

return dict;
}

const checkSameLength = (dictA, dictB) => {
return Object.keys(dictA).length === Object.keys(dictB).length
}

const checkSameDict = (s, t) => {
for (const key in s) {
if (s[key] !== t[key]) {
return false;
}
}

return true;
}

/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) {
const dictA = getDictionary(s);

const dictB = getDictionary(t);

return checkSameLength(dictA, dictB) && checkSameDict(dictA, dictB);
};

// 공간복잡도: 해시 테이블에 모든 문자를 저장하게 되므로 O(n)
// 시간복잡도: 두 개의 문자열을 한 번씩 루프를 돌고 있기 때문에 0(n)

Loading