diff --git a/best-time-to-buy-and-sell-stock/gwbaik9717.js b/best-time-to-buy-and-sell-stock/gwbaik9717.js new file mode 100644 index 000000000..a42e22825 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/gwbaik9717.js @@ -0,0 +1,18 @@ +// Time complexity: O(n) +// Space complexity: O(1) + +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function (prices) { + let answer = 0; + let minValue = Number.MAX_SAFE_INTEGER; + + for (const price of prices) { + minValue = Math.min(minValue, price); + answer = Math.max(answer, price - minValue); + } + + return answer; +}; diff --git a/encode-and-decode-strings/gwbaik9717.js b/encode-and-decode-strings/gwbaik9717.js new file mode 100644 index 000000000..68ddc950b --- /dev/null +++ b/encode-and-decode-strings/gwbaik9717.js @@ -0,0 +1,37 @@ +// n: len(str) +// Time complexity: O(n) +// Space complexity: O(1) +const encode = function (arr) { + let answer = ""; + + for (const word of arr) { + answer += `${word.length}${SEPERATOR}`; + } + + return answer; +}; + +// n: len(str) +// Time complexity: O(n) +// Space complexity: O(n) +const decode = function (str) { + const SEPERATOR = "|"; + const words = []; + + let i = 0; + let wordLength = ""; + + while (i < str.length) { + if (str[i] === SEPERATOR) { + words.push(str.slice(i + 1, i + 1 + Number(wordLength))); + i += Number(wordLength) + 1; + wordLength = ""; + continue; + } + + wordLength += str[i]; + i += 1; + } + + return words; +}; diff --git a/group-anagrams/gwbaik9717.js b/group-anagrams/gwbaik9717.js new file mode 100644 index 000000000..68633e595 --- /dev/null +++ b/group-anagrams/gwbaik9717.js @@ -0,0 +1,46 @@ +// n: length of strs, m: length of strs[i] +// Time complexity: O(nm) +// Space complexity: O(n) + +/** + * @param {string[]} strs + * @return {string[][]} + */ +var groupAnagrams = function (strs) { + const answer = []; + const anagramDict = new Map(); + + const getKey = (str) => { + const minCharCode = "a".charCodeAt(); + const maxCharCode = "z".charCodeAt(); + + const counter = Array.from( + { length: maxCharCode - minCharCode + 1 }, + () => 0 + ); + + for (const chr of str) { + const index = chr.charCodeAt() - minCharCode; + counter[index]++; + } + + return counter.join("#"); + }; + + for (let i = 0; i < strs.length; i++) { + const str = strs[i]; + const key = getKey(str); + + if (!anagramDict.has(key)) { + anagramDict.set(key, []); + } + + anagramDict.get(key).push(str); + } + + for (const [_, value] of anagramDict) { + answer.push(value); + } + + return answer; +}; diff --git a/implement-trie-prefix-tree/gwbaik9717.js b/implement-trie-prefix-tree/gwbaik9717.js new file mode 100644 index 000000000..5d47e8fb6 --- /dev/null +++ b/implement-trie-prefix-tree/gwbaik9717.js @@ -0,0 +1,79 @@ +var Node = function () { + this.children = new Map(); + this.isEnd = false; +}; + +var Trie = function () { + this.head = new Node(); +}; + +/** + * @param {string} str + * @return {TrieNode | null} + */ +Trie.prototype._traverse = function (str) { + let current = this.head; + + for (const chr of str) { + if (!current.children.has(chr)) { + return null; + } + current = current.children.get(chr); + } + + return current; +}; + +/** + * @param {string} word + * @return {void} + */ +Trie.prototype.insert = function (word) { + let current = this.head; + + for (const chr of word) { + if (!current.children.has(chr)) { + current.children.set(chr, new Node()); + } + + current = current.children.get(chr); + } + + current.isEnd = true; +}; + +/** + * @param {string} word + * @return {boolean} + */ +Trie.prototype.search = function (word) { + const node = this._traverse(word); + + if (!node) { + return false; + } + + return node.isEnd; +}; + +/** + * @param {string} prefix + * @return {boolean} + */ +Trie.prototype.startsWith = function (prefix) { + const node = this._traverse(prefix); + + if (!node) { + return false; + } + + return true; +}; + +/** + * Your Trie object will be instantiated and called as such: + * var obj = new Trie() + * obj.insert(word) + * var param_2 = obj.search(word) + * var param_3 = obj.startsWith(prefix) + */ diff --git a/word-break/gwbaik9717.js b/word-break/gwbaik9717.js new file mode 100644 index 000000000..ca7754f75 --- /dev/null +++ b/word-break/gwbaik9717.js @@ -0,0 +1,25 @@ +// n: len(s), m: len(wordDict) +// Time complexity: O(n^2*m) +// Space complexity: O(n) + +/** + * @param {string} s + * @param {string[]} wordDict + * @return {boolean} + */ +var wordBreak = function (s, wordDict) { + const dp = Array.from({ length: s.length + 1 }, () => false); + dp[0] = true; + + for (let i = 1; i <= s.length; i++) { + for (const word of wordDict) { + const sliced = s.slice(i - word.length, i); + + if (word === sliced && !dp[i]) { + dp[i] = dp[i - word.length]; + } + } + } + + return dp.at(-1); +};