diff --git a/container-with-most-water/sora0319.java b/container-with-most-water/sora0319.java new file mode 100644 index 000000000..ff955e9f0 --- /dev/null +++ b/container-with-most-water/sora0319.java @@ -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; + } +} + diff --git a/design-add-and-search-words-data-structure/sora0319.java b/design-add-and-search-words-data-structure/sora0319.java new file mode 100644 index 000000000..a55ef19ef --- /dev/null +++ b/design-add-and-search-words-data-structure/sora0319.java @@ -0,0 +1,54 @@ +class WordDictionary { + private static class TrieNode { + boolean isEnd; + Map 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); + } +} + diff --git a/longest-increasing-subsequence/sora0319.java b/longest-increasing-subsequence/sora0319.java new file mode 100644 index 000000000..467fc3643 --- /dev/null +++ b/longest-increasing-subsequence/sora0319.java @@ -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; + } +} + diff --git a/spiral-matrix/sora0319.java b/spiral-matrix/sora0319.java new file mode 100644 index 000000000..f3726ee39 --- /dev/null +++ b/spiral-matrix/sora0319.java @@ -0,0 +1,42 @@ +import java.util.*; + +class SolutionSpiral { + public List spiralOrder(int[][] matrix) { + List 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; + } +} + diff --git a/valid-parentheses/sora0319.java b/valid-parentheses/sora0319.java new file mode 100644 index 000000000..0947bd835 --- /dev/null +++ b/valid-parentheses/sora0319.java @@ -0,0 +1,30 @@ +class Solution { + public boolean isValid(String s) { + Stack 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; + } +} +