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
14 changes: 14 additions & 0 deletions best-time-to-buy-and-sell-stock/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function maxProfit(prices: number[]): number {
let minPrice = Infinity;
let maxProfit = 0;

for (const price of prices) {
if (price < minPrice) {
minPrice = price;
} else {
maxProfit = Math.max(maxProfit, price - minPrice);
}
}

return maxProfit;
}
47 changes: 47 additions & 0 deletions encode-and-decode-strings/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Solution {
private DELIMITER = "*";

/**
* @param strs: a list of strings
* @returns: encodes a list of strings to a single string.
*/
public encode(strs: string[]): string {
let encoded = "";

for (const str of strs) {
encoded += `${str.length}${this.DELIMITER}${str}`;
}

return encoded;
}

/**
* @param str: A string
* @returns: decodes a single string to a list of strings
*/
public decode(str: string): string[] {
const decoded: string[] = [];

let stack: string[] = [];
let pointer = 0;

while (pointer < str.length) {
const char = str[pointer];

if (char === this.DELIMITER) {
let strLength: number = Number(stack.join(""));
stack = [];

const word = str.substring(pointer + 1, pointer + 1 + strLength);
pointer = pointer + 1 + strLength;

decoded.push(word);
} else {
stack.push(char);
pointer++;
}
}

return decoded;
}
}
15 changes: 15 additions & 0 deletions group-anagrams/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function groupAnagrams(strs: string[]): string[][] {
const anagramsMap = new Map<string, string[]>();

for (const word of strs) {
const anagramKey = [...word].sort().join();

if (anagramsMap.has(anagramKey)) {
anagramsMap.get(anagramKey)!.push(word);
} else {
anagramsMap.set(anagramKey, [word]);
}
}

return Array.from(anagramsMap.values());
}
69 changes: 69 additions & 0 deletions implement-trie-prefix-tree/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class TNode {
isEndOf: boolean;
children: Map<string, TNode>;

constructor() {
this.isEndOf = false;
this.children = new Map<string, TNode>();
}
}

class Trie {
private root: TNode;

constructor() {
this.root = new TNode();
}

insert(word: string): void {
let currentNode: TNode = this.root;

for (const ch of word) {
if (currentNode.children.has(ch)) {
currentNode = currentNode.children.get(ch)!;
} else {
const newNode = new TNode();

currentNode.children.set(ch, newNode);
currentNode = currentNode.children.get(ch)!;
}
}
currentNode.isEndOf = true;
}

search(word: string): boolean {
let currentNode = this.root;

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

currentNode = currentNode.children.get(ch)!;
}

return currentNode.isEndOf;
}

startsWith(prefix: string): boolean {
let currentNode = this.root;

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

currentNode = currentNode.children.get(ch)!;
}

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)
*/
93 changes: 93 additions & 0 deletions word-break/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// using set
function wordBreak(s: string, wordDict: string[]): boolean {
const sLen = s.length;

const dp: boolean[] = new Array(sLen + 1).fill(false);
dp[0] = true;

const wordSet = new Set<string>(wordDict);

for (let i = 1; i <= sLen; i++) {
for (let j = 0; j < i; j++) {
if (dp[j] && wordSet.has(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}

return dp[sLen];
}

// using trie
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다! 코드가 이해하기 좋고, 매우 깔끔하네요 👍 근데 다른 문제의 코드가 들어있는 것 같아요..!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trie랑 TNode class 때문에 그러신 건가요? 해당 문제를 trie로도 풀 수 있다기에 한 번 풀어봤습니다.

class TNode {
isEndOf: boolean;
children: Map<string, TNode>;

constructor() {
this.isEndOf = false;
this.children = new Map<string, TNode>();
}
}

class Trie {
root: TNode;

constructor() {
this.root = new TNode();
}

insert(word: string): void {
let currentNode = this.root;

for (const ch of word) {
if (!currentNode.children.has(ch)) {
currentNode.children.set(ch, new TNode());
}

currentNode = currentNode.children.get(ch)!;
}

currentNode.isEndOf = true;
}

search(word: string): boolean {
let currentNode = this.root;

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

currentNode = currentNode.children.get(ch)!;
}

return currentNode.isEndOf;
}
}

function wordBreak(s: string, wordDict: string[]): boolean {
const sLen = s.length;

const dp: boolean[] = new Array(sLen + 1).fill(false);
dp[0] = true;

const trie = new Trie();

for (const word of wordDict) {
trie.insert(word);
}

for (let i = 1; i <= sLen; i++) {
for (let j = 0; j < i; j++) {
if (dp[j]) {
if (trie.search(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
}

return dp[sLen];
}