Created
April 27, 2019 07:59
-
-
Save xjj59307/79bffe75c10c3321c938761117338b68 to your computer and use it in GitHub Desktop.
659. Split Array into Consecutive Subsequences
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, PriorityQueue<Integer>> valToSize = new HashMap<>(); | |
private PriorityQueue<Integer> getOrPut(int num) { | |
PriorityQueue<Integer> sizeSet = valToSize.get(num); | |
if (sizeSet == null) valToSize.put(num, new PriorityQueue<>()); | |
return valToSize.get(num); | |
} | |
public boolean isPossible(int[] nums) { | |
for (int num : nums) { | |
PriorityQueue<Integer> sizeSet = getOrPut(num - 1); | |
int size = sizeSet.isEmpty() ? 0 : sizeSet.poll(); | |
getOrPut(num).offer(size + 1); | |
} | |
//System.out.println(valToSize); | |
for (Map.Entry<Integer, PriorityQueue<Integer>> entry : valToSize.entrySet()) { | |
if (entry.getValue().stream().anyMatch(val -> val < 3)) return false; | |
} | |
return true; | |
} |
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, TreeSet<Integer>> valToSize = new HashMap<>(); | |
private TreeSet<Integer> getOrPut(int num) { | |
TreeSet<Integer> sizeSet = valToSize.get(num); | |
if (sizeSet == null) valToSize.put(num, new TreeSet<>()); | |
return valToSize.get(num); | |
} | |
public boolean isPossible(int[] nums) { | |
for (int num : nums) { | |
TreeSet<Integer> sizeSet = getOrPut(num - 1); | |
int size = sizeSet.isEmpty() ? 0 : sizeSet.pollFirst(); | |
getOrPut(num).add(size + 1); | |
} | |
//System.out.println(valToSize); | |
for (Map.Entry<Integer, TreeSet<Integer>> entry : valToSize.entrySet()) { | |
if (entry.getValue().stream().anyMatch(val -> val < 3)) return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment