Skip to content

Commit 3de3b7a

Browse files
authored
Merge pull request #1848 from hyer0705/main
2 parents edddfe9 + b5fc3ec commit 3de3b7a

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function maxProfit(prices: number[]): number {
2+
let minPrice = Infinity;
3+
let maxProfit = 0;
4+
5+
for (const price of prices) {
6+
if (price < minPrice) {
7+
minPrice = price;
8+
} else {
9+
maxProfit = Math.max(maxProfit, price - minPrice);
10+
}
11+
}
12+
13+
return maxProfit;
14+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
private DELIMITER = "*";
3+
4+
/**
5+
* @param strs: a list of strings
6+
* @returns: encodes a list of strings to a single string.
7+
*/
8+
public encode(strs: string[]): string {
9+
let encoded = "";
10+
11+
for (const str of strs) {
12+
encoded += `${str.length}${this.DELIMITER}${str}`;
13+
}
14+
15+
return encoded;
16+
}
17+
18+
/**
19+
* @param str: A string
20+
* @returns: decodes a single string to a list of strings
21+
*/
22+
public decode(str: string): string[] {
23+
const decoded: string[] = [];
24+
25+
let stack: string[] = [];
26+
let pointer = 0;
27+
28+
while (pointer < str.length) {
29+
const char = str[pointer];
30+
31+
if (char === this.DELIMITER) {
32+
let strLength: number = Number(stack.join(""));
33+
stack = [];
34+
35+
const word = str.substring(pointer + 1, pointer + 1 + strLength);
36+
pointer = pointer + 1 + strLength;
37+
38+
decoded.push(word);
39+
} else {
40+
stack.push(char);
41+
pointer++;
42+
}
43+
}
44+
45+
return decoded;
46+
}
47+
}

group-anagrams/hyer0705.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function groupAnagrams(strs: string[]): string[][] {
2+
const anagramsMap = new Map<string, string[]>();
3+
4+
for (const word of strs) {
5+
const anagramKey = [...word].sort().join();
6+
7+
if (anagramsMap.has(anagramKey)) {
8+
anagramsMap.get(anagramKey)!.push(word);
9+
} else {
10+
anagramsMap.set(anagramKey, [word]);
11+
}
12+
}
13+
14+
return Array.from(anagramsMap.values());
15+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
class TNode {
2+
isEndOf: boolean;
3+
children: Map<string, TNode>;
4+
5+
constructor() {
6+
this.isEndOf = false;
7+
this.children = new Map<string, TNode>();
8+
}
9+
}
10+
11+
class Trie {
12+
private root: TNode;
13+
14+
constructor() {
15+
this.root = new TNode();
16+
}
17+
18+
insert(word: string): void {
19+
let currentNode: TNode = this.root;
20+
21+
for (const ch of word) {
22+
if (currentNode.children.has(ch)) {
23+
currentNode = currentNode.children.get(ch)!;
24+
} else {
25+
const newNode = new TNode();
26+
27+
currentNode.children.set(ch, newNode);
28+
currentNode = currentNode.children.get(ch)!;
29+
}
30+
}
31+
currentNode.isEndOf = true;
32+
}
33+
34+
search(word: string): boolean {
35+
let currentNode = this.root;
36+
37+
for (const ch of word) {
38+
if (!currentNode.children.has(ch)) {
39+
return false;
40+
}
41+
42+
currentNode = currentNode.children.get(ch)!;
43+
}
44+
45+
return currentNode.isEndOf;
46+
}
47+
48+
startsWith(prefix: string): boolean {
49+
let currentNode = this.root;
50+
51+
for (const ch of prefix) {
52+
if (!currentNode.children.has(ch)) {
53+
return false;
54+
}
55+
56+
currentNode = currentNode.children.get(ch)!;
57+
}
58+
59+
return true;
60+
}
61+
}
62+
63+
/**
64+
* Your Trie object will be instantiated and called as such:
65+
* var obj = new Trie()
66+
* obj.insert(word)
67+
* var param_2 = obj.search(word)
68+
* var param_3 = obj.startsWith(prefix)
69+
*/

word-break/hyer0705.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// using set
2+
function wordBreak(s: string, wordDict: string[]): boolean {
3+
const sLen = s.length;
4+
5+
const dp: boolean[] = new Array(sLen + 1).fill(false);
6+
dp[0] = true;
7+
8+
const wordSet = new Set<string>(wordDict);
9+
10+
for (let i = 1; i <= sLen; i++) {
11+
for (let j = 0; j < i; j++) {
12+
if (dp[j] && wordSet.has(s.substring(j, i))) {
13+
dp[i] = true;
14+
break;
15+
}
16+
}
17+
}
18+
19+
return dp[sLen];
20+
}
21+
22+
// using trie
23+
class TNode {
24+
isEndOf: boolean;
25+
children: Map<string, TNode>;
26+
27+
constructor() {
28+
this.isEndOf = false;
29+
this.children = new Map<string, TNode>();
30+
}
31+
}
32+
33+
class Trie {
34+
root: TNode;
35+
36+
constructor() {
37+
this.root = new TNode();
38+
}
39+
40+
insert(word: string): void {
41+
let currentNode = this.root;
42+
43+
for (const ch of word) {
44+
if (!currentNode.children.has(ch)) {
45+
currentNode.children.set(ch, new TNode());
46+
}
47+
48+
currentNode = currentNode.children.get(ch)!;
49+
}
50+
51+
currentNode.isEndOf = true;
52+
}
53+
54+
search(word: string): boolean {
55+
let currentNode = this.root;
56+
57+
for (const ch of word) {
58+
if (!currentNode.children.has(ch)) {
59+
return false;
60+
}
61+
62+
currentNode = currentNode.children.get(ch)!;
63+
}
64+
65+
return currentNode.isEndOf;
66+
}
67+
}
68+
69+
function wordBreak(s: string, wordDict: string[]): boolean {
70+
const sLen = s.length;
71+
72+
const dp: boolean[] = new Array(sLen + 1).fill(false);
73+
dp[0] = true;
74+
75+
const trie = new Trie();
76+
77+
for (const word of wordDict) {
78+
trie.insert(word);
79+
}
80+
81+
for (let i = 1; i <= sLen; i++) {
82+
for (let j = 0; j < i; j++) {
83+
if (dp[j]) {
84+
if (trie.search(s.substring(j, i))) {
85+
dp[i] = true;
86+
break;
87+
}
88+
}
89+
}
90+
}
91+
92+
return dp[sLen];
93+
}

0 commit comments

Comments
 (0)