-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #606 from TonyKim9401/main
[Tony] WEEK 15 Solutions
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |