diff --git a/3sum/Jeehay28.js b/3sum/Jeehay28.js new file mode 100644 index 000000000..7c46a2859 --- /dev/null +++ b/3sum/Jeehay28.js @@ -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; + +// } + diff --git a/climbing-stairs/Jeehay28.js b/climbing-stairs/Jeehay28.js new file mode 100644 index 000000000..8775493ac --- /dev/null +++ b/climbing-stairs/Jeehay28.js @@ -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)); + } + + return ways; +}; + diff --git a/valid-anagram/Jeehay28.js b/valid-anagram/Jeehay28.js new file mode 100644 index 000000000..1ad1776b7 --- /dev/null +++ b/valid-anagram/Jeehay28.js @@ -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; + +// }; +