Skip to content

Commit f670e4a

Browse files
authored
Merge pull request #1870 from hyer0705/main
2 parents 75fe00c + 54bab01 commit f670e4a

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function maxArea(height: number[]): number {
2+
const n = height.length;
3+
4+
let maximumArea = 0;
5+
let l = 0;
6+
let r = n - 1;
7+
8+
while (l < r) {
9+
const currentArea = (r - l) * Math.min(height[l], height[r]);
10+
11+
maximumArea = Math.max(maximumArea, currentArea);
12+
13+
if (height[l] > height[r]) {
14+
r--;
15+
} else {
16+
l++;
17+
}
18+
}
19+
20+
return maximumArea;
21+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 WordDictionary {
12+
private root: TNode;
13+
14+
constructor() {
15+
this.root = new TNode();
16+
}
17+
18+
addWord(word: string): void {
19+
let current = this.root;
20+
21+
for (const ch of word) {
22+
if (!current.children.has(ch)) {
23+
current.children.set(ch, new TNode());
24+
}
25+
26+
current = current.children.get(ch)!;
27+
}
28+
29+
current.isEndOf = true;
30+
}
31+
32+
search(word: string): boolean {
33+
const find = (node: TNode, index: number): boolean => {
34+
if (index === word.length) {
35+
if (node.isEndOf) return true;
36+
return false;
37+
}
38+
39+
const currentCh = word[index];
40+
41+
if (currentCh === ".") {
42+
for (const [ch, child] of node.children) {
43+
if (find(child, index + 1)) return true;
44+
}
45+
46+
return false;
47+
} else {
48+
const child = node.children.get(currentCh);
49+
if (child) {
50+
return find(child, index + 1);
51+
}
52+
53+
return false;
54+
}
55+
56+
return false;
57+
};
58+
59+
return find(this.root, 0);
60+
}
61+
}
62+
63+
/**
64+
* Your WordDictionary object will be instantiated and called as such:
65+
* var obj = new WordDictionary()
66+
* obj.addWord(word)
67+
* var param_2 = obj.search(word)
68+
*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// using binary search, lower bound
2+
function lengthOfLIS(nums: number[]): number {
3+
const n = nums.length;
4+
const sub: number[] = [];
5+
6+
for (const num of nums) {
7+
if (sub.length === 0 || num > sub[sub.length - 1]) {
8+
sub.push(num);
9+
} else if (num <= sub[sub.length - 1]) {
10+
let l = 0;
11+
let r = sub.length - 1;
12+
13+
while (l < r) {
14+
const mid = Math.floor((l + r) / 2);
15+
16+
if (num <= sub[mid]) {
17+
r = mid;
18+
} else {
19+
l = mid + 1;
20+
}
21+
}
22+
23+
sub[l] = num;
24+
}
25+
}
26+
27+
return sub.length;
28+
}
29+
30+
// using dp
31+
function lengthOfLIS(nums: number[]): number {
32+
const n = nums.length;
33+
const dp: number[] = Array(n).fill(1);
34+
35+
for (let i = 1; i < n; i++) {
36+
for (let j = 0; j < i; j++) {
37+
if (nums[j] < nums[i]) {
38+
dp[i] = Math.max(dp[i], dp[j] + 1);
39+
}
40+
}
41+
}
42+
43+
return Math.max(...dp);
44+
}

spiral-matrix/hyer0705.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function spiralOrder(matrix: number[][]): number[] {
2+
const m = matrix.length;
3+
const n = matrix[0].length;
4+
5+
const spiral: number[] = [];
6+
7+
let top = 0;
8+
let bottom = m - 1;
9+
let left = 0;
10+
let right = n - 1;
11+
12+
// right -> bottom -> left -> top
13+
while (left <= right && top <= bottom) {
14+
for (let i = left; i <= right; i++) {
15+
spiral.push(matrix[top][i]);
16+
}
17+
top++;
18+
19+
for (let i = top; i <= bottom; i++) {
20+
spiral.push(matrix[i][right]);
21+
}
22+
right--;
23+
24+
if (left <= right && top <= bottom) {
25+
for (let i = right; i >= left; i--) {
26+
spiral.push(matrix[bottom][i]);
27+
}
28+
bottom--;
29+
30+
for (let i = bottom; i >= top; i--) {
31+
spiral.push(matrix[i][left]);
32+
}
33+
left++;
34+
}
35+
}
36+
37+
return spiral;
38+
}

valid-parentheses/hyer0705.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function isValid(s: string): boolean {
2+
const stack: string[] = [];
3+
4+
const matchMap = new Map<string, string>();
5+
matchMap.set(")", "(");
6+
matchMap.set("]", "[");
7+
matchMap.set("}", "{");
8+
9+
for (const bracket of s) {
10+
if (matchMap.has(bracket)) {
11+
if (stack.length === 0 || matchMap.get(bracket) !== stack.pop()) {
12+
return false;
13+
}
14+
} else {
15+
stack.push(bracket);
16+
}
17+
}
18+
19+
return stack.length === 0;
20+
}

0 commit comments

Comments
 (0)