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
public boolean isValidBST(TreeNode root) | |
{ | |
Integer[] prev = new Integer[1]; | |
return isValidBST(root, prev); | |
} | |
public boolean isValidBST(TreeNode root, Integer[] prev) | |
{ | |
boolean left = true; | |
if (root.left != null) |
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
public int[] topKFrequent(int[] nums, int k) { | |
Map<Integer, Integer> valueToCount = new HashMap<>(); | |
int max = 0; | |
for (int num : nums) { | |
int count = valueToCount.getOrDefault(num, 0) + 1; | |
valueToCount.put(num, count); | |
max = Math.max(max, count); | |
} | |
System.out.println(valueToCount); |
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
int pos = max; | |
int[] answer = new int[k]; | |
int i = 0; | |
while (pos >= 0) { | |
for (int value : buckets.get(pos)) { | |
answer[i++] = value; | |
if (i == k) { | |
return answer; | |
} | |
} |
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
for (Map.Entry<Integer, Integer> entry : valueToCount.entrySet()) { | |
buckets.get(entry.getValue()).add(entry.getKey()); | |
} |
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
List<List<Integer>> buckets = new ArrayList<>(); | |
for (int i = 0; i <= max; i++) { | |
buckets.add(new ArrayList<>()); | |
} |
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
Map<Integer, Integer> valueToCount = new HashMap<>(); | |
int max = 0; | |
for (int num : nums) { | |
int count = valueToCount.getOrDefault(num, 0) + 1; | |
valueToCount.put(num, count); | |
max = Math.max(max, count); | |
} |
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
public int[] topKFrequent(int[] nums, int k) { | |
Map<Integer, Integer> valueToCount = new HashMap<>(); | |
for (int num : nums) { | |
int count = valueToCount.getOrDefault(num, 0) + 1; | |
valueToCount.put(num, count); | |
} | |
int pos = 0; | |
int[] answer = new int[k]; | |
PriorityQueue<Integer> pq = new PriorityQueue<>((a,b) -> |
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
int pos = 0; | |
int[] answer = new int[k]; | |
PriorityQueue<Integer> pq = new PriorityQueue<>((a,b) -> | |
valueToCount.get(b) - valueToCount.get(a)); | |
pq.addAll(valueToCount.keySet()); | |
while (k-- > 0) | |
{ | |
answer[pos++] = pq.remove(); | |
} |
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
PriorityQueue<Integer> pq = new PriorityQueue<>((a,b) -> | |
valueToCount.get(b) - valueToCount.get(a)); |
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
Map<Integer, Integer> valueToCount = new HashMap<>(); | |
for (int num : nums) { | |
int count = valueToCount.getOrDefault(num, 0) + 1; | |
valueToCount.put(num, count); | |
} |
NewerOlder