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) | |
public int findLongestChain(int[][] pairs) { | |
if (pairs == null || pairs.length == 0) return 0; | |
Arrays.sort(pairs, (a, b) -> (a[0] - b[0])); | |
int[] dp = new int[pairs.length]; | |
Arrays.fill(dp, 1); | |
for (int i = 0; i < dp.length; i++) { | |
for (int j = 0; j < i; j++) { |
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[] findErrorNums(int[] nums) { | |
boolean[] res = new boolean[nums.length]; | |
int dup = -1; | |
int miss = -1; | |
for (int i = 0 ; i < nums.length; i++) { | |
if (res[nums[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 { | |
public List<List<Integer>> levelOrderBottom(TreeNode root) { | |
List<List<Integer>> res = new LinkedList<List<Integer>>(); | |
if (root == null) return res; | |
Deque<TreeNode> deque = new LinkedList<TreeNode>(); | |
deque.addLast(root); | |
while ( !deque.isEmpty() ) { | |
int n = deque.size(); | |
List<Integer> currentLevel = new ArrayList<Integer>(); |
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 { | |
public boolean checkEqualTree(TreeNode root) { | |
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); | |
int sum = subtreeSum(root, map); | |
if (sum == 0) return map.getOrDefault(sum, 0) > 1; | |
return sum%2 == 0 && map.containsKey(sum/2); | |
} | |
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 { | |
public List<List<Integer>> zigzagLevelOrder(TreeNode root) { | |
Deque<TreeNode> deque = new LinkedList<>(); | |
List<List<Integer>> res = new ArrayList<List<Integer>>(); | |
if (root == null) return res; | |
deque.add(root); | |
while ( !deque.isEmpty() ) { | |
List<Integer> current = new LinkedList<>(); |
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 { | |
public List<List<Integer>> pathSum(TreeNode root, int sum) { | |
List<List<Integer>> res = new ArrayList<List<Integer>>(); | |
List<Integer> tmp = new LinkedList<>(); | |
dfs(root, sum, tmp, res); | |
return res; | |
} | |
private void dfs(TreeNode node, int sum, List<Integer> tmp, List<List<Integer>> res) { | |
if ( node == null ) return; |
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 { | |
public TreeNode buildTree(int[] inorder, int[] postorder) { | |
return dfs(inorder, postorder, inorder.length-1, 0, inorder.length-1); | |
} | |
private TreeNode dfs(int[] inorder, int[] postorder, int postInd, int leftIn, int rightIn) { | |
if ( leftIn > rightIn || postInd < 0 ) return null; | |
int value = postorder[postInd]; | |
TreeNode node = new TreeNode(value); |
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 { | |
public boolean isValidBST(TreeNode root) { | |
return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE); | |
} | |
private boolean dfs(TreeNode node, long left, long right) { | |
if ( node == null ) return true; | |
return left < node.val && right > node.val && dfs(node.left, left, node.val) && dfs(node.right, node.val, right); | |
} |
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 Solution1 { | |
// O(n!) O(n) | |
private static int[] res; | |
private static int count = 0; | |
public int totalNQueens(int n) { | |
if ( n == 0 ) { | |
return 1; | |
} | |
res = new int[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 { | |
private static int[] res; | |
private static List<List<String>> sol; | |
public List<List<String>> solveNQueens(int n) { | |
res = new int[n]; | |
sol = new ArrayList<>(); | |
if (n == 0) { | |
sol.add(new ArrayList<String>()); | |
return sol; |