diff --git a/SeungyeonJi/week7/2023-10-29-SumRootToLeafNumbers.js b/SeungyeonJi/week7/2023-10-29-SumRootToLeafNumbers.js index 2f55c3b0..d36ac3ba 100644 --- a/SeungyeonJi/week7/2023-10-29-SumRootToLeafNumbers.js +++ b/SeungyeonJi/week7/2023-10-29-SumRootToLeafNumbers.js @@ -24,3 +24,5 @@ var sumNumbers = function (root) { return num; }; + + diff --git a/SeungyeonJi/week8/2023-11-03-.js b/SeungyeonJi/week8/2023-11-03-PartitionEqualSubsetSum.js similarity index 100% rename from SeungyeonJi/week8/2023-11-03-.js rename to SeungyeonJi/week8/2023-11-03-PartitionEqualSubsetSum.js diff --git a/SeungyeonJi/week8/2023-11-05-SubarraySumEqualsK.js b/SeungyeonJi/week8/2023-11-05-SubarraySumEqualsK.js new file mode 100644 index 00000000..e69de29b diff --git a/SeungyeonJi/week8/2023-11-05.MaximumSubarrayjs b/SeungyeonJi/week8/2023-11-05.MaximumSubarrayjs new file mode 100644 index 00000000..3700a5b6 --- /dev/null +++ b/SeungyeonJi/week8/2023-11-05.MaximumSubarrayjs @@ -0,0 +1,11 @@ +var maxSubArray = function (nums) { + let maxSum = nums[0]; + let currentSum = nums[0]; + + for (let i = 0; i < nums.length; i++) { + currentSum = Math.max(num[i], currentSum + num[i]); + maxSum = Math.max(maxSum, currentSum); + } + + return maxSum; +}; diff --git a/SeungyeonJi/week9/2023-11-10-LetterCombinationsOfAPhoneNumber.js b/SeungyeonJi/week9/2023-11-10-LetterCombinationsOfAPhoneNumber.js new file mode 100644 index 00000000..7c28905f --- /dev/null +++ b/SeungyeonJi/week9/2023-11-10-LetterCombinationsOfAPhoneNumber.js @@ -0,0 +1,38 @@ +let letterCombinations = function (digits) { + let result = []; + + //alpha has map + const alpha = { + 2: "abc", + 3: "def", + 4: "ghi", + 5: "jkl", + 6: "mno", + 7: "pqrs", + 8: "tuv", + 9: "wxyz", + }; + + const dfs = function (i, digits, state) { + // base case (condition to sotp this recursive function) + + // If we loop through digits.length time, we will return the result; + if (i === digits.length) { + result.push(slate.join("")); + return; + } + + //dfs recursive case + let chars = alpha[digits[i]]; + + for (let char of chars) { + state.push(char); + dfs(i + 1, digits, state); + state.pop(); + } + }; + + dfs(0, digits, []); + + return result; +}; diff --git a/SeungyeonJi/week9/2023-11-12-SplitAStringIntoTheMaxNumberOfUniqueSubstrings.js b/SeungyeonJi/week9/2023-11-12-SplitAStringIntoTheMaxNumberOfUniqueSubstrings.js new file mode 100644 index 00000000..ec05edaf --- /dev/null +++ b/SeungyeonJi/week9/2023-11-12-SplitAStringIntoTheMaxNumberOfUniqueSubstrings.js @@ -0,0 +1,24 @@ +var maxUniqueSplit = function (s) { + let wordSet = new Set(), + res = 1; + + function checkUniqueSubstring(i) { + if (i === s.length) { + res = Math.max(wordSet.size, res); + return; + } + + for (let j = i + 1; j <= s.length; j++) { + let str = s.substring(i, j); + if (!wordSet.has(str)) { + wordSet.add(str); + checkUniqueSubstring(j); + wordSet.delete(str); + } + } + } + + checkUniqueSubstring(0); + + return res; +}; diff --git a/SeungyeonJi/week9/2023-11-12-WordSearch.js b/SeungyeonJi/week9/2023-11-12-WordSearch.js new file mode 100644 index 00000000..833163a7 --- /dev/null +++ b/SeungyeonJi/week9/2023-11-12-WordSearch.js @@ -0,0 +1,36 @@ +const isOutOfBound = (board, row, col) => + row < 0 || row >= board.length || col < 0 || col >= board[0].length; + +const checkNeighbors = (board, word, row, col) => { + // Check exit conditions + if (!word.length) return true; + if (isOutOfBound(board, row, col) || board[row][col] !== word[0]) + return false; + + // Save some stuff + const curChar = board[row][col]; + const newWord = word.substr(1); + + board[row][col] = 0; // Disable the current character + + // Check if neighbors are fruitful + const results = + checkNeighbors(board, newWord, row + 1, col) || + checkNeighbors(board, newWord, row - 1, col) || + checkNeighbors(board, newWord, row, col + 1) || + checkNeighbors(board, newWord, row, col - 1); + + // Enable current character + board[row][col] = curChar; + + return results; +}; + +var exist = function (board, word) { + for (let row = 0; row < board.length; row++) { + for (let col = 0; col < board[0].length; col++) { + if (checkNeighbors(board, word, row, col)) return true; + } + } + return false; +};