diff --git a/doitchuu/BestTimeToBuyAndSellStock.js b/doitchuu/BestTimeToBuyAndSellStock.js new file mode 100644 index 0000000..7d78bda --- /dev/null +++ b/doitchuu/BestTimeToBuyAndSellStock.js @@ -0,0 +1,20 @@ +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function(prices) { + // MEMO: 시간 초과! + let maxPrice = 0; + + for (let i = 0; i < prices.length - 1; i++) { + for (let j = i + 1; j < prices.length; j++) { + const price = prices[i] - prices[j]; + + if (price < 0) { + maxPrice = Math.max(maxPrice, Math.abs(price)); + } + } + } + + return maxPrice; +}; diff --git a/doitchuu/MergeTwoSortedLists.js b/doitchuu/MergeTwoSortedLists.js new file mode 100644 index 0000000..fe0f383 --- /dev/null +++ b/doitchuu/MergeTwoSortedLists.js @@ -0,0 +1,15 @@ +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} list1 + * @param {ListNode} list2 + * @return {ListNode} + */ +var mergeTwoLists = function(list1, list2) { + // MEMO: 이 문제는 풀지 못했습니다. +}; diff --git a/doitchuu/TwoSum.js b/doitchuu/TwoSum.js index a260cdf..db22ab9 100644 --- a/doitchuu/TwoSum.js +++ b/doitchuu/TwoSum.js @@ -1,4 +1,16 @@ -// MEMO: 편하신 언어로 작성 부탁드립니다. 해당 주석은 풀이 입력 시 지워주세요. -function twoSum(nums, target) { - -} +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function(nums, target) { + for (let i = 0; i < nums.length; i++) { + for (let j = i + 1; j < nums.length; j++) { + if (nums[i] + nums[j] === target) { + const output = [i, j]; + + return output; + } + } + } +}; diff --git a/doitchuu/ValidParentheses.js b/doitchuu/ValidParentheses.js new file mode 100644 index 0000000..a84fc14 --- /dev/null +++ b/doitchuu/ValidParentheses.js @@ -0,0 +1,35 @@ + +/** + * @param {string} s + * @return {boolean} + */ +function isValid(s) { + if (s.length % 2 !== 0) { + return false; + } + + let count = 0; + const stack = []; + + for (let i = 0; i < s.length; i++) { + if (s[i] === "(" || s[i] === "{" || s[i] === "[") { + stack.push(s[i]); + count++; + continue; + } + + if (stack[stack.length - 1] === "(" && s[i] === ")") { + stack.pop(); + } + + if (stack[stack.length - 1] === "{" && s[i] === "}") { + stack.pop(); + } + + if (stack[stack.length - 1] === "[" && s[i] === "]") { + stack.pop(); + } + } + + return stack.length > 0 && count !== (s.length / 2) ? false : true; +};