ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

力扣日常 #659分割数组为连续子序列 题解解析

2020-12-04 18:33:08  阅读:280  来源: 互联网

标签:map get int 题解 countMap 力扣 put containsKey 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, new PriorityQueue<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 }

方法二 贪心 这个力扣写的挺明白的 就不说了

又是做不出日常的一天 难顶 真有你的啊 力扣

 

                                                            

标签:map,get,int,题解,countMap,力扣,put,containsKey,659
来源: https://www.cnblogs.com/doomslayer/p/14087227.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有