diff --git a/combination-sum/Wonjuny0804.ts b/combination-sum/Wonjuny0804.ts new file mode 100644 index 000000000..2f5dd57e4 --- /dev/null +++ b/combination-sum/Wonjuny0804.ts @@ -0,0 +1,22 @@ +function combinationSum(candidates: number[], target: number): number[][] { + const result: number[][] = []; + + const dfs = (start: number, target: number, path: number[]) => { + if (target === 0) { + result.push([...path]); + return; + } + + for (let i = start; i < candidates.length; i++) { + if (candidates[i] > target) continue; + + path.push(candidates[i]); + dfs(i, target - candidates[i], path); + path.pop(); + } + } + + candidates.sort((a, b) => a - b); + dfs(0, target, []); + return result; +}; diff --git a/decode-ways/Wonjuny0804.ts b/decode-ways/Wonjuny0804.ts new file mode 100644 index 000000000..069581715 --- /dev/null +++ b/decode-ways/Wonjuny0804.ts @@ -0,0 +1,21 @@ +function numDecodings(s: string): number { + const memo: Record = {}; + + const dfs = (index: number): number => { + if (index === s.length) return 1; + if (s[index] === '0') return 0; + + if (memo[index] !== undefined) return memo[index]; + + let res = dfs(index + 1); + + if (index + 1 < s.length && Number(s.slice(index, index + 2)) <= 26) { + res += dfs(index + 2); + } + + memo[index] = res; + return res; + } + + return dfs(0); +} diff --git a/maximum-subarray/Wonjuny0804.ts b/maximum-subarray/Wonjuny0804.ts new file mode 100644 index 000000000..f725ff8ea --- /dev/null +++ b/maximum-subarray/Wonjuny0804.ts @@ -0,0 +1,13 @@ +function maxSubArray(nums: number[]): number { + const n = nums.length; + const dp = new Array(n).fill(0); + dp[0] = nums[0]; + let result = dp[0]; + + for (let i = 1; i < n; i++) { + dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]); + result = Math.max(result, dp[i]); + } + + return result; +} diff --git a/number-of-1-bits/Wonjuny0804.ts b/number-of-1-bits/Wonjuny0804.ts new file mode 100644 index 000000000..45802d8e6 --- /dev/null +++ b/number-of-1-bits/Wonjuny0804.ts @@ -0,0 +1,6 @@ +function hammingWeight(n: number): number { + return n + .toString(2) + .split("") + .reduce((acc, i) => acc + +i, 0); +} diff --git a/two-sum/Wonjuny0804.ts b/two-sum/Wonjuny0804.ts new file mode 100644 index 000000000..864110456 --- /dev/null +++ b/two-sum/Wonjuny0804.ts @@ -0,0 +1,35 @@ +/* + + Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. + You may assume that each input would have exactly one solution, and you may not use the same element twice. + You can return the answer in any order. + + Example 1: + + Input: nums = [2,7,11,15], target = 9 + Output: [0,1] + Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. + + Example 2: + + Input: nums = [3,2,4], target = 6 + Output: [1,2] + + Example 3: + + Input: nums = [3,3], target = 6 + Output: [0,1] + +*/ + +function twoSum(nums: number[], target: number): number[] { + const map = new Map(); + + for (let i = 0; i < nums.length; i++) { + const diff = target - nums[i]; + if (map.has(nums[i])) return [map.get(nums[i]), i]; + else map.set(diff, i); + } + + return []; +} diff --git a/valid-palindrome/Wonjuny0804.ts b/valid-palindrome/Wonjuny0804.ts new file mode 100644 index 000000000..c868e3ac9 --- /dev/null +++ b/valid-palindrome/Wonjuny0804.ts @@ -0,0 +1,12 @@ +function isPalindrome(s: string): boolean { + const lowered = s.toLowerCase(); + const removed = lowered.replaceAll( + /[!|\s|.\\|\|()@#$%^&*_\-+,:'"\[\]{}\?;`]/g, + "" + ); + + for (let i = 0; i < removed.length; i++) { + if (removed[i] !== removed[removed.length - i - 1]) return false; + } + return true; +}