Skip to content

Commit

Permalink
Merge pull request #476 from Sunjae95/main
Browse files Browse the repository at this point in the history
[선재] WEEK06 문제 풀이
  • Loading branch information
Sunjae95 authored Sep 21, 2024
2 parents ce9ec5d + fffc8e1 commit 879ecd7
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
28 changes: 28 additions & 0 deletions container-with-most-water/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @description
* brainstorming:
* 1. brute force -> time limited
* 2. two pointer
*
* n: length of height
* time complexity: O(n)
* space complexity: O(1)
*/
var maxArea = function (height) {
let answer = 0;
let start = 0;
let end = height.length - 1;

while (start !== end) {
const w = end - start;
const h = Math.min(height[start], height[end]);
answer = Math.max(answer, w * h);
if (height[start] >= height[end]) {
end--;
} else if (height[start] < height[end]) {
start++;
}
}

return answer;
};
35 changes: 35 additions & 0 deletions longest-increasing-subsequence/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @description
* brainstorming:
* 1. dfs -> time limited
* 2. memoization + dfs
*
* n: length of nums
* time complexity: O(n^2)
* space complexity: O(n)
*/
var lengthOfLIS = function (nums) {
const memo = new Array(nums.length).fill(-1);
let answer = 0;

const dfs = (index) => {
if (memo[index] !== -1) return memo[index];

let maxLength = 1;

for (let i = index + 1; i < nums.length; i++) {
if (nums[index] < nums[i]) {
maxLength = Math.max(maxLength, 1 + dfs(i));
}
}

memo[index] = maxLength;
return maxLength;
};

for (let i = 0; i < nums.length; i++) {
answer = Math.max(answer, dfs(i));
}

return answer;
};
45 changes: 45 additions & 0 deletions spiral-matrix/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @description
* brainstorming:
* just implement question
*
* m: length of matrix
* n: length of matrix[i]
* time complexity: O(m * n)
* space complexity: O(m * n)
*/
var spiralOrder = function (matrix) {
let count = matrix.length * matrix[0].length;
let [r, c] = [1, 1];
const answer = [];
const MAX_R = matrix.length + 2;
const MAX_C = matrix[0].length + 2;
const visited = Array.from({ length: MAX_R }, (_, i) => {
return Array.from(
{ length: MAX_C },
(_, j) => i === 0 || i === MAX_R - 1 || j === 0 || j === MAX_C - 1
);
});

while (count--) {
const check = {
left: c >= 1 && visited[r][c - 1],
right: c < MAX_C - 1 && visited[r][c + 1],
top: r >= 1 && visited[r - 1][c],
bottom: r < MAX_R - 1 && visited[r + 1][c],
};
visited[r][c] = true;
answer.push(matrix[r - 1][c - 1]);

if (check.left && check.top && check.bottom) c++;
else if (check.left && check.top && check.right) r++;
else if (check.right && check.top && check.bottom) c--;
else if (check.left && check.right && check.bottom) r--;
else if (check.left && check.top) c++;
else if (check.top && check.right) r++;
else if (check.right && check.bottom) c--;
else if (check.bottom && check.left) r--;
}

return answer;
};
32 changes: 32 additions & 0 deletions valid-parentheses/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @description
* brainstorming:
* stack
*
* n: length of s
* time complexity: O(n)
* space complexity: O(n)
*/
var isValid = function (s) {
const stack = [];

for (let i = 0; i < s.length; i++) {
if (s[i] === "(" || s[i] === "{" || s[i] === "[") {
stack.push(s[i]);
continue;
}

const top = stack.length ? stack[stack.length - 1] : null;
if (top === null) return false;

if (
(top === "(" && s[i] === ")") ||
(top === "{" && s[i] === "}") ||
(top === "[" && s[i] === "]")
) {
stack.pop();
} else return false;
}

return !stack.length;
};

0 comments on commit 879ecd7

Please sign in to comment.