
Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
History is littered with hundreds of conflicts over the future of a community, group, location or business that were "resolved" when one of the parties stepped ahead and destroyed what was there. With the original point of contention destroyed, the debates would fall to the wayside. Archive Team believes that by duplicated condemned data, the conversation and debate can continue, as well as the richness and insight gained by keeping the materials. Our projects have ranged in size from a single volunteer downloading the data to a small-but-critical site, to over 100 volunteers stepping forward to acquire terabytes of user-created data to save for future generations.
The main site for Archive Team is at archiveteam.org and contains up to the date information on various projects, manifestos, plans and walkthroughs.
This collection contains the output of many Archive Team projects, both ongoing and completed. Thanks to the generous providing of disk space by the Internet Archive, multi-terabyte datasets can be made available, as well as in use by the Wayback Machine, providing a path back to lost websites and work.
Our collection has grown to the point of having sub-collections for the type of data we acquire. If you are seeking to browse the contents of these collections, the Wayback Machine is the best first stop. Otherwise, you are free to dig into the stacks to see what you may find.
The Archive Team Panic Downloads are full pulldowns of currently extant websites, meant to serve as emergency backups for needed sites that are in danger of closing, or which will be missed dearly if suddenly lost due to hard drive crashes or server failures.
Alice has a
hand
of cards, given as an array of integers.Now she wants to rearrange the cards into groups so that each group is size
W
, and consists ofW
consecutive cards.Return
true
if and only if she can.Example 1:
Example 2:
Note:
1 <= hand.length <= 10000
0 <= hand[i] <= 10^9
1 <= W <= hand.length
这道题说是我们在打扑克牌,是否能将手里的牌都以顺子的形式出完。在打拐3挖坑或者斗地主的时候,顺子牌在后期的威力是蛮大的,某些人手里憋了一把牌,结果到后期找个机会上手了之后,直接一把甩完,看的手中只有一把单牌的博主是目瞪口呆。其实到了后期,大家手中都没啥牌了的时候,就算是很小的连牌,也不一定能要得起,而划单是最没前途的出法,所以要尽量将手中的牌都组成顺子丢出去。这里给了一个W,规定了顺子的最小长度,那么我们就拿例子1来模拟下打牌吧,首先摸到了牌之后,肯定要先整牌,按从小到大的顺序排列,这里就不考虑啥3最大,4最小啥的,就统一按原始数字排列吧:
1 2 2 3 3 4 6 7 8
好,下面要来组顺子,既然这里是3张可连,那么从最小的开始连呗。其实这道题还是简化了许多,真正打牌的时候,即便是3张起连,那么连4张5张都是可以的,可以这里限定了只能连W张,就使得题目变简单了。我们用贪婪算法就可以了,首先从1开始,那么一定得有2和3,才能起连,若少了任何一个,都可以直接返回false,好那么取出这三张后,手里还有:
2 3 4 6 7 8
那么从当前手里的最小的牌2开始起连,那么手里必须要有3和4,若少了任何一个,都可以直接返回 false,好那么取出这三张后,手里还有:
6 7 8
从当前手里的最小的牌6开始起连,那么手里必须要有7和8,若少了任何一个,都可以直接返回 false,好那么取出这三张后,手里没牌了,我们成功的连完了所有的牌。分析这个过程,不难发现,由于牌可以重复,所以要统计每张牌出现的次数,同时还要给牌按大小排序,用 TreeMap 来建立牌的大小和其出现次数之间的映射就最好不过了,利用了其可以按 key 值排序的特点。首先遍历手中牌,建立映射。然后开始 while 循环,条件是 TreeMap 不为空,然后去除最小的那张牌,然后遍历能组成顺子的W张牌,若没有直接返回 true,有的话,则映射值自减1,若映射值为0了,则从 TreeMap 中移除该映射对儿即可,while 循环退出后返回 true,参见代码如下:
解法一:
我们也可以不对 TreeMap 进行删除操作,而是直接修改其映射值,在建立好映射对儿之后,不用 while 循环,而是遍历所有的映射对儿,若某个映射值为0了,直接跳过。然后还是遍历能组成顺子的W张牌,若某张牌出现的次数小于顺子起始位置的牌的个数,则直接返回 false,因为肯定会有起始牌剩余,无法全组成顺子,这样还避免了上面解法中一张一张减的操作,提高了运算效率。然后映射值减去起始牌的个数,最后 for 循环退出后,返回 true,参见代码如下:
解法二:
Github 同步地址:
#846
参考资料:
https://leetcode.com/problems/hand-of-straights/
https://leetcode.com/problems/hand-of-straights/discuss/135700/Short-Java-solution!
https://leetcode.com/problems/hand-of-straights/discuss/135598/C%2B%2BJavaPython-O(MlogM)-Complexity
LeetCode All in One 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered: