
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 array
A
of integers, for each integerA[i]
we need to choose eitherx = -K
orx = K
, and addx
toA[i] (only once)
.After this process, we have some array
B
.Return the smallest possible difference between the maximum value of
B
and the minimum value ofB
.Example 1:
Example 2:
Example 3:
Note:
1 <= A.length <= 10000
0 <= A[i] <= 10000
0 <= K <= 10000
这道题是之前那道 Smallest Range I 的拓展,那道题说的是每个数字可以加上 [-K, K] 范围内的任意一个数字,这道题说是每个数字必须加上K或者 —K,都是问新数组的最大值最小值之间的差值最小是多少。这两个条件有显著的区别,对于前一道题来说,非常的 flexible,因为可以加上 [-K, K] 范围内的任意一个数字,就是说也可以加上0,从而数字保持不变,那么只需要对原数组的最大值最小值进行修改即可,而这道题所有数字都强制要进行修改,则只考虑最大值最小值显然是不对的。来想想为什么不能直接对最小值加K,最大值减K,然后算差值。来看题目中的例子3,A = [1,3,6], K = 3,对最小值加K,得到4,对最大值减K,得到3,相减得到 -1,但实际上是不对的,因为中间的3也要进行加减操作,只能变成0或6,这样相当于改变了新数组的最大值最小值,最终算下来的结果应该是3。博主其实也尝试了暴力搜索法,即将原数组中的每个数字进行加K和减K,把得到的两个新数字放到一个新数组中,最后遍历新数组,找出最大值最小值并做差,结果超时了 Time Limit Exceeded!即便这只是一道 Medium 的题目,仍然不许我们用如此 Naive 的方法。只能另辟蹊径了。
如果不考虑数字修改,那么原数组的最大值最小值之间的差值就是所求,这里可以当作结果 res 的初始值。由于每个数字都需要加K或者减K,为了使得新数组的最大值最小值的差值最小,应该尽量使原数组中的较小的数字加K,较大的数字减K,所以最好是先给原数组排个序,然后在数组的某个位置i为界限,将原数组分为两段,前面所有的数字都加K,后面所有的数字都减K。则前半段 [0, i] 中的最大值是 A[i]+K,最小值是 A[0]+K,后半段 [i+1, n-1] 范围内的最大值是 A[n-1]-K,最小值是 A[i+1]-K,所以整个数组的最大值是 A[i]+K 和 A[n-1]-K 中的较大值,最小值是 A[0]+K 和 A[i+1]-K 中的较小值,二者做差就是可能的结果了,遍历所有的i,用每次计算出的差值来更新结果 res 即可,参见代码如下:
Github 同步地址:
#910
类似题目:
Smallest Range I
参考资料:
https://leetcode.com/problems/smallest-range-ii/
https://leetcode.com/problems/smallest-range-ii/discuss/173377/C%2B%2BJavaPython-Add-0-or-2-*-K
https://leetcode.com/problems/smallest-range-ii/discuss/173389/simple-C%2B%2B-solution-with-explanation
LeetCode All in One 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered: