diff --git a/solutions/javascript/practice1.js b/solutions/javascript/practice1.js index 54c7d32..8f90fcc 100644 --- a/solutions/javascript/practice1.js +++ b/solutions/javascript/practice1.js @@ -1,49 +1,24 @@ -// Problem: Valid Parentheses -// Source: https://leetcode.com/problems/valid-parentheses/ -// Note: Use an array as a stack and a lookup object to match closing brackets -// Time: O(n), Space: O(n) +// Problem: Two Sum +// Source: https://leetcode.com/problems/two-sum/ +// Note: Uses a Map for efficient O(n) lookup. -function isValid(s) { - const matchingOpen = { - ")": "(", - "]": "[", - "}": "{", - }; - const stack = []; +function twoSum(nums, target) { + const seen = new Map(); - for (const char of s) { - if (Object.hasOwn(matchingOpen, char)) { - if (stack.length === 0 || stack.pop() !== matchingOpen[char]) { - return false; - } - } else { - stack.push(char); - } - } - - return stack.length === 0; -} + for (let i = 0; i < nums.length; i++) { + const complement = target - nums[i]; -if (require.main === module) { - const cases = [ - ["()", true], - ["()[]{}", true], - ["(]", false], - ["([])", true], - ["([)]", false], - ["{[]}", true], - ["", true], - ["(", false], - ["(((((((((())))))))))", true], - ]; + if (seen.has(complement)) { + return [seen.get(complement), i]; + } - for (const [value, expected] of cases) { - if (isValid(value) !== expected) { - throw new Error("Failed for input: " + value); + seen.set(nums[i], i); } - } - console.log("All " + cases.length + " test cases passed!"); + return []; } -module.exports = { isValid }; +// Example tests +console.log(twoSum([2, 7, 11, 15], 9)); // [0, 1] +console.log(twoSum([3, 2, 4], 6)); // [1, 2] +console.log(twoSum([3, 3], 6)); // [0, 1]