Skip to content

[sora0319] WEEK 06 Solutions #1450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions container-with-most-water/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int maxArea(int[] height) {
int maxArea = 0;
int s = 0, e = height.length - 1;

while (s < e) {
int h = Math.min(height[s], height[e]);
int area = (e - s) * h;
maxArea = Math.max(maxArea, area);

if (height[s] < height[e]) {
s++;
} else {
e--;
}
}

return maxArea;
}
}

54 changes: 54 additions & 0 deletions design-add-and-search-words-data-structure/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class WordDictionary {
private static class TrieNode {
boolean isEnd;
Map<Character, TrieNode> children;

TrieNode() {
this.isEnd = false;
this.children = new HashMap<>();
}
}

private final TrieNode root;

public WordDictionary() {
root = new TrieNode();
}

public void addWord(String word) {
TrieNode node = root;
for (char ch : word.toCharArray()) {
node.children.putIfAbsent(ch, new TrieNode());
node = node.children.get(ch);
}
node.isEnd = true;
}

public boolean search(String word) {
return dfs(root, word, 0);
}

private boolean dfs(TrieNode node, String word, int index) {
if (index == word.length()) {
return node.isEnd;
}

char ch = word.charAt(index);
if (ch == '.') {
for (TrieNode child : node.children.values()) {
if (dfs(child, word, index + 1)) {
return true;
}
}
return false;
}

TrieNode next = node.children.get(ch);
if (next == null) {
return false;
}

return dfs(next, word, index + 1);
}
}

24 changes: 24 additions & 0 deletions longest-increasing-subsequence/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.Arrays;

class SolutionLIS {
public int lengthOfLIS(int[] nums) {
int n = nums.length;
int[] dp = new int[n];
Arrays.fill(dp, 1);

for (int current = 1; current < n; current++) {
for (int prev = 0; prev < current; prev++) {
if (nums[prev] < nums[current]) {
dp[current] = Math.max(dp[current], dp[prev] + 1);
}
}
}

int maxLength = 0;
for (int len : dp) {
maxLength = Math.max(maxLength, len);
}
return maxLength;
}
}

42 changes: 42 additions & 0 deletions spiral-matrix/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.*;

class SolutionSpiral {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<>();

if (matrix == null || matrix.length == 0) return result;

int top = 0, bottom = matrix.length - 1;
int left = 0, right = matrix[0].length - 1;

while (top <= bottom && left <= right) {
// Traverse from left to right
for (int col = left; col <= right; col++) {
result.add(matrix[top][col]);
}
top++;

if (top > bottom) break;

for (int row = top; row <= bottom; row++) {
result.add(matrix[row][right]);
}
right--;

if (left > right) break;

for (int col = right; col >= left; col--) {
result.add(matrix[bottom][col]);
}
bottom--;

for (int row = bottom; row >= top; row--) {
result.add(matrix[row][left]);
}
left++;
}

return result;
}
}

30 changes: 30 additions & 0 deletions valid-parentheses/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public boolean isValid(String s) {
Stack<Character> checking = new Stack<>();

for(char c : s.toCharArray()){
if(c == '(' || c == '{' || c == '['){
checking.push(c);
continue;
}
if(checking.empty()) return false;

if(c == ')' && checking.peek() == '('){
checking.pop();
continue;
}
if(c == '}' && checking.peek() == '{'){
checking.pop();
continue;
}
if(c == ']' && checking.peek() == '['){
checking.pop();
continue;
}
return false;
}
if(!checking.empty()) return false;
return true;
}
}