Skip to content

Commit 879ecd7

Browse files
authored
Merge pull request #476 from Sunjae95/main
[선재] WEEK06 문제 풀이
2 parents ce9ec5d + fffc8e1 commit 879ecd7

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

container-with-most-water/sunjae95.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* 1. brute force -> time limited
5+
* 2. two pointer
6+
*
7+
* n: length of height
8+
* time complexity: O(n)
9+
* space complexity: O(1)
10+
*/
11+
var maxArea = function (height) {
12+
let answer = 0;
13+
let start = 0;
14+
let end = height.length - 1;
15+
16+
while (start !== end) {
17+
const w = end - start;
18+
const h = Math.min(height[start], height[end]);
19+
answer = Math.max(answer, w * h);
20+
if (height[start] >= height[end]) {
21+
end--;
22+
} else if (height[start] < height[end]) {
23+
start++;
24+
}
25+
}
26+
27+
return answer;
28+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* 1. dfs -> time limited
5+
* 2. memoization + dfs
6+
*
7+
* n: length of nums
8+
* time complexity: O(n^2)
9+
* space complexity: O(n)
10+
*/
11+
var lengthOfLIS = function (nums) {
12+
const memo = new Array(nums.length).fill(-1);
13+
let answer = 0;
14+
15+
const dfs = (index) => {
16+
if (memo[index] !== -1) return memo[index];
17+
18+
let maxLength = 1;
19+
20+
for (let i = index + 1; i < nums.length; i++) {
21+
if (nums[index] < nums[i]) {
22+
maxLength = Math.max(maxLength, 1 + dfs(i));
23+
}
24+
}
25+
26+
memo[index] = maxLength;
27+
return maxLength;
28+
};
29+
30+
for (let i = 0; i < nums.length; i++) {
31+
answer = Math.max(answer, dfs(i));
32+
}
33+
34+
return answer;
35+
};

spiral-matrix/sunjae95.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* just implement question
5+
*
6+
* m: length of matrix
7+
* n: length of matrix[i]
8+
* time complexity: O(m * n)
9+
* space complexity: O(m * n)
10+
*/
11+
var spiralOrder = function (matrix) {
12+
let count = matrix.length * matrix[0].length;
13+
let [r, c] = [1, 1];
14+
const answer = [];
15+
const MAX_R = matrix.length + 2;
16+
const MAX_C = matrix[0].length + 2;
17+
const visited = Array.from({ length: MAX_R }, (_, i) => {
18+
return Array.from(
19+
{ length: MAX_C },
20+
(_, j) => i === 0 || i === MAX_R - 1 || j === 0 || j === MAX_C - 1
21+
);
22+
});
23+
24+
while (count--) {
25+
const check = {
26+
left: c >= 1 && visited[r][c - 1],
27+
right: c < MAX_C - 1 && visited[r][c + 1],
28+
top: r >= 1 && visited[r - 1][c],
29+
bottom: r < MAX_R - 1 && visited[r + 1][c],
30+
};
31+
visited[r][c] = true;
32+
answer.push(matrix[r - 1][c - 1]);
33+
34+
if (check.left && check.top && check.bottom) c++;
35+
else if (check.left && check.top && check.right) r++;
36+
else if (check.right && check.top && check.bottom) c--;
37+
else if (check.left && check.right && check.bottom) r--;
38+
else if (check.left && check.top) c++;
39+
else if (check.top && check.right) r++;
40+
else if (check.right && check.bottom) c--;
41+
else if (check.bottom && check.left) r--;
42+
}
43+
44+
return answer;
45+
};

valid-parentheses/sunjae95.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* stack
5+
*
6+
* n: length of s
7+
* time complexity: O(n)
8+
* space complexity: O(n)
9+
*/
10+
var isValid = function (s) {
11+
const stack = [];
12+
13+
for (let i = 0; i < s.length; i++) {
14+
if (s[i] === "(" || s[i] === "{" || s[i] === "[") {
15+
stack.push(s[i]);
16+
continue;
17+
}
18+
19+
const top = stack.length ? stack[stack.length - 1] : null;
20+
if (top === null) return false;
21+
22+
if (
23+
(top === "(" && s[i] === ")") ||
24+
(top === "{" && s[i] === "}") ||
25+
(top === "[" && s[i] === "]")
26+
) {
27+
stack.pop();
28+
} else return false;
29+
}
30+
31+
return !stack.length;
32+
};

0 commit comments

Comments
 (0)