力扣日常 #659分割陣列為連續子序列 題解解析
太難了 不會寫 試試寫過程分析吧 方法一 雜湊表+最小堆
1 class Solution { 2 public boolean isPossible(int[] nums) { 3 Map<Integer, PriorityQueue<Integer>> map = new HashMap<Integer, PriorityQueue<Integer>>(); 4 for (int x : nums) { 5 if (!map.containsKey(x)) { 6 map.put(x, newPriorityQueue<Integer>()); 7 } 8 if (map.containsKey(x - 1)) { 9 int prevLength = map.get(x - 1).poll(); 10 if (map.get(x - 1).isEmpty()) { 11 map.remove(x - 1); 12 } 13 map.get(x).offer(prevLength + 1);14 } else { 15 map.get(x).offer(1); 16 } 17 } 18 Set<Map.Entry<Integer, PriorityQueue<Integer>>> entrySet = map.entrySet(); 19 for (Map.Entry<Integer, PriorityQueue<Integer>> entry : entrySet) { 20 PriorityQueue<Integer> queue = entry.getValue();21 if (queue.peek() < 3) { 22 return false; 23 } 24 } 25 return true; 26 } 27 }
1:!map.containsKey(1) => map現狀: {1: [ ]}
!map.containsKey(1 - 1) => map.get(1).offer(1) => map現狀: {1: [1]}
2: !map.containsKey(2) => map現狀: {1: [1] --------------------------------------------↓
2: [ ] } => map.containsKey(1) => prevLength = map.get(1).poll() => poll()後1的Queue為empty => map更新為:{2: [2]}
3:!map.containsKey(3) => map現狀:{2: [2]----------------------------------------------↓
3: [ ] } => map.containsKey(2) => prevLength = map.get(2).poll() => poll()後2的Queue為empty => map更新為:{3: [3]}
3:不滿足前兩個if迴圈 進入 else =>map更新為: {3: [1, 3] }
4:!map.contains(4) => map更新: {3: [1, 3]
4: [ ] } => map.contansKey(3) => prevLength = map.get(3).poll() =>
poll()後3的Queue不為empty map 更新為{3: [3]
4: [2]}
4: map.containsKey(3) => prevLength = 3 =>map.get(3).isEmpty 為空 => map更新為{4: [ 4, 2] }
5: map.containsKey(4) => prevLength = 2 =>map.get(4).isEmpty 不為空 => map更新為{4: [4]
5: [3]}
5:map.containsKey(4) => prevLength =4 =>map.get(4).isEmpty為空 => map更新為 {5: [3, 5] }
最後呢 就是遍歷一下map中的PriorityQueue 如果有<3的就返回false
思路呢 這裡抄力扣的答案力:
遍歷陣列,當遍歷到元素 xx 時,可以得到一個以 xx 結尾的子序列。
如果雜湊表中存在以 x-1x−1 結尾的子序列,則取出以 x-1x−1 結尾的最小的子序列長度,將子序列長度加 11 之後作為以 xx 結尾的子序列長度。此時,以 x-1x−1 結尾的子序列減少了一個,以 xx 結尾的子序列增加了一個。
如果雜湊表中不存在以 x-1x−1 結尾的子序列,則新建一個長度為 11 的以 xx 結尾的子序列。
1 class Solution { 2 public boolean isPossible(int[] nums) { 3 Map<Integer, Integer> countMap = new HashMap<Integer, Integer>(); 4 Map<Integer, Integer> endMap = new HashMap<Integer, Integer>(); 5 for (int x : nums) { 6 int count = countMap.getOrDefault(x, 0) + 1; 7 countMap.put(x, count); 8 } 9 for (int x : nums) { 10 int count = countMap.getOrDefault(x, 0); 11 if (count > 0) { 12 int prevEndCount = endMap.getOrDefault(x - 1, 0); 13 if (prevEndCount > 0) { 14 countMap.put(x, count - 1); 15 endMap.put(x - 1, prevEndCount - 1); 16 endMap.put(x, endMap.getOrDefault(x, 0) + 1); 17 } else { 18 int count1 = countMap.getOrDefault(x + 1, 0); 19 int count2 = countMap.getOrDefault(x + 2, 0); 20 if (count1 > 0 && count2 > 0) { 21 countMap.put(x, count - 1); 22 countMap.put(x + 1, count1 - 1); 23 countMap.put(x + 2, count2 - 1); 24 endMap.put(x + 2, endMap.getOrDefault(x + 2, 0) + 1); 25 } else { 26 return false; 27 } 28 } 29 } 30 } 31 return true; 32 } 33 }
方法二 貪心 這個力扣寫的挺明白的 就不說了
又是做不出日常的一天 難頂 真有你的啊 力扣