diff --git a/Jiho/Day28/Leetcode_70. Climbing Stairs.js b/Jiho/Day28/Leetcode_70. Climbing Stairs.js new file mode 100644 index 0000000..f72967c --- /dev/null +++ b/Jiho/Day28/Leetcode_70. Climbing Stairs.js @@ -0,0 +1,11 @@ +var climbStairs = function (n) { + let arr = new Array(n + 1); + arr[1] = 1; + arr[2] = 2; + for (let i = 3; i <= n; i++) { + arr[i] = arr[i - 1] + arr[i - 2]; + } + return arr[n]; +}; + +console.log(climbStairs(10)); diff --git a/Jiho/Day28/Leetcode_746. Min Cost Climbing Stairs.js b/Jiho/Day28/Leetcode_746. Min Cost Climbing Stairs.js new file mode 100644 index 0000000..3492434 --- /dev/null +++ b/Jiho/Day28/Leetcode_746. Min Cost Climbing Stairs.js @@ -0,0 +1,7 @@ +var minCostClimbingStairs = function (cost) { + for (let i = cost.length - 3; i >= 0; i--) { + cost[i] += Math.min(cost[i + 1], cost[i + 2]); + } + + return Math.min(cost[0], cost[1]); +}; diff --git a/Jiho/Day29/Leetcode_198. House Robber.js b/Jiho/Day29/Leetcode_198. House Robber.js new file mode 100644 index 0000000..45a55cc --- /dev/null +++ b/Jiho/Day29/Leetcode_198. House Robber.js @@ -0,0 +1,17 @@ +var rob = function (nums) { + const len = nums.length; + + let arr = new Array(len); + arr[0] = nums[0]; + arr[1] = Math.max(nums[0], nums[1]); + let i; + for (i = 2; i < len; i++) { + arr[i] = Math.max(arr[i - 1], arr[i - 2] + nums[i]); + } + + return arr[len - 1]; +}; + +console.log(rob([2, 1])); + +// DP diff --git a/Jiho/Day29/Leetcode_213. House Robber II.js b/Jiho/Day29/Leetcode_213. House Robber II.js new file mode 100644 index 0000000..7429e0e --- /dev/null +++ b/Jiho/Day29/Leetcode_213. House Robber II.js @@ -0,0 +1,19 @@ +var rob2 = function (nums) { + const getMax = (nums) => { + let prevRob = 0, + maxRob = 0; + + for (let num of nums) { + let temp = Math.max(maxRob, prevRob + num); + prevRob = maxRob; + maxRob = temp; + } + + return maxRob; + }; + + if (nums.length === 1) return nums[0]; + return Math.max(getMax(nums.slice(0, -1)), getMax(nums.slice(1)), nums[0]); +}; + +console.log(rob2([1, 2, 3, 1])); diff --git a/Jiho/Day30/Leetcode_34. Find First and Last Position of Element in Sorted Array.js b/Jiho/Day30/Leetcode_34. Find First and Last Position of Element in Sorted Array.js new file mode 100644 index 0000000..bdd0d07 --- /dev/null +++ b/Jiho/Day30/Leetcode_34. Find First and Last Position of Element in Sorted Array.js @@ -0,0 +1,26 @@ +var searchRange = function (nums, target) { + let arr = []; + let [left, right] = [0, nums.length - 1]; + const find_match = (l, r) => { + while (l <= r) { + const mid = Math.floor((l + r) / 2); + const val = nums[mid]; + if (val > target) { + r = mid - 1; + } else if (val < target) { + l = mid + 1; + } else { + arr.push(mid); + find_match(l, mid - 1); + find_match(mid + 1, r); + return; + } + } + }; + + find_match(left, right); + + return arr.length === 0 ? [-1, -1] : [Math.min(...arr), Math.max(...arr)]; +}; + +console.log(searchRange([], 6)); diff --git a/Jiho/Day30/Leetcode_4. Median of Two Sorted Arrays.js b/Jiho/Day30/Leetcode_4. Median of Two Sorted Arrays.js new file mode 100644 index 0000000..e14dae2 --- /dev/null +++ b/Jiho/Day30/Leetcode_4. Median of Two Sorted Arrays.js @@ -0,0 +1,16 @@ +var findMedianSortedArrays = function (nums1, nums2) { + let arr = [...nums1, ...nums2]; + arr.sort((a, b) => a - b); + const len = arr.length; + // 홀수 + if (len % 2) { + return arr[Math.floor(len / 2)]; + } + // 짝수 + else { + const mid = Math.floor(len / 2); + return (arr[mid] + arr[mid - 1]) / 2; + } +}; + +console.log(findMedianSortedArrays([1, 3], [2, 4, 5]));