Skip to content

Commit 849650a

Browse files
authored
Merge pull request #1450 from sora0319/main
[sora0319] WEEK 06 Solutions
2 parents be17893 + a98baee commit 849650a

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int maxArea(int[] height) {
3+
int maxArea = 0;
4+
int s = 0, e = height.length - 1;
5+
6+
while (s < e) {
7+
int h = Math.min(height[s], height[e]);
8+
int area = (e - s) * h;
9+
maxArea = Math.max(maxArea, area);
10+
11+
if (height[s] < height[e]) {
12+
s++;
13+
} else {
14+
e--;
15+
}
16+
}
17+
18+
return maxArea;
19+
}
20+
}
21+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class WordDictionary {
2+
private static class TrieNode {
3+
boolean isEnd;
4+
Map<Character, TrieNode> children;
5+
6+
TrieNode() {
7+
this.isEnd = false;
8+
this.children = new HashMap<>();
9+
}
10+
}
11+
12+
private final TrieNode root;
13+
14+
public WordDictionary() {
15+
root = new TrieNode();
16+
}
17+
18+
public void addWord(String word) {
19+
TrieNode node = root;
20+
for (char ch : word.toCharArray()) {
21+
node.children.putIfAbsent(ch, new TrieNode());
22+
node = node.children.get(ch);
23+
}
24+
node.isEnd = true;
25+
}
26+
27+
public boolean search(String word) {
28+
return dfs(root, word, 0);
29+
}
30+
31+
private boolean dfs(TrieNode node, String word, int index) {
32+
if (index == word.length()) {
33+
return node.isEnd;
34+
}
35+
36+
char ch = word.charAt(index);
37+
if (ch == '.') {
38+
for (TrieNode child : node.children.values()) {
39+
if (dfs(child, word, index + 1)) {
40+
return true;
41+
}
42+
}
43+
return false;
44+
}
45+
46+
TrieNode next = node.children.get(ch);
47+
if (next == null) {
48+
return false;
49+
}
50+
51+
return dfs(next, word, index + 1);
52+
}
53+
}
54+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Arrays;
2+
3+
class SolutionLIS {
4+
public int lengthOfLIS(int[] nums) {
5+
int n = nums.length;
6+
int[] dp = new int[n];
7+
Arrays.fill(dp, 1);
8+
9+
for (int current = 1; current < n; current++) {
10+
for (int prev = 0; prev < current; prev++) {
11+
if (nums[prev] < nums[current]) {
12+
dp[current] = Math.max(dp[current], dp[prev] + 1);
13+
}
14+
}
15+
}
16+
17+
int maxLength = 0;
18+
for (int len : dp) {
19+
maxLength = Math.max(maxLength, len);
20+
}
21+
return maxLength;
22+
}
23+
}
24+

spiral-matrix/sora0319.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.*;
2+
3+
class SolutionSpiral {
4+
public List<Integer> spiralOrder(int[][] matrix) {
5+
List<Integer> result = new ArrayList<>();
6+
7+
if (matrix == null || matrix.length == 0) return result;
8+
9+
int top = 0, bottom = matrix.length - 1;
10+
int left = 0, right = matrix[0].length - 1;
11+
12+
while (top <= bottom && left <= right) {
13+
// Traverse from left to right
14+
for (int col = left; col <= right; col++) {
15+
result.add(matrix[top][col]);
16+
}
17+
top++;
18+
19+
if (top > bottom) break;
20+
21+
for (int row = top; row <= bottom; row++) {
22+
result.add(matrix[row][right]);
23+
}
24+
right--;
25+
26+
if (left > right) break;
27+
28+
for (int col = right; col >= left; col--) {
29+
result.add(matrix[bottom][col]);
30+
}
31+
bottom--;
32+
33+
for (int row = bottom; row >= top; row--) {
34+
result.add(matrix[row][left]);
35+
}
36+
left++;
37+
}
38+
39+
return result;
40+
}
41+
}
42+

valid-parentheses/sora0319.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public boolean isValid(String s) {
3+
Stack<Character> checking = new Stack<>();
4+
5+
for(char c : s.toCharArray()){
6+
if(c == '(' || c == '{' || c == '['){
7+
checking.push(c);
8+
continue;
9+
}
10+
if(checking.empty()) return false;
11+
12+
if(c == ')' && checking.peek() == '('){
13+
checking.pop();
14+
continue;
15+
}
16+
if(c == '}' && checking.peek() == '{'){
17+
checking.pop();
18+
continue;
19+
}
20+
if(c == ']' && checking.peek() == '['){
21+
checking.pop();
22+
continue;
23+
}
24+
return false;
25+
}
26+
if(!checking.empty()) return false;
27+
return true;
28+
}
29+
}
30+

0 commit comments

Comments
 (0)