diff --git a/best-time-to-buy-and-sell-stock/hsskey.js b/best-time-to-buy-and-sell-stock/hsskey.js new file mode 100644 index 000000000..a3d7f7831 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/hsskey.js @@ -0,0 +1,19 @@ +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function(prices) { + let buy = prices[0] + let maxVal = 0 + + for(let i = 1; i < prices.length; i++) { + if(prices[i - 1] > prices[i]) { + buy = Math.min(buy, prices[i]) + } + + if(prices[i - 1] < prices[i]) { + maxVal = Math.max(maxVal, prices[i] - buy) + } + } + return maxVal +}; diff --git a/encode-and-decode-strings/hsskey.js b/encode-and-decode-strings/hsskey.js new file mode 100644 index 000000000..061fdb6a6 --- /dev/null +++ b/encode-and-decode-strings/hsskey.js @@ -0,0 +1,33 @@ +class Solution { + /** + * @param {string[]} strs + * @returns {string} + */ + encode(strs) { + return strs.map((item) => `${item.length}#${item}`).join(''); + } + + /** + * @param {string} str + * @returns {string[]} + */ + decode(str) { + const result = []; + let i = 0; + + while (i < str.length) { + let j = i; + while (str[j] !== '#') { + j++; + } + + const length = parseInt(str.slice(i, j)); + const word = str.slice(j + 1, j + 1 + length); + result.push(word); + + i = j + 1 + length; + } + + return result; + } +} diff --git a/group-anagrams/hsskey.js b/group-anagrams/hsskey.js new file mode 100644 index 000000000..e22a2edb4 --- /dev/null +++ b/group-anagrams/hsskey.js @@ -0,0 +1,18 @@ +/** + * @param {string[]} strs + * @return {string[][]} + */ +var groupAnagrams = function(strs) { + const map = new Map() + + for(const str of strs) { + const sortedStr = [...str].sort().join('') + if(map.has(sortedStr)) { + map.get(sortedStr).push(str) + } else { + map.set(sortedStr, [str]) + } + } + + return Array.from(map.values()) +}; diff --git a/implement-trie-prefix-tree/hsskey.js b/implement-trie-prefix-tree/hsskey.js new file mode 100644 index 000000000..a52da9050 --- /dev/null +++ b/implement-trie-prefix-tree/hsskey.js @@ -0,0 +1,51 @@ +class TrieNode { + constructor() { + this.children = {}; + this.isEndOfWord = false; + } +} + +var Trie = function() { + this.root = new TrieNode(); +}; + +/** + * @param {string} word + * @return {void} + */ +Trie.prototype.insert = function(word) { + let node = this.root; + for (const char of word) { + if (!node.children[char]) { + node.children[char] = new TrieNode(); + } + node = node.children[char]; + } + node.isEndOfWord = true; +}; + +/** + * @param {string} word + * @return {boolean} + */ +Trie.prototype.search = function(word) { + let node = this.root; + for (const char of word) { + if (!node.children[char]) return false; + node = node.children[char]; + } + return node.isEndOfWord; +}; + +/** + * @param {string} prefix + * @return {boolean} + */ +Trie.prototype.startsWith = function(prefix) { + let node = this.root; + for (const char of prefix) { + if (!node.children[char]) return false; + node = node.children[char]; + } + return true; +}; diff --git a/word-break/hsskey.js b/word-break/hsskey.js new file mode 100644 index 000000000..9306c7f37 --- /dev/null +++ b/word-break/hsskey.js @@ -0,0 +1,22 @@ +/** + * @param {string} s + * @param {string[]} wordDict + * @return {boolean} + */ +var wordBreak = function(s, wordDict) { + const wordSet = new Set(wordDict); + const dp = new Array(s.length + 1).fill(false); + dp[0] = true; + + for (let i = 1; i <= s.length; i++) { + for (let j = 0; j < i; j++) { + const word = s.slice(j, i); + if (dp[j] && wordSet.has(word)) { + dp[i] = true; + break; + } + } + } + + return dp[s.length]; +};