Skip to content
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

[Tony] WEEK 15 Solutions #606

Merged
merged 5 commits into from
Nov 23, 2024
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
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);
}
}