
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.
Given an integer array
arr
, return the length of a maximum size turbulent subarray ofarr
.A subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.
More formally, a subarray
[arr[i], arr[i + 1], ..., arr[j]]
ofarr
is said to be turbulent if and only if:i <= k < j
:arr[k] > arr[k + 1]
whenk
is odd, andarr[k] < arr[k + 1]
whenk
is even.i <= k < j
:arr[k] > arr[k + 1]
whenk
is even, andarr[k] < arr[k + 1]
whenk
is odd.Example 1:
Example 2:
Example 3:
Constraints:
1 <= arr.length <= 4 * 104
0 <= arr[i] <= 109
这道题给了一个数组,定义一种湍流子数组,即数字增减交替,就是先增大再减小再增大等交替进行,或者是先减小再增大再减小等交替进行的。现在让找出最长的湍流子数组,并返回长度。首先最无脑的暴力搜索就是遍历所有的子数组,并判断是否是湍流数组,这样太不高效了,有点不尊重 OJ 的感觉,估计不会给过。毕竟是道 Medium 题,总得给点面子吧,那就来想一下,我们并不知道湍流子数组的起点在哪,也不知道它到底是先增大还是先减小,这样的话其实每个位置都有可能是一个湍流数组的起点或终点,就按终点来考虑,每个位置都代表一种状态,而且这道题又是求极值的问题,那么该什么方法是不是就呼之欲出了,来~大声地告诉博主,用什么方法?对了,就是动态规划 Dynamic Programming,首先来想怎么定义 dp 数组,要求的是湍流子数组的长度,那么 dp 值代表的含义应该也是长度。又因为每个位置都可能是个湍流子数组的终点,并且末尾数字可能是下降或者上升,有两种状态,可以用一个二维 dp 数组,也可以用两个一维数组 dec 和 inc 来表示,这里 dec[i] 表示湍流数组的长度,同时其末尾是数字是 arr[i] 且是下降的,同理,inc[i] 表示湍流数组的长度,同时其末尾是数字是 arr[i] 且是上升的。数组定义好了,下面是就是找状态转移方程了,其实也不难,当前状态跟前一个状态息息相关,首先要比较当前数字和前一个数字的大小关系,若前一个数字大于当前数字,则表示下降的关系,则可以更新 dec[i] 为 inc[i-1] + 1,反之,若前一个数字小于当前数字,则表示上升的关系,则可以更新 inc[i] 为 dec[i-1] + 1。每次更新完一个位置,从 dec[i] 和 inc[i] 中找出最大的位置,用来更新结果 res 即可,参见代码如下:
解法一:
我们可以进一步的更新空间复杂度,因为当前状态仅仅依赖前一个状态,所以没有必要保留所有位置的状态,就只有两个变量就可以了,不过要注意的是别忘了及时的将 inc 或 dec 重置为1,当相邻的两个数字相同时,两者还要同时重置1,参见代码如下:
解法二:
Github 同步地址:
#978
类似题目:
Maximum Subarray
参考资料:
https://leetcode.com/problems/longest-turbulent-subarray/
https://leetcode.com/problems/longest-turbulent-subarray/discuss/221935/Java-O(N)-time-O(1)-space
https://leetcode.com/problems/longest-turbulent-subarray/discuss/221929/C%2B%2BJava-6-lines-flip-the-sign
LeetCode All in One 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered: