Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions best-time-to-buy-and-sell-stock/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -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;
};
37 changes: 37 additions & 0 deletions encode-and-decode-strings/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -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;
};
46 changes: 46 additions & 0 deletions group-anagrams/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -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;
};
79 changes: 79 additions & 0 deletions implement-trie-prefix-tree/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -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)
*/
25 changes: 25 additions & 0 deletions word-break/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -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);
};
Loading