Skip to content

Commit a453874

Browse files
authored
Merge pull request DaleStudy#2526 from reeseo3o/main
[reeseo3o] WEEK 06 Solutions
2 parents 4fb656c + 66bba72 commit a453874

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Brute Force
2+
// Time Complexity: O(n^2)
3+
// Space Complexity: O(1)
4+
const maxAreaBrute = (height) => {
5+
let maxWater = 0;
6+
for (let i = 0; i < height.length; i++) {
7+
for (let j = i + 1; j < height.length; j++) {
8+
const area = Math.min(height[i], height[j]) * (j - i);
9+
maxWater = Math.max(maxWater, area);
10+
}
11+
}
12+
return maxWater;
13+
};
14+
15+
// Two Pointer
16+
// Time Complexity: O(n)
17+
// Space Complexity: O(1)
18+
const maxArea = (height) => {
19+
let left = 0;
20+
let right = height.length - 1;
21+
let maxWater = 0;
22+
23+
while (left < right) {
24+
const area = Math.min(height[left], height[right]) * (right - left);
25+
26+
maxWater = Math.max(maxWater, area);
27+
28+
if (height[left] < height[right]) {
29+
left++;
30+
} else {
31+
right--;
32+
}
33+
}
34+
35+
return maxWater;
36+
};

valid-parentheses/reeseo3o.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const isValid = (s) => {
2+
const stack = [];
3+
const map = {
4+
"(": ")",
5+
"[": "]",
6+
"{": "}"
7+
};
8+
9+
for (const char of s) {
10+
if (map[char]) {
11+
console.log(char);
12+
stack.push(char);
13+
} else {
14+
if (stack.length === 0 || map[stack.pop()] !== char) {
15+
return false;
16+
}
17+
}
18+
}
19+
return stack.length === 0;
20+
}

0 commit comments

Comments
 (0)