Skip to content

[Jeehay28] WEEK 06 #891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 18, 2025
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
29 changes: 29 additions & 0 deletions container-with-most-water/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @param {number[]} height
* @return {number}
*/

// Time Complexity: O(n)
// Space Complexity: O(1)
var maxArea = function (height) {
let start = 0;
let end = height.length - 1;
let maxArea = 0;

while (start < end) {
const area = (end - start) * Math.min(height[start], height[end]);
maxArea = Math.max(area, maxArea);

// The shorter height limits the area.
// By moving the pointer associated with the shorter height,
// the algorithm maximizes the chance of finding a taller line that can increase the area.
// This is the essence of the two-pointer strategy for the container problem.
if (height[start] < height[end]) {
start += 1;
} else {
end -= 1;
}
}
return maxArea;
};

187 changes: 187 additions & 0 deletions design-add-and-search-words-data-structure/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/**
* ==================== Complexity Summary ====================
* | Operation | Time Complexity | Space Complexity |
* |------------|------------------------|--------------------|
* | addWord | O(n) | O(n) |
* | search | O(n) (no '.') | O(n) |
* | | O(k^n) (with '.') | O(n) |
* ============================================================
*
* Notes:
* - n: Length of the word being added/searched.
* - k: Average branching factor (number of children per node).
* - Worst-case time complexity for `search` with '.' can grow exponentially
* when many wildcards are present in the word.
*/

var WordDictionary = function () {
this.root = { $: false };
};

/**
* @param {string} word
* @return {void}
*/
WordDictionary.prototype.addWord = function (word) {
let node = this.root;

for (char of word) {
if (!node[char]) {
node[char] = { $: false };
}
node = node[char];
}

node["$"] = true;
};

/**
* @param {string} word
* @return {boolean}
*/
WordDictionary.prototype.search = function (word) {
const dfs = (node, index) => {
// Base case: If we've reached the end of the word
if (index === word.length) {
return node["$"] === true;
}

const char = word[index];

// If the character is not a wildcard
if (char !== ".") {
if (node[char]) {
return dfs(node[char], index + 1); // Continue DFS
} else {
return false; // Character not found
}
}

// If the character is a wildcard
for (const key of Object.keys(node).filter((key) => key !== "$")) {
if (dfs(node[key], index + 1)) {
return true; // Return true if any child path matches
}
// Why we need to check if the recursive call to dfs returns true or false:
// 1) Early termination of recursion when a match is found:
// - Once we find a valid match, there's no need to explore other branches.
// 2) Proper propagation of the result back up the recursion stack:
// - A valid match found in a deeper level of recursion needs to return true
// all the way back to the initial search call to indicate success.
}

return false; // No paths matched for the wildcard
};

return dfs(this.root, 0); // Start DFS from the root node
};

/**
* Your WordDictionary object will be instantiated and called as such:
* var obj = new WordDictionary()
* obj.addWord(word)
* var param_2 = obj.search(word)
*/

// ***** second trial *****
// var WordDictionary = function () {
// this.root = { $: true };
// };

// /**
// * @param {string} word
// * @return {void}
// */
// WordDictionary.prototype.addWord = function (word) {
// const dfs = (node, index) => {
// // exit
// if (index === word.length) {
// node["$"] = true;
// return;
// }

// // add .
// if (!node["."]) {
// node["."] = { $: false };
// }
// dfs(node["."], index + 1);

// // add each character
// const temp = word[index];

// if (!node[temp]) {
// node[temp] = { $: false };
// }
// dfs(node[temp], index + 1);
// };

// dfs(this.root, 0);
// };

// /**
// * @param {string} word
// * @return {boolean}
// */
// WordDictionary.prototype.search = function (word) {
// let node = this.root;

// for (char of word) {
// if (!node[char]) {
// return false;
// }
// node = node[char];
// }

// return node["$"];
// };

/**
* Your WordDictionary object will be instantiated and called as such:
* var obj = new WordDictionary()
* obj.addWord(word)
* var param_2 = obj.search(word)
*/

// ***** first trial *****
// var WordDictionary = function () {
// this.root = new Set();
// };

// /**
// * @param {string} word
// * @return {void}
// */
// WordDictionary.prototype.addWord = function (word) {
// this.root.add(word);
// };

// /**
// * @param {string} word
// * @return {boolean}
// */
// WordDictionary.prototype.search = function (word) {
// if (this.root.size === 0) {
// return false;
// } else {
// for (dict of this.root) {
// if (dict.length === word.length) {
// const found = word
// .split("")
// .every((str, index) => str === "." || dict[index] === str);
// if (found) {
// return true;
// }
// } else {
// continue;
// }
// }
// 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)
*/
26 changes: 26 additions & 0 deletions longest-increasing-subsequence/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {number[]} nums
* @return {number}
*/

// Time Complexity: O(n^)
// Space Complexity: O(n)
var lengthOfLIS = function (nums) {
if (nums.length === 0) {
return 0;
}

let dp = new Array(nums.length).fill(1); // dp[i] will be the length of LIS ending at i

for (let i = 1; i < nums.length; i++) {
for (let j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
// Strictly increasing condition
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}

return Math.max(...dp); // The length of the longest subsequence
};

66 changes: 66 additions & 0 deletions spiral-matrix/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Time Complexity: O(m * n), where m is the number of rows and n is the number of columns in the matrix,
// because every element is processed once.

// Space Complexity: O(m * n), where m is the number of rows and n is the number of columns in the matrix,
// because we store all matrix elements in the result array.

/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function (matrix) {

let topRow = 0;
let bottomRow = matrix.length - 1;
let leftCol = 0;
let rightCol = matrix[0].length - 1;
let result = [];

while (topRow <= bottomRow && leftCol <= rightCol) {
// move to the right
for (let col = leftCol; col <= rightCol; col++) {
result.push(matrix[topRow][col]);
}

topRow += 1;

if (topRow > bottomRow) {
break;
}

// move down
for (let row = topRow; row <= bottomRow; row++) {
result.push(matrix[row][rightCol]);
}

rightCol -= 1;

if (leftCol > rightCol) {
break;
}

// move to the left
for (let col = rightCol; col >= leftCol; col--) {
result.push(matrix[bottomRow][col]);
}

bottomRow -= 1;
if (topRow > bottomRow) {
break;
}

// move up
for (let row = bottomRow; row >= topRow; row--) {
result.push(matrix[row][leftCol]);
}

leftCol += 1;

if (leftCol > rightCol) {
break;
}
}

return result;
};

35 changes: 35 additions & 0 deletions valid-parentheses/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @param {string} s
* @return {boolean}
*/

// Time Complexity: O(n)
// Space Complexity: O(n)
var isValid = function (s) {
const obj = {
"(" : ")",
"{" : "}",
"[" : "]",
};

let stack = [];

for (any of s) {
// open bracket
if (obj[any]) {
stack.push(any);
} else {
// close bracket
if (stack.length === 0) {
return false;
} else if (obj[stack[stack.length - 1]] !== any) {
return false;
} else {
stack.pop();
}
}
}
return stack.length === 0 ? true : false;
Copy link
Contributor

Choose a reason for hiding this comment

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

return stack.length === 0 만 해도 true, false 로 반환되지 않을까요? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

아, 그렇네요. 감사합니다.

};


Loading