Skip to content

Commit

Permalink
Merge pull request #468 from HC-kang/main
Browse files Browse the repository at this point in the history
[강희찬] WEEK 6 Solution
  • Loading branch information
HC-kang authored Sep 20, 2024
2 parents 17504f2 + a33714f commit 189509b
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 0 deletions.
21 changes: 21 additions & 0 deletions container-with-most-water/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* https://leetcode.com/problems/container-with-most-water/
* T.C. O(n)
* S.C. O(1)
*/
function maxArea(height: number[]): number {
let [res, i, j] = [0, 0, height.length - 1];

while (i < j) {
const volume = Math.min(height[i], height[j]) * (j - i);
res = Math.max(res, volume);

if (height[i] < height[j]) {
i++;
} else {
j--;
}
}

return res;
}
102 changes: 102 additions & 0 deletions design-add-and-search-words-data-structure/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* https://leetcode.com/problems/design-add-and-search-words-data-structure
*/
// Using Trie
class WordDictionary {
constructor(private root: Record<string, any> = {}) {}

/**
* T.C. O(L) L: length of a word
* S.C. O(L)
*/
addWord(word: string): void {
let node = this.root;
for (const char of word) {
if (!node[char]) {
node[char] = {};
}
node = node[char];
}
node['isEnd'] = true;
}

/**
* T.C. O(N) - there are only 2 dots in the word(26 * 26 * N)
* S.C. O(N * L) N: number of words, L: length of a word
*/
search(word: string): boolean {
return this.dfs(word, this.root);
}

private dfs(word: string, node: Record<string, any>): boolean {
for (let i = 0; i < word.length; i++) {
if (word[i] === '.') {
for (const key in node) {
if (this.dfs(word.slice(i + 1), node[key])) {
return true;
}
}
return false;
}
if (!node[word[i]]) {
return false;
}
node = node[word[i]];
}
return !!node['isEnd'];
}
}

// Using Array and Set
class WordDictionary {
constructor(
private words: Set<string>[] = Array.from({ length: 25 }, () => new Set())
) {}

/**
* T.C. O(1)
* S.C. O(N * L)
*/
addWord(word: string): void {
this.words[word.length - 1].add(word);
}

/**
* T.C. O(N * L) N: number of words, L: length of a word
* S.C. O(1)
*/
search(word: string): boolean {
const hasDot = word.indexOf('.') !== -1;
const set = this.words[word.length - 1];

if (!hasDot) {
return set.has(word);
}

for (const w of set) {
let i = 0;
while (i < word.length) {
if (word[i] == '.') {
i++;
continue;
}
if (word[i] !== w[i]) {
break;
}
i++;
}

if (i === word.length) {
return true;
}
}
return false;
}
}

/**
* Your WordDictionary object will be instantiated and called as such:
* var obj = new WordDictionary()
* obj.addWord(word)
* var param_2 = obj.search(word)
*/
32 changes: 32 additions & 0 deletions longest-increasing-subsequence/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* https://leetcode.com/problems/longest-increasing-subsequence
* T.C. O(nlogn)
* S.C. O(n)
*/
function lengthOfLIS(nums: number[]): number {
const sub: number[] = [];

function findSlot(num: number): number {
let left = 0;
let right = sub.length - 1;

while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (sub[mid] < num) {
left = mid + 1;
} else {
right = mid - 1;
}
}

return left;
}

for (let i = 0; i < nums.length; i++) {
const num = nums[i];
const slot = findSlot(num);
sub[slot] = num;
}

return sub.length;
}
44 changes: 44 additions & 0 deletions spiral-matrix/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* https://leetcode.com/problems/spiral-matrix
* T.C. O(m * n)
* S.C. O(m * n)
*/
function spiralOrder(matrix: number[][]): number[] {
const clockwise = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
let currentDirection = 0;

const visited = new Array(matrix.length)
.fill(0)
.map(() => new Array(matrix[0].length).fill(false));

const result: number[] = [];

let row = 0;
let col = 0;

while (result.length < matrix.length * matrix[0].length) {
result.push(matrix[row][col]);
visited[row][col] = true;

const nextRow = row + clockwise[currentDirection][0];
const nextCol = col + clockwise[currentDirection][1];

if (
nextRow < 0 || nextRow >= matrix.length ||
nextCol < 0 || nextCol >= matrix[0].length ||
visited[nextRow][nextCol]
) {
currentDirection = (currentDirection + 1) % 4;
}

row += clockwise[currentDirection][0];
col += clockwise[currentDirection][1];
}

return result;
}
17 changes: 17 additions & 0 deletions valid-parentheses/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* https://leetcode.com/problems/valid-parentheses
* T.C. O(n)
* S.C. O(n)
*/
function isValid(s: string): boolean {
const pairs: Record<string, string> = { '{': '}', '[': ']', '(': ')' };
const stack: string[] = [];

if (s.length % 2 == 1) return false;

for (const char of s) {
if (pairs[char]) stack.push(char);
else if (char != pairs[stack.pop()!]) return false;
}
return !stack.length;
}

0 comments on commit 189509b

Please sign in to comment.