Skip to content

Commit bf24cec

Browse files
authored
Merge pull request DaleStudy#908 from taewanseoul/main
2 parents 4039896 + 983ddb4 commit bf24cec

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 11. Container With Most Water
3+
* You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
4+
* Find two lines that together with the x-axis form a container, such that the container contains the most water.
5+
* Return the maximum amount of water a container can store.
6+
*
7+
* Notice that you may not slant the container.
8+
*
9+
* https://leetcode.com/problems/container-with-most-water/description/
10+
*
11+
*/
12+
13+
// O(n) time
14+
// O(1) space
15+
function maxArea(height: number[]): number {
16+
let l = 0;
17+
let r = height.length - 1;
18+
let max = 0;
19+
20+
while (l < r) {
21+
max = Math.max(max, Math.min(height[l], height[r]) * (r - l));
22+
23+
if (height[l] < height[r]) {
24+
l++;
25+
} else {
26+
r--;
27+
}
28+
}
29+
30+
return max;
31+
}

valid-parentheses/taewanseoul.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 20. Valid Parentheses
3+
* Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
4+
*
5+
* An input string is valid if:
6+
* 1. Open brackets must be closed by the same type of brackets.
7+
* 2. Open brackets must be closed in the correct order.
8+
* 3. Every close bracket has a corresponding open bracket of the same type.
9+
*
10+
* https://leetcode.com/problems/valid-parentheses/
11+
*
12+
*/
13+
14+
// O(n^2) time
15+
// O(1) space
16+
function isValid(s: string): boolean {
17+
if (s.length % 2 === 1) return false;
18+
19+
while (
20+
s.indexOf("()") !== -1 ||
21+
s.indexOf("{}") !== -1 ||
22+
s.indexOf("[]") !== -1
23+
) {
24+
s = s.replace("()", "");
25+
s = s.replace("{}", "");
26+
s = s.replace("[]", "");
27+
}
28+
29+
return s === "";
30+
}

0 commit comments

Comments
 (0)