diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/sunjae95.js b/construct-binary-tree-from-preorder-and-inorder-traversal/sunjae95.js new file mode 100644 index 000000000..ef930c492 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/sunjae95.js @@ -0,0 +1,45 @@ +/** + * @description + * time complexity: O(n^2) + * space complexity: O(n) + * + * brainstorming: + * stack, Drawing a graph + * + * strategy: + * discover the rules + * leftStack = left create , rightStack = right create + */ +var buildTree = function (preorder, inorder) { + let answer = null; + let pointer = 0; + + const leftStack = []; + const rightStack = []; + + preorder.forEach((val, i) => { + const node = new TreeNode(val); + + if (i === 0) answer = node; + + const leftLen = leftStack.length; + const rightLen = rightStack.length; + + if (leftLen && rightLen) { + if (leftStack[leftLen - 1].left) rightStack[rightLen - 1].right = node; + else leftStack[leftLen - 1].left = node; + } + if (leftLen && !rightLen) leftStack[leftLen - 1].left = node; + if (!leftLen && rightLen) rightStack[rightLen - 1].right = node; + + leftStack.push(node); + + while (leftStack.length && pointer < inorder.length) { + if (leftStack[leftStack.length - 1].val !== inorder[pointer]) break; + rightStack.push(leftStack.pop()); + pointer++; + } + }); + + return answer; +}; diff --git a/counting-bits/sunjae95.js b/counting-bits/sunjae95.js new file mode 100644 index 000000000..c6639315b --- /dev/null +++ b/counting-bits/sunjae95.js @@ -0,0 +1,24 @@ +/** + * @description + * time complexity: O(n log n) + * space complexity: O(N) + * + * brainstorming: + * convert integer to bit + * for loop + * + * strategy: + * string change to hash table + */ +var countBits = function (n) { + return Array.from({ length: n + 1 }, (_, i) => convertBitCount(i)); +}; + +const convertBitCount = (n) => { + let count = 0; + while (n > 0) { + count += n % 2; + n = Math.floor(n / 2); + } + return count; +}; diff --git a/decode-ways/sunjae95.js b/decode-ways/sunjae95.js new file mode 100644 index 000000000..3961ad719 --- /dev/null +++ b/decode-ways/sunjae95.js @@ -0,0 +1,76 @@ +/** + * @description + * brainstorming: + * 1. dp -> dp[i] = dp[i-1] + count + * 2. recursive function + * + * strategy: + * https://www.algodale.com/problems/decode-ways/ + * + * result: + * 1. couldn't think of the conditions + * true: 1~9, 10~27 + * false: 0, 0N, 28↑ + * 2. persist solution that is top down + */ + +// https://www.algodale.com/problems/decode-ways/ Solve 1 +/** + * time complexity: O(2^n) + * space complexity: O(n) + */ +var numDecodings = function (s) { + const search = (start) => { + if (start === s.length) return 1; + if (s[start] === "0") return 0; + if (start + 1 < s.length && Number(`${s[start]}${s[start + 1]}`) < 27) { + return search(start + 1) + search(start + 2); + } + return search(start + 1); + }; + + return search(0); +}; + +// https://www.algodale.com/problems/decode-ways/ Solve 2 +/** + * time complexity: O(2^n) + * space complexity: O(n) + */ +var numDecodings = function (s) { + const memo = new Map(); + memo.set(s.length, 1); + + const search = (start) => { + if (!!memo.get(start)) return memo.get(start); + + if (s[start] === "0") memo.set(start, 0); + else if (start + 1 < s.length && Number(`${s[start]}${s[start + 1]}`) < 27) + memo.set(start, search(start + 1) + search(start + 2)); + else memo.set(start, search(start + 1)); + + return memo.get(start); + }; + + return search(0); +}; + +// https://www.algodale.com/problems/decode-ways/ Solve 3 +/** + * time complexity: O(n) + * space complexity: O(n) + */ +var numDecodings = function (s) { + const dp = Array.from({ length: s.length + 1 }, (_, i) => + i === s.length ? 1 : 0 + ); + + for (let i = s.length - 1; i >= 0; i--) { + if (s[i] === "0") dp[i] = 0; + else if (i + 1 < s.length && Number(`${s[i]}${s[i + 1]}`) < 27) + dp[i] = dp[i + 1] + dp[i + 2]; + else dp[i] = dp[i + 1]; + } + + return dp[0]; +}; diff --git a/palindromic-substrings/sunjae95.js b/palindromic-substrings/sunjae95.js index 1e3598be0..2522c0b92 100644 --- a/palindromic-substrings/sunjae95.js +++ b/palindromic-substrings/sunjae95.js @@ -88,3 +88,77 @@ var countSubstrings = function (s) { return answer.length; }; + +/** + * @description + * WEEK 01 Feedback + * 1. remove slice and change to endpoint + * 2. kadane's algorithm + * + * time complexity: O(N^3) + * space complexity: O(1) + * + * result + * solution 1 apply + */ +var countSubstrings = function (s) { + let answer = 0; + const len = s.length; + + for (let startIndex = 0; startIndex < len; startIndex++) { + let subStr = ""; + let isSubPalindromic = true; + answer++; + + for (let endIndex = startIndex + 1; endIndex < len; endIndex++) { + if (isSubPalindromic && s[startIndex] === s[endIndex]) answer++; + subStr += s[endIndex]; + isSubPalindromic = isPalindromic(subStr); + } + } + + return answer; +}; + +function isPalindromic(str) { + const len = str.length; + const middleIndex = Math.floor(len / 2); + + for (let i = 0; i < middleIndex; i++) { + if (str[i] !== str[len - 1 - i]) return false; + } + + return true; +} +/** + * @description + * WEEK 01 Feedback + * 1. remove slice and change to endpoint + * 2. kadane's algorithm + * + * time complexity: O(N^2) + * space complexity: O(1) + * + * result + * solve 3(https://www.algodale.com/problems/palindromic-substrings/) include 1,2 solution + */ +var countSubstrings = function (s) { + let count = 0; + const len = s.length; + + for (let i = 0; i < len; i++) { + let [start, end] = [i, i]; + + while (s[start] === s[end] && start >= 0 && end < len) { + count++; + [start, end] = [start - 1, end + 1]; + } + [start, end] = [i, i + 1]; + while (s[start] === s[end] && start >= 0 && end < len) { + count++; + [start, end] = [start - 1, end + 1]; + } + } + + return count; +}; diff --git a/valid-anagram/sunjae95.js b/valid-anagram/sunjae95.js new file mode 100644 index 000000000..2888ba7c3 --- /dev/null +++ b/valid-anagram/sunjae95.js @@ -0,0 +1,31 @@ +/** + * @description + * time complexity: O(N) + * space complexity: O(N) + * + * brainstorming: + * 1. hash table value compare to count + * + * strategy: + * string change to hash table + */ +var isAnagram = function (s, t) { + if (s.length !== t.length) return false; + + let answer = true; + const tableS = convertHashTable(s); + const tableT = convertHashTable(t); + + tableS.forEach((_, key) => { + if (tableT.get(key) && tableT.get(key) === tableS.get(key)) return; + answer = false; + }); + + return answer; +}; + +const convertHashTable = (str) => + str.split("").reduce((map, s) => { + map.set(s, (map.get(s) ?? 0) + 1); + return map; + }, new Map());