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
21 changes: 21 additions & 0 deletions container-with-most-water/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function maxArea(height: number[]): number {
const n = height.length;

let maximumArea = 0;
let l = 0;
let r = n - 1;

while (l < r) {
const currentArea = (r - l) * Math.min(height[l], height[r]);

maximumArea = Math.max(maximumArea, currentArea);

if (height[l] > height[r]) {
r--;
} else {
l++;
}
}

return maximumArea;
}
68 changes: 68 additions & 0 deletions design-add-and-search-words-data-structure/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class TNode {
isEndOf: boolean;
children: Map<string, TNode>;

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

class WordDictionary {
private root: TNode;

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

addWord(word: string): void {
let current = this.root;

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

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

current.isEndOf = true;
}

search(word: string): boolean {
const find = (node: TNode, index: number): boolean => {
if (index === word.length) {
if (node.isEndOf) return true;
return false;
}

const currentCh = word[index];

if (currentCh === ".") {
for (const [ch, child] of node.children) {
if (find(child, index + 1)) return true;
}

return false;
} else {
const child = node.children.get(currentCh);
if (child) {
return find(child, index + 1);
}

return false;
}

return false;
};

return find(this.root, 0);
}
}

/**
* Your WordDictionary object will be instantiated and called as such:
* var obj = new WordDictionary()
* obj.addWord(word)
* var param_2 = obj.search(word)
*/
44 changes: 44 additions & 0 deletions longest-increasing-subsequence/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// using binary search, lower bound
function lengthOfLIS(nums: number[]): number {
const n = nums.length;
const sub: number[] = [];

for (const num of nums) {
if (sub.length === 0 || num > sub[sub.length - 1]) {
sub.push(num);
} else if (num <= sub[sub.length - 1]) {
let l = 0;
let r = sub.length - 1;

while (l < r) {
const mid = Math.floor((l + r) / 2);

if (num <= sub[mid]) {
r = mid;
} else {
l = mid + 1;
}
}

sub[l] = num;
}
}

return sub.length;
}

// using dp
function lengthOfLIS(nums: number[]): number {
const n = nums.length;
const dp: number[] = Array(n).fill(1);

for (let i = 1; i < n; i++) {
for (let j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}

return Math.max(...dp);
}
38 changes: 38 additions & 0 deletions spiral-matrix/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function spiralOrder(matrix: number[][]): number[] {
const m = matrix.length;
const n = matrix[0].length;

const spiral: number[] = [];

let top = 0;
let bottom = m - 1;
let left = 0;
let right = n - 1;

// right -> bottom -> left -> top
while (left <= right && top <= bottom) {
for (let i = left; i <= right; i++) {
spiral.push(matrix[top][i]);
}
top++;

for (let i = top; i <= bottom; i++) {
spiral.push(matrix[i][right]);
}
right--;

if (left <= right && top <= bottom) {
for (let i = right; i >= left; i--) {
spiral.push(matrix[bottom][i]);
}
bottom--;

for (let i = bottom; i >= top; i--) {
spiral.push(matrix[i][left]);
}
left++;
}
}

return spiral;
}
20 changes: 20 additions & 0 deletions valid-parentheses/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function isValid(s: string): boolean {
const stack: string[] = [];

const matchMap = new Map<string, string>();
matchMap.set(")", "(");
matchMap.set("]", "[");
matchMap.set("}", "{");

for (const bracket of s) {
if (matchMap.has(bracket)) {
if (stack.length === 0 || matchMap.get(bracket) !== stack.pop()) {
return false;
}
} else {
stack.push(bracket);
}
}

return stack.length === 0;
}