This file contains hidden or 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
public class Solution { | |
public String mirroring(String s) { | |
String x = s.substring(0, (s.length()) / 2); | |
return x + (s.length() % 2 == 1 ? s.charAt(s.length() / 2) : "") + new StringBuilder(x).reverse().toString(); | |
} | |
public String nearestPalindromic(String n) { | |
if (n.equals("1")) | |
return "0"; | |
String a = mirroring(n); |
This file contains hidden or 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
public class Solution { | |
public int findMinMoves(int[] machines) { | |
int total = 0; | |
for(int i: machines) | |
total += i; | |
if(total%machines.length != 0) | |
return -1; | |
int avg = total/machines.length, cnt = 0, max = 0; | |
for(int load: machines){ |
This file contains hidden or 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
class Solution { | |
// O(9^n) O(1) | |
public char[][] updateBoard(char[][] board, int[] click) { | |
int m = board.length; | |
int n = board[0].length; | |
if (click.length < 2) return board; | |
int i = click[0]; | |
int j = click[1]; | |
This file contains hidden or 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
class Solution { | |
// O(n^2) O(n^2) | |
public int countSubstrings(String s) { | |
int n = s.length(); | |
int[][] dp = new int[n][n]; | |
int res = 0; | |
for (int i = n - 1; i >= 0; i--) { | |
dp[i][i] = 1; |
This file contains hidden or 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
class Solution { | |
// O(2^n) O(n) | |
public int longestPalindromeSubseq(String s) { | |
return helper(0, s.length()-1, s); | |
} | |
private int helper(int left, int right, String s) { | |
if (left == right) return 1; | |
if (left > right) return 0; | |
return s.charAt(left) == s.charAt(right) ? 2 + helper(left+1, right-1, s) : |
This file contains hidden or 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
class Solution { | |
// O(n) O(n) | |
public int[] findFrequentTreeSum(TreeNode root) { | |
HashMap<Integer, Integer> sums = new HashMap<Integer, Integer>(); | |
dfs(root, sums); | |
List<Integer> res = new ArrayList<Integer>(); | |
int max = 0; | |
This file contains hidden or 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
class Solution { | |
// O(n) O(n) | |
public int findPairs(int[] nums, int k) { | |
if (nums.length < 2 || k < 0) return 0; | |
HashSet visited = new HashSet(); | |
HashSet pairs = new HashSet(); | |
for (int n : nums) { | |
if (visited.contains(n+k)) { |
This file contains hidden or 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
public class Solution { | |
class Result { // (size, rangeLower, rangeUpper) -- size of current tree, range of current tree [rangeLower, rangeUpper] | |
int size; | |
int lower; | |
int upper; | |
Result(int size, int lower, int upper) { | |
this.size = size; | |
this.lower = lower; |
This file contains hidden or 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
class Solution { | |
// O(n) o(h) | |
private int max = 0; | |
public int longestConsecutive(TreeNode root) { | |
if (root == null) return 0; | |
dfs(root, 0, root.val); | |
return max; | |
} | |
This file contains hidden or 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
// O(n) O(1) | |
class Solution { | |
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { | |
if (root == null) return null; | |
TreeNode succ = null; | |
Deque<TreeNode> stack = new LinkedList<TreeNode>(); | |
while (!stack.isEmpty() || root != null) { | |
if (root != null) { |