Skip to content

Commit

Permalink
dvalue
Browse files Browse the repository at this point in the history
  • Loading branch information
ljfirst committed Jan 8, 2019
1 parent a79e2a5 commit 7659ca9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions alg.other/other/D_Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* 例如b2+b3+b4 = (a2-a3) + (a3-a4) + (a4-a5) = a2 - a5
* O(n)构造出B序列后,用类似“最大子段和”算法求“最小绝对值子段和”(不是最简洁)。
*
* 方法二:遍历一遍数据,找出最大值Max和最小值Min,然后把整个数据进行划分,step=(Max - Min)/n.然后遍历这n个桶,
* 方法二(废):遍历一遍数据,找出最大值Max和最小值Min,然后把整个数据进行划分,step=(Max - Min)/n.然后遍历这n个桶,
* 相邻元素的最大值一定是某个桶i中的最大值和桶(i+1)中的最小值的差值。满足Min + n * step = Max。
* 反证法:假如这个相邻元素的最大间距不是某个桶 i 中的最大值和桶 (i+1) 中的最小值的差值,
* 即最大间距的两个元素位于同一个桶中,即最大间距小于step,所以Min + n * step < Maxd的。
Expand All @@ -24,8 +24,23 @@
*/
public class D_Value {

//最小绝对值子段和
public void method1(int[] array) {
// TODO Auto-generated method stub
//排序
Arrays.sort(array);
int min = Integer.MAX_VALUE;
int temp = 0;
for (int i = 1; i < array.length; i++) {
temp = array[i] - array[i-1];
if(min > temp){
min = temp;
}
}
System.out.println(min);
}

//最小绝对值子段和
public void method2(int[] array) {

//构造数组B=(b1,b2,...,bn-1)
int[] array_b = new int[array.length - 1];
Expand All @@ -37,7 +52,18 @@ public void method1(int[] array) {
}
}
//数组B 最小绝对值子段和
int start = 0;
int[] array_c = new int[array_b.length];
int min = Integer.MAX_VALUE;
for (int i = 0; i < array_c.length; i++) {
for (int j = 0; j <= i; j++) {
array_c[j] += array_b[i];
if(Math.abs(min) > Math.abs(array_c[j])){
min = Math.abs(array_c[j]);
}
}
}
//数组B 最小绝对值子段和(有问题)
/*int start = 0;
int end = 0;
int start_temp = 0;
int temp_abs = 0;
Expand All @@ -49,6 +75,7 @@ public void method1(int[] array) {
sum_temp += array_b[i];
sum_temp_abs = sum_temp > 0 ? sum_temp : 0 - sum_temp;
temp_abs = array_b[i] > 0 ? array_b[i] : 0 - array_b[i];
if (temp_abs < best_min) {
best_min = temp_abs;
start = i;
Expand All @@ -64,13 +91,13 @@ public void method1(int[] array) {
sum_temp = best_min;
}
}
}
}*/
//输出差值
System.out.println(best_min);
System.out.println(min);
}

//桶方法:这个方法有极大的漏洞
public void method2(int[] array) {
/*public void method3(int[] array) {
// TODO Auto-generated method stub
//找出最大值Max和最小值Min
Arrays.sort(array);
Expand All @@ -90,24 +117,26 @@ public void method2(int[] array) {
while (array[start] < count) {
start++;
}

if(start <= n - 1 && best_min > (array[start] - array[start - 1])){
best_min = array[start] - array[start - 1];
}
}
count += step;
}
System.out.println(best_min);
}
}*/

//主方法测试
public static void main(String[] args) {
int[] array = {15,7,9,-640,80,-2,33,25,41,-5,84,6};
//期望输出9 - 8 = 1
int[] array = {15,7,-640,10,80,-2,33,25,41,-5,84,22};
int[] array1 = {15,7,-640,10,80,-2,33,25,41,-5,84,22};
int[] array2 = {15,7,-640,10,80,-2,33,25,41,-5,84,22};

//期望输出10 - 7 = 3
D_Value dv = new D_Value();
System.out.println("方法一:最小绝对值子段和");
System.out.println("方法一:排序暴力差值");
dv.method1(array);
System.out.println("方法二:桶方法");
dv.method2(array);
System.out.println("方法二:最小绝对值子段和");
dv.method2(array1);
}
}
Binary file modified bin/other/D_Value.class
Binary file not shown.

0 comments on commit 7659ca9

Please sign in to comment.