Skip to content

Commit

Permalink
update 659 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 18, 2024
1 parent 814c68f commit 077f482
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,64 @@
public class SplitArrayIntoConsecutiveSubsequences {

// V0
// TODO : implement
// public boolean isPossible(int[] nums) {
//
// }
// IDEA : HASHMAP + PQ (fixed by gpt)
public boolean isPossible(int[] nums) {
// edge case
if (nums == null || nums.length == 0) {
return false;
}

// Map to track the subsequences
Map<Integer, PriorityQueue<Integer>> map = new HashMap<>();

for (int x : nums) {
int subSeqCnt = 0;

// Try to extend an existing subsequence ending with x-1
/** NOTE !!! below logic
*
*
* especially map.containsKey(x - 1)
*/
if (map.containsKey(x - 1)) {
subSeqCnt = map.get(x - 1).poll();
if (map.get(x - 1).isEmpty()) {
map.remove(x - 1); // Remove if no more subsequences end with x-1
}
}

// Add the current number x to the subsequence
/** NOTE !!! below logic
*
*/
PriorityQueue<Integer> curPq = map.getOrDefault(x, new PriorityQueue<>());
curPq.offer(subSeqCnt + 1); // Add the length of the extended subsequence
map.put(x, curPq);
}

// Check if all subsequences have at least length 3
for (PriorityQueue<Integer> pq : map.values()) {
/**
* NOTE !!!
*
* can't use pq.size() < 3 as validation logic
*
* Using pq.size() < 3 would not correctly check the length of each individual subsequence in this context because:
*
* 1. What the PriorityQueue Stores: The PriorityQueue in your map is not storing the lengths of subsequences directly; rather, it stores the counts of individual subsequences. For example, for a number x, the queue might contain values like [2, 4, 5], meaning that there are subsequences of lengths 2, 4, and 5 that end with x.
* 2. Why pq.size() is Incorrect: The size of the PriorityQueue (pq.size()) gives you the number of subsequences ending with a specific number, not the actual length of those subsequences. Therefore, pq.size() < 3 would only be comparing the count of subsequences, not the length of each subsequence.
* 3. Correct Logic: The correct logic is to check the actual length of each subsequence (i.e., the value stored in the PriorityQueue). This is why we poll each element from the queue and check if any subsequence has a length less than 3 (pq.poll() < 3).
*
*/
while (!pq.isEmpty()) {
if (pq.poll() < 3) {
return false;
}
}
}

return true;
}

// V1
// IDEA : MAP
Expand Down
41 changes: 38 additions & 3 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -7704,14 +7704,49 @@ public int compare(int[] o1, int[] o2) {
// return false;
// }

// IDEA : HASHMAP
// IDEA : HASHMAP + PQ
public boolean isPossible(int[] nums) {
// edge case
if (nums == null || nums.length == 0){
return false;
}
Map<Integer, Integer> map = new HashMap<>();
// check subsequence
// key : last element (?)
// val : PQ (subsequence ?)
Map<Integer, PriorityQueue<Integer>> map = new HashMap<>();
for (int x : nums){
int subSeqCnt = 0;
if (map.containsKey(x-1)){
/**
* .poll() : get and remove last element
*/
subSeqCnt = map.get(x-1).poll();
if (map.get(x-1).isEmpty()){
map.remove(x-1);
}
}
PriorityQueue<Integer> pq = new PriorityQueue<>();
PriorityQueue<Integer> curPq = map.get(x);
if (curPq.isEmpty()){
pq.add(1);
map.put(x, pq);
}else{
curPq.add(subSeqCnt+1); // ?
map.put(x, curPq);
}
//map.putIfAbsent(x, pq);
}

return false;
// check
int cnt = 0;
for (Integer k : map.keySet()){
PriorityQueue<Integer> _pq = map.get(k);
if (_pq.size() < 3){
return false;
}
cnt += _pq.size();
}
return cnt == nums.length;
}


Expand Down

0 comments on commit 077f482

Please sign in to comment.