Skip to content
Open
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
11 changes: 11 additions & 0 deletions Jiho/Day28/Leetcode_70. Climbing Stairs.js
Original file line number Diff line number Diff line change
@@ -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));
7 changes: 7 additions & 0 deletions Jiho/Day28/Leetcode_746. Min Cost Climbing Stairs.js
Original file line number Diff line number Diff line change
@@ -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]);
};
17 changes: 17 additions & 0 deletions Jiho/Day29/Leetcode_198. House Robber.js
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions Jiho/Day29/Leetcode_213. House Robber II.js
Original file line number Diff line number Diff line change
@@ -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]));
Original file line number Diff line number Diff line change
@@ -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));
16 changes: 16 additions & 0 deletions Jiho/Day30/Leetcode_4. Median of Two Sorted Arrays.js
Original file line number Diff line number Diff line change
@@ -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]));
Loading