
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.
We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1.
Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subsequences.
Example 1:
Note: The length of the input array will not exceed 20,000.
这道题给了我们一个数组,让我们找出最长的和谐子序列,关于和谐子序列就是序列中数组的最大最小差值均为1。由于这里只是让我们求长度,并不需要返回具体的子序列。所以我们可以对数组进行排序,那么实际上我们只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列的长度了。明白了这一点,我们就可以建立一个数字和其出现次数之间的映射,利用 TreeMap 的自动排序的特性,那么我们遍历 TreeMap 的时候就是从小往大开始遍历,我们从第二个映射对开始遍历,每次跟其前面的映射对比较,如果二者的数字刚好差1,那么就把二个数字的出现的次数相加并更新结果 res 即可,参见代码如下:
解法一:
其实我们并不用向上面那种解法那样用 next 和 prev 来移动迭代器,因为其用到了 TreeMap 的自动排序功能,所以才可以利用 next 和 prev。其实我们还可以用 HashMap 来做,先遍历一遍,建立每个数字跟其出现次数之间的映射,然后再遍历每个数字的时候,只需在 HashMap 中查找该数字加1是否存在,存在就更新结果 res,这样更简单一些,参见代码如下:
解法二:
我们其实也可以在一个 for 循环中搞定,遍历每个数字时,先累加其映射值,然后查找该数字加1是否存在,存在的话用 m[num] 和 m[num+1] 的和来更新结果 res,同时,还要查找该数字减1是否存在,存在的话用 m[num] 和 m[num-1] 的和来更新结果 res,这样也是可以的,参见代码如下:
解法三:
下面方法不用任何 map,但是需要对数组进行排序,当数组有序了之后,我们就可以一次遍历搞定了。这实际上用到了滑动窗口 Sliding Window 的思想,用变量 start 记录当前窗口的左边界,初始化为0。用 new_start 指向下一个潜在窗口的左边界,初始化为0。i为当前窗口的右边界,从1开始遍历,首先验证当前窗口的差值是否小于1,用 nums[i] 减去 nums[start],若不满足,则将 start 赋值为 new_start,即移动到下一个窗口。然后看当前数字跟之前一个数字是否相等,若不相等,说明当前数字可能是下一个潜在窗口的左边界,将 new_start 赋值为i。然后再看窗口的左右边界值是否刚好为1,因为题目中说了差值必须正好为1,由于我们对数组排序了,所以只要左右边界差值正好为1,那么这个窗口包含的数字就可以组成满足题意的子序列,用其长度来更新结果 res 即可,参见代码如下:
解法四:
参考资料:
https://leetcode.com/problems/longest-harmonious-subsequence/
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/103497/Simple-Java-HashMap-Solution
https://leetcode.com/problems/longest-harmonious-subsequence/discuss/103499/Three-C%2B%2B-Solution-run-time-with-explanation
LeetCode All in One 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered: