From e930fadfd6871f31cca1e036e1cf9f2e7a9b8fa9 Mon Sep 17 00:00:00 2001 From: JiHo00 <110150963+jihostudy@users.noreply.github.com> Date: Sun, 28 Jul 2024 22:29:31 +0900 Subject: [PATCH 1/3] day28 2Q JIHO --- Jiho/Day28/Leetcode_70. Climbing Stairs.js | 11 +++++++++++ Jiho/Day28/Leetcode_746. Min Cost Climbing Stairs.js | 7 +++++++ 2 files changed, 18 insertions(+) create mode 100644 Jiho/Day28/Leetcode_70. Climbing Stairs.js create mode 100644 Jiho/Day28/Leetcode_746. Min Cost Climbing Stairs.js 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]); +}; From b4a29a4eb1a21ce39e1c7e9bc95189871d7358a8 Mon Sep 17 00:00:00 2001 From: JiHo00 <110150963+jihostudy@users.noreply.github.com> Date: Mon, 29 Jul 2024 20:27:28 +0900 Subject: [PATCH 2/3] Day29 2Q JIHO --- Jiho/Day29/Leetcode_198. House Robber.js | 17 +++++++++++++++++ Jiho/Day29/Leetcode_213. House Robber II.js | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 Jiho/Day29/Leetcode_198. House Robber.js create mode 100644 Jiho/Day29/Leetcode_213. House Robber II.js 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])); From b891ba7a0b1e50df1af8b113b3c6ed8453935fa5 Mon Sep 17 00:00:00 2001 From: JiHo00 <110150963+jihostudy@users.noreply.github.com> Date: Tue, 30 Jul 2024 21:36:59 +0900 Subject: [PATCH 3/3] =?UTF-8?q?Day30=202Q=20=EC=A7=80=ED=98=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ast Position of Element in Sorted Array.js | 26 +++++++++++++++++++ ...Leetcode_4. Median of Two Sorted Arrays.js | 16 ++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 Jiho/Day30/Leetcode_34. Find First and Last Position of Element in Sorted Array.js create mode 100644 Jiho/Day30/Leetcode_4. Median of Two Sorted Arrays.js 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]));