Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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;
};
43 changes: 43 additions & 0 deletions group-anagrams/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 counter = Array.from(
{ length: "z".charCodeAt() - "a".charCodeAt() + 1 },
() => 0
);

for (const chr of str) {
const index = chr.charCodeAt() - "a".charCodeAt();
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;
};
70 changes: 70 additions & 0 deletions implement-trie-prefix-tree/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
var Node = function () {
this.children = new Map();
this.isEnd = false;
};

var Trie = function () {
this.head = new Node();
};

/**
* @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) {
let current = this.head;

for (const chr of word) {
if (!current.children.has(chr)) {
return false;
}

current = current.children.get(chr);
}

return current.isEnd;
};

/**
* @param {string} prefix
* @return {boolean}
*/
Trie.prototype.startsWith = function (prefix) {
let current = this.head;

for (const chr of prefix) {
if (!current.children.has(chr)) {
return false;
}

current = current.children.get(chr);
}

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