Skip to content

[ganu] Week6 #886

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 17, 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/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// n: len(height)
// Time complexity: O(n)
// Space complexity: O(1)

/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function (height) {
const n = height.length;
let left = 0;
let right = n - 1;
let answer = 0;

while (left < right) {
const w = right - left;
const h = Math.min(height[left], height[right]);

answer = Math.max(w * h, answer);

if (height[left] < height[right]) {
left++;
} else {
right--;
}
}

return answer;
};
67 changes: 67 additions & 0 deletions design-add-and-search-words-data-structure/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var Node = function () {
this.children = new Map();
this.isEnd = false;
};

var WordDictionary = function () {
this.root = new Node();
};

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

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

current = current.children.get(chr);
}

current.isEnd = true;
};

/**
* @param {string} word
* @return {boolean}
*/
WordDictionary.prototype.search = function (word) {
const stack = [[0, this.root]];

while (stack.length > 0) {
const [index, currentNode] = stack.pop();

if (index === word.length) {
if (currentNode.isEnd) {
return true;
}

continue;
}

if (word[index] === ".") {
for (const [_, child] of currentNode.children) {
stack.push([index + 1, child]);
}

continue;
}

if (currentNode.children.has(word[index])) {
stack.push([index + 1, currentNode.children.get(word[index])]);
}
}

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)
*/
21 changes: 21 additions & 0 deletions longest-increasing-subsequence/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// n: len(nums)
// Time complexity: O(n^2)
// Space complexity: O(n)

/**
* @param {number[]} nums
* @return {number}
*/
var lengthOfLIS = function (nums) {
const dp = Array.from({ length: nums.length }, () => 1);

for (let i = 1; i < nums.length; 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);
};
52 changes: 52 additions & 0 deletions spiral-matrix/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// n: height of matrix, m: width of matrix
// Time complexity: O(n*m)
// Space complexity: O(n+m)
Copy link
Contributor

Choose a reason for hiding this comment

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

혹시 공간복잡도를 이렇게 판단하신 이유가 있으실까요?
answer 배열을 포함하셨다면 O(n*m)이 되어야 할 것 같고, 제외한다면 O(1)으로도 표현할 수 있을 것 같아서요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

말씀해주신 것처럼 answer 배열을 포함을 해서 O(n*m) 으로 판단하였습니다! 어디까지 공간 복잡도로 포함시켜야할지 기준이 약간 모호하다 느껴서요, 현재 저는 input 을 제외한 나머지를 모두 공간 복잡도에 포함시키고 있습니다.

Copy link
Contributor

Choose a reason for hiding this comment

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

아하 그렇다면 Space complexity를 O(n+m)로 기입해주신 부분은 오타일까요..?!


/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function (matrix) {
const n = matrix.length;
const m = matrix[0].length;

// order of direction: East, South, West, North
const dy = [0, 1, 0, -1];
const dx = [1, 0, -1, 0];

let dir = 0;
let y = 0;
let x = 0;

const answer = [];

while (true) {
answer.push(matrix[y][x]);
matrix[y][x] = "";

let ny = y + dy[dir];
let nx = x + dx[dir];

if (ny >= 0 && ny < n && nx >= 0 && nx < m && matrix[ny][nx] !== "") {
y = ny;
x = nx;
continue;
}

// If the new position is out of bounds or already visited, Change direction
dir = (dir + 1) % 4;

ny = y + dy[dir];
nx = x + dx[dir];

// If the changed direction still has a problem, Break the loop
if (ny < 0 || ny >= n || nx < 0 || nx >= m || matrix[ny][nx] === "") {
break;
}

y = ny;
x = nx;
}

return answer;
};
26 changes: 26 additions & 0 deletions valid-parentheses/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// n: len(s)
// Time complexity: O(n)
// Space complexity: O(n)

/**
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
const stack = [];

for (const chr of s) {
if (
(stack.at(-1) === "(" && chr === ")") ||
(stack.at(-1) === "{" && chr === "}") ||
(stack.at(-1) === "[" && chr === "]")
) {
stack.pop();
continue;
}

stack.push(chr);
}

return stack.length === 0;
};
Loading