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
private double curMinDelta = Double.MAX_VALUE; | |
private int smallestValue = 0; | |
public int closestValue(TreeNode root, double target) { | |
this.searchMin(root, target); | |
return this.smallestValue; | |
} | |
private void searchMin(TreeNode root, double target) { | |
if (root == null) { |
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 List<Integer> closestKValuesUsingPreOrder(TreeNode root, double target, int k) { | |
List<Integer> res = new ArrayList<>(); | |
// PQ by default is ascending order using the element's natural order, so by default, it's a min heap | |
Queue<Node> pq = new PriorityQueue<>(); // max heap | |
this.preOrderTraversal(root, target, pq, k); | |
for (Node n : pq) { | |
res.add(n.val); | |
} |
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
/** | |
this method is also worst case O(N), however, if BST is balanced, this would be O(K + lgN) | |
*/ | |
public List<Integer> closestKValues(TreeNode root, double target, int k) { | |
List<Integer> res = new ArrayList<>(); | |
Stack<Integer> predecessors = new Stack<>(); | |
Stack<Integer> successors = new Stack<>(); | |
this.traverseTreeInorder(root, target, predecessors); |
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
private final int[] xDirection = {1, 0, -1, 0}; | |
private final int[] yDirection = {0, -1, 0, 1}; | |
private final int ONE_MILLION = 1000000; | |
public boolean isEscapePossible(int[][] blocked, int[] source, int[] target) { | |
if (blocked == null || source == null || target == null) { | |
return false; | |
} | |
Set<String> blockLookup = this.indexBlockedMatrixToSet(blocked); |
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
private final int[] xDirection = {1, 0, -1, 0}; | |
private final int[] yDirection = {0, -1, 0, 1}; | |
private final int ONE_MILLION = 1000000; | |
private final int MAX_COUNT_THRESHOLD = 20000; | |
public boolean isEscapePossible(int[][] blocked, int[] source, int[] target) { | |
if (blocked == null || source == null || target == null) { | |
return false; | |
} |
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
// I recommend this solution: just need map to keep the mapping relationship | |
public boolean wordPattern(String pattern, String str) { | |
if (pattern == null || pattern.length() == 0 || str == null || str.length() == 0) { | |
return false; | |
} | |
String[] strs = str.trim().split(" "); | |
if (pattern.length() != strs.length) { | |
return false; |
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
// Reference: https://leetcode.com/problems/word-pattern/discuss/73402/8-lines-simple-Java | |
public boolean wordPattern(String pattern, String str) { | |
String[] words = str.split(" "); | |
if (words.length != pattern.length()) | |
return false; | |
Map index = new HashMap(); | |
for (Integer i=0; i<words.length; ++i) | |
if (index.put(pattern.charAt(i), i) != index.put(words[i], i)) | |
return false; | |
return true; |
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
// This way will also work, just a little bit more work by encoding each string into the same one | |
// Kinda similar to the isomorphic string | |
public boolean wordPatternEncoding(String pattern, String str) { | |
if (str == null || str.isEmpty() || pattern == null || pattern.isEmpty()) { | |
return false; | |
} | |
String[] s = str.split(" "); | |
if (pattern.length() != s.length) { | |
return false; |
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 boolean isIsomorphic(String a, String b) { | |
if (a == null || b == null || a.length() != b.length()) { | |
return false; | |
} | |
Map<Character, Character> lookup = new HashMap<>(); | |
Set<Character> dupSet = new HashSet<>(); | |
for (int i = 0; i < a.length(); i++) { | |
char c1 = a.charAt(i); | |
char c2 = b.charAt(i); |
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 boolean wordPatternMatch(String pattern, String str) { | |
if (pattern == null || str == null) { | |
return false; | |
} | |
Map<Character, String> lookup = new HashMap<>(); | |
Set<String> dup = new HashSet<>(); | |
return this.isMatch(pattern, str, lookup, dup); | |
} |