Skip to content

Commit

Permalink
Merge pull request #606 from TonyKim9401/main
Browse files Browse the repository at this point in the history
[Tony] WEEK 15 Solutions
  • Loading branch information
TonyKim9401 authored Nov 23, 2024
2 parents ac2a2e9 + 9c14f52 commit d33ff58
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
31 changes: 31 additions & 0 deletions longest-palindromic-substring/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// TC: O(n^2)
// for loop & nested while loop => total O(n^2)
// SC: O(1)
// uses only constant space => O(1)
class Solution {
public String longestPalindrome(String s) {
int right = 0;
int left = 0;

for (int i = 0; i < s.length(); i++) {
int even = pad(s, i, i);
int odd = pad(s, i, i+1);
int max = Math.max(even, odd);

if (max > (right - left)) {
right = i + (max + 1) / 2;
left = i - max / 2;
}
}

return s.substring(left, right+1);
}

private int pad(String s, int start, int end) {
while (start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)) {
start -= 1;
end += 1;
}
return start - end;
}
}
19 changes: 19 additions & 0 deletions rotate-image/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// TC: O(n^2)
// visit all elements with nested for-loop
// SC: O(1)
// constant space complexity
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;

for (int i = 0; i < (n + 1) / 2; i++) {
for (int j = 0; j < n / 2; j++) {
int temp = matrix[n-1-j][i];
matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
matrix[j][n-1-i] = matrix[i][j];
matrix[i][j] = temp;
}
}
}
}
25 changes: 25 additions & 0 deletions subtree-of-another-tree/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// TC: O(n * m)
// need to check all nodes and subRoot nodes in the worst case
// SC: O(h1 + h2)
// the high of root = h1 + the high of subRoot h2 => O(h1 + h2)
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if (root == null) return false;

if (sameCheck(root, subRoot)) return true;

return isSubtree(root.left, subRoot) ||
isSubtree(root.right, subRoot);
}

private boolean sameCheck(TreeNode root, TreeNode subRoot) {
if (root == null || subRoot == null) {
return root == null && subRoot == null;
}

if (root.val != subRoot.val) return false;

return sameCheck(root.left, subRoot.left) &&
sameCheck(root.right, subRoot.right);
}
}
17 changes: 17 additions & 0 deletions validate-binary-search-tree/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// TC: O(n)
// visit all nodes to compare
// SC: O(n)
// recursion requires O(n) SC in the worst case
class Solution {
public boolean isValidBST(TreeNode root) {
return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE);
}

private boolean dfs(TreeNode node, long min, long max) {
if (node == null) return true;

if (node.val <= min || node.val >= max) return false;
return dfs(node.left, min, node.val) &&
dfs(node.right, node.val, max);
}
}

0 comments on commit d33ff58

Please sign in to comment.