也...您只能与之前的值进行比较 例如:(5,3,4,6,2,10)
n = 5
4 -(3)
5-(4,3)...
在这种情况下,答案是8 而这里(4,3,2,1)-anwer是0 我写的是:但是我似乎错过了一些东西
public static int ReverseFindDiffenrce(int[] arr)
{
if (arr == null || arr.Length < 3 || arr[0] != arr.Length - 1)
return -1;
if (arr.Any(i => i < 0)) return -1;
int max_diff = 0;
for (int i = 2; i <= arr[0] ; ++i)
{
for (int j = 1; j <= i -1 ; ++j)
{
if (arr[i] - arr[j] > max_diff)
max_diff = arr[i] - arr[j];
}
}
return max_diff;
}
效率更高
答案 0 :(得分:2)
这对于缓存左侧的最小值更为有效。
public static int ReverseFindDiffenrce(int[] arr)
{
if (arr == null || arr.Length < 3 || arr[0] != arr.Length - 1)
return -1;
if (arr.Any(i => i < 0)) return -1;
int max_diff = 0;
int min = arr[1];
for (int i = 2; i <= arr[0] ; ++i)
{
if (arr[i] < min)
min = arr[i];
if (arr[i] - min > max_diff)
max_diff = arr[i] - min;
}
return max_diff;
}
答案 1 :(得分:0)
从我的测试来看,它只减少了约20%,仅消除一次Any()
就循环一次。
Arr [0]始终处于测试状态,因为它应等于Lenght-1
已测试Arr [1],因为它不在I = 2循环中。
public static int ReverseFindDiffenrceWithoutAny(int[] arr)
{
if (arr == null || arr.Length < 3 || arr[0] != arr.Length - 1 || arr[1] < 0)
{
return -1;
}
int max_diff = 0;
for (int i = 2; i <= arr[0]; ++i)
{
if (arr[i] < 0)
{
return -1;
}
for (int j = 1; j <= i - 1; ++j)
{
if (arr[i] - arr[j] > max_diff)
{
max_diff = arr[i] - arr[j];
}
}
}
return max_diff;
}
基准结果,最后一列是基于原始功能的百分比:
// * Summary *
Method | Mean | Error | StdDev |
------------------ |---------:|---------:|---------:|
DiffOriginal | 366.6 ns | 4.802 ns | 4.492 ns | 0 %
DiffTom | 272.2 ns | 2.766 ns | 2.587 ns | -25.75%
DiffOriginalNoAny | 340.1 ns | 2.474 ns | 2.193 ns | - 7.22%
DiffTomNoAny | 217.4 ns | 1.878 ns | 1.756 ns | -40.69%
使用的方法:
public class ArrayDiff
{
public static int[] arr => new[] { 5, 3, 4, 6, 2, 10 };
[Benchmark]
public static int DiffOriginalNoAny()
{
if (arr == null || arr.Length < 3 || arr[0] != arr.Length - 1 || arr[1] < 0)
{
return -1;
}
int max_diff = 0;
for (int i = 2; i <= arr[0]; ++i)
{
if (arr[i] < 0)
{
return -1;
}
for (int j = 1; j <= i - 1; ++j)
{
if (arr[i] - arr[j] > max_diff)
{
max_diff = arr[i] - arr[j];
}
}
}
return max_diff;
}
[Benchmark]
public static int DiffOriginal()
{
if (arr == null || arr.Length < 3 || arr[0] != arr.Length - 1)
{
return -1;
}
if (arr.Any(i => i < 0))
{
return -1;
}
int max_diff = 0;
for (int i = 2; i <= arr[0]; ++i)
{
for (int j = 1; j <= i - 1; ++j)
{
if (arr[i] - arr[j] > max_diff)
{
max_diff = arr[i] - arr[j];
}
}
}
return max_diff;
}
[Benchmark]
public static int DiffTom()
{
if (arr == null || arr.Length < 3 || arr[0] != arr.Length - 1)
{
return -1;
}
if (arr.Any(i => i < 0))
{
return -1;
}
int max_diff = 0;
int min = arr[1];
for (int i = 2; i <= arr[0]; ++i)
{
if (arr[i] < 0)
{
return -1;
}
if (arr[i] < min)
{
min = arr[i];
}
if (arr[i] - min > max_diff)
{
max_diff = arr[i] - min;
}
}
return max_diff;
}
[Benchmark]
public static int DiffTomNoAny()
{
if (arr == null || arr.Length < 3 || arr[0] != arr.Length - 1 || arr[1] < 0)
{
return -1;
}
int max_diff = 0;
int min = arr[1];
for (int i = 2; i <= arr[0]; ++i)
{
if (arr[i] < 0)
{
return -1;
}
if (arr[i] < min)
{
min = arr[i];
}
if (arr[i] - min > max_diff)
{
max_diff = arr[i] - min;
}
}
return max_diff;
}
}
答案 2 :(得分:0)
我想更新,这是一个错误,有可能出现负数,并且也没有像原始问题所建议的那样在输入中添加arr [0],它只是数组的一部分: 在这种情况下,这是正确的答案:
public static int GetMaxDiff(int[] arr)
{
if (arr == null || arr.Length < 2 )
return -1;
int max_diff = -1;
int min = arr[0];
for (int i = 0; i < arr.Length; ++i)
{
if (arr[i] < 0)
return -1;
if (arr[i] < min)
min = arr[i];
if (arr[i] - min > max_diff)
max_diff = arr[i] - min;
}
if (max_diff == 0) return -1;
return max_diff;
}
感谢帮助:)