diff --git a/climbing-stairs/seona926.js b/climbing-stairs/seona926.js new file mode 100644 index 000000000..616a458ee --- /dev/null +++ b/climbing-stairs/seona926.js @@ -0,0 +1,24 @@ +/** + * @param {number} n + * @return {number} + */ +let climbStairs = function (n) { + if (n <= 1) return 1; + + let ways = new Array(n + 1); + ways[0] = 1; + ways[1] = 1; + + for (let i = 2; i <= n; i++) { + ways[i] = ways[i - 1] + ways[i - 2]; // 점화식 사용 + } + + return ways[n]; +}; + +/* + 1. 시간 복잡도: O(n) + - for 루프의 시간 복잡도 + 2. 공간 복잡도: O(n) + - 배열 ways의 공간 복잡도 +*/ diff --git a/two-sum/seona926.js b/two-sum/seona926.js new file mode 100644 index 000000000..ce03af969 --- /dev/null +++ b/two-sum/seona926.js @@ -0,0 +1,27 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +let twoSum = function (nums, target) { + let indices = {}; + + nums.forEach((item, index) => { + indices[item] = index; + }); + + for (let i = 0; i < nums.length; i++) { + let findNum = target - nums[i]; + + if (indices[findNum] !== i && findNum.toString() in indices) { + return [indices[findNum], i]; + } + } +}; + +/* + 1. 시간복잡도: O(n) + - forEach와 for루프의 시간복잡도가 각 O(n) + 2. 공간복잡도: O(n) + - indices 객체의 공간복잡도가 O(n), 나머지는 O(1) +*/