Skip to content

Commit b12687f

Browse files
authored
Merge pull request #884 from GangBean/main
[GangBean] Week 6
2 parents f39ef72 + 4bd04ae commit b12687f

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
public int maxArea(int[] height) {
3+
/**
4+
1. understanding
5+
- with two pair of line, each can contain "min(line1,line2) * (distance)" mount of water
6+
- find maximum amount of water can contain
7+
2. strategy
8+
- brute force
9+
- for each pair of lines, calculate amount and update maximum amount.
10+
- it can takes O(N), where N is the count of lines.
11+
- N is 10^5 at most, so it can takes 10^10, which can takes about 10 seconds
12+
- you need to swap out unnecessary calculation
13+
- so, let's find a unnecessary calculation in this problem.
14+
3. complexity
15+
- time: O(N)
16+
- space: O(1)
17+
*/
18+
int l = 0;
19+
int r = height.length - 1;
20+
int maxAmount = amountOf(height, l, r); // Math.min(height[l], height[r], r-l);
21+
while (l < r) { // O(N)
22+
maxAmount = Math.max(maxAmount, amountOf(height, l, r));
23+
if (height[l] < height[r]) {
24+
l++;
25+
} else if (height[l] > height[r]) {
26+
r--;
27+
} else {
28+
int nextLeftAmount = amountOf(height, l+1, r);
29+
int nextRightAmount = amountOf(height, l, r-1);
30+
if (nextLeftAmount < nextRightAmount) {
31+
r--;
32+
} else {
33+
l++;
34+
}
35+
}
36+
}
37+
38+
return maxAmount;
39+
}
40+
41+
private int amountOf(int[] height, int left, int right) {
42+
return (Math.min(height[left], height[right]) * (right - left));
43+
}
44+
}
45+

valid-parentheses/GangBean.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public boolean isValid(String s) {
3+
/**
4+
1. understanding
5+
- given string contains character which has paired characters.
6+
- validate all given characters in input string has each pair, and valid in order.
7+
2. strategy
8+
- use stack, to save left brackets, and if you encounter right bracket, then pop from stack, and compare two characters are paired.
9+
- if not paired on any right character, or stack is empty then return false.
10+
- all characters are iterated and stack is not empty, then return false.
11+
3. complexity
12+
- time: O(N), N is the length of input string s.
13+
- space: O(N), for stack variable memory.
14+
*/
15+
Stack<Character> leftBracket = new Stack<>();
16+
for (char c : s.toCharArray()) {
17+
if (c == '(' || c == '{' || c == '[') {
18+
// when character is left bracket, then push to stack.
19+
leftBracket.push(c);
20+
} else if (c == ')' || c == '}' || c == ']') {
21+
if (leftBracket.isEmpty()) return false;
22+
char left = leftBracket.pop();
23+
if (isPair(left, c)) continue;
24+
return false;
25+
} else {
26+
throw new RuntimeException(String.format("Not valid input character: %c in input %s", c, s));
27+
}
28+
}
29+
return leftBracket.isEmpty();
30+
}
31+
32+
private boolean isPair(char left, char right) {
33+
return (left == '(' && right == ')')
34+
|| (left == '{' && right == '}')
35+
|| (left == '[' && right == ']');
36+
}
37+
}
38+

0 commit comments

Comments
 (0)