diff --git a/sik9252/BestTimeToBuyAndSellStock.js b/sik9252/BestTimeToBuyAndSellStock.js new file mode 100644 index 0000000..bc2ef33 --- /dev/null +++ b/sik9252/BestTimeToBuyAndSellStock.js @@ -0,0 +1,14 @@ +var maxProfit = function (prices) { + let minPrice = Infinity; + let currentProfit = 0; + + for (let i = 0; i < prices.length; i++) { + if (prices[i] < minPrice) { + minPrice = prices[i]; + } else if (prices[i] - minPrice > currentProfit) { + currentProfit = prices[i] - minPrice; + } + } + + return currentProfit; +}; diff --git a/sik9252/MergeTwoSortedList.js b/sik9252/MergeTwoSortedList.js new file mode 100644 index 0000000..66c4801 --- /dev/null +++ b/sik9252/MergeTwoSortedList.js @@ -0,0 +1,24 @@ +var mergeTwoLists = function (list1, list2) { + const start = new ListNode(-1); + let curr = start; + + while (list1 !== null && list2 !== null) { + if (list1.val <= list2.val) { + curr.next = list1; + list1 = list1.next; + } else { + curr.next = list2; + list2 = list2.next; + } + + curr = curr.next; + } + + if (list1 !== null) { + curr.next = list1; + } else { + curr.next = list2; + } + + return start.next; +}; diff --git a/sik9252/TwoSum.js b/sik9252/TwoSum.js index a260cdf..d042c68 100644 --- a/sik9252/TwoSum.js +++ b/sik9252/TwoSum.js @@ -1,4 +1,9 @@ -// MEMO: 편하신 언어로 작성 부탁드립니다. 해당 주석은 풀이 입력 시 지워주세요. -function twoSum(nums, target) { - -} +var twoSum = function (nums, target) { + for (let i = 0; i < nums.length - 1; i++) { + for (let j = i + 1; j < nums.length; j++) { + if (nums[i] + nums[j] === target) { + return [i, j]; + } + } + } +}; diff --git a/sik9252/ValidParentheses.js b/sik9252/ValidParentheses.js new file mode 100644 index 0000000..f6b02ad --- /dev/null +++ b/sik9252/ValidParentheses.js @@ -0,0 +1,23 @@ +var isValid = function (s) { + if (s.length % 2 !== 0) return false; + + const stack = []; + const map = { + ")": "(", + "]": "[", + "}": "{", + }; + + for (const char of s) { + if (map[char]) { + const topElement = stack.pop(); + if (topElement !== map[char]) { + return false; + } + } else { + stack.push(char); + } + } + + return stack.length === 0; +};