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
86 changes: 86 additions & 0 deletions 3sum/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* @param {number[]} nums
* @return {number[][]}
*/


// Time Complexity: O(n^2)
// Space Complexity: O(n^2)

var threeSum = function (nums) {

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

let result = [];

// Loop through the array and pick each number as the first number for the triplet
for (let i = 0; i < sorted.length - 2; i++) {

// skip duplicate values for sorted[middle]
if(i > 0 && sorted[i - 1] === sorted[i]) {
continue;
}

let left = i + 1; // Left pointer starts right after the current middle
let right = sorted.length - 1; // Right pointer starts at the last element

while (left < right) {
const sum = sorted[i] + sorted[left] + sorted[right];

if (sum === 0) {
result.push([sorted[left], sorted[i], sorted[right]]);

// skip duplicates for sorted[left] and sorted[right]
while(left < right && sorted[left] === sorted[left + 1]){
left += 1; // Move left pointer to the right to skip duplicate values
}

while(left < right && sorted[right] === sorted[right - 1]) {
right -= 1; // Move right pointer to the left to skip duplicate values
}

left += 1;
right -= 1;

} else if (sum > 0) {
right -= 1;

} else {
left += 1
}
}
}

return result;

};


// var threeSum = function (nums) {

// i != j, i != k, and j != k can be interpreted as i < j < k

// three nested loops
// time complexity of O(n³)
// Time Limit Exceeded

// let result = [];

// for (let i = 0; i < nums.length - 2; i++) {
// for (let j = i + 1; j < nums.length - 1; j++) {
// for (let k = j + 1; k < nums.length; k++) {
// if (nums[i] + nums[j] + nums[k] === 0) {
// const str = [nums[i], nums[j], nums[k]].sort((a, b) => a - b).join(",")
// result.push(str)
// }
// }
// }

// }

// result = [... new Set(result)].map(str => str.split(",").map(str => +str))

// return result;

// }

41 changes: 41 additions & 0 deletions climbing-stairs/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @param {number} n
* @return {number}
*/

// Time Complexity: O(n^2)
// First O(n): From the loop that runs up to n times.
// Second O(n): From the factorial calculations in each iteration.
// overall time complexity is O(n^2).

// Space Complexity: O(n)
// the factorial function's recursion has a space complexity of O(n)
// O(1) means constant space complexity. It implies that the amount of memory used does not grow with the size of the input.
// The other variables in the function only use a constant amount of space, resulting in an O(1) space usage.

var climbStairs = function (n) {
let ways = 0;

let maxStepTwo = Math.floor(n / 2);

const factorial = (num) => {
if (num === 0 || num === 1) {
return 1;
}

for (let i = 2; i <= num; i++) {
return num * factorial(num - 1);
}
};

for (let i = 0; i <= maxStepTwo; i++) {
const stepTwo = i;
const stepOne = n - 2 * i;
const steps = stepTwo + stepOne;

ways += factorial(steps) / (factorial(stepTwo) * factorial(stepOne));
}
Comment on lines +16 to +37
Copy link
Contributor

Choose a reason for hiding this comment

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

DP를 사용하시면 시간 복잡도를 O(n) 으로 줄일 수 있을것 같아요 :)


return ways;
};

55 changes: 55 additions & 0 deletions valid-anagram/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/

// 시간 복잡도: O(n)
// 공간 복잡도: O(n)

var isAnagram = function (s, t) {

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

let obj = {};

for (let k of s) {
obj[k] = (obj[k] || 0) + 1;

}

for (let k of t) {
if (obj[k] === undefined || obj[k] === 0) {
return false;
}
obj[k]--;
}

return true;

};

// 시간 복잡도: O(n log n)
// 공간 복잡도: O(n)

// var isAnagram = function (s, t) {

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

// let sArr = s.split("").sort();
// let tArr = t.split("").sort();

// for (let i = 0; i < sArr.length; i++) {
// if (sArr[i] !== tArr[i]) {
// return false;
// }
// }

// return true;

// };

Loading