[使用动态规划的新解决方案]
我花了很多时间在这上面,希望它能够很好地工作。像往常一样,我进行了注释以使其更加清晰易懂。希望这可以帮到你,我没有找到更有效的方法,抱歉。
package temp2;
public class Main {
private static int
X=1,
Y=2,
VERY_LARGE_NUMBER=1000000;
private static int array[] = {1, 100, 2, 2, 100, 1};
public static void main(String args[]){
System.out.println("Obtained result: "+alg(array.length-1, 0, 0, 0, false));
}
public static int alg(int index, int partialSum, int numberOfUsedArrays, int numberOfUsedElements, boolean beforeIsInResults){
if(index+1+numberOfUsedElements == Y && (numberOfUsedArrays<X || (beforeIsInResults && numberOfUsedArrays<=X))){
int summ = 0;
for(int i=0; i<=index; i++)
summ+=array[i];
return summ+partialSum;
}
if((((numberOfUsedArrays > X && beforeIsInResults) || (numberOfUsedArrays >= X && !beforeIsInResults)) && numberOfUsedElements < Y )){
return VERY_LARGE_NUMBER;
}
if( index < 0 || ((numberOfUsedArrays > X && beforeIsInResults) || (numberOfUsedArrays >= X && !beforeIsInResults)) || numberOfUsedElements >= Y )
return partialSum;
if(beforeIsInResults)
return Math.min(
alg(index-1, partialSum+array[index], numberOfUsedArrays, numberOfUsedElements+1, true),
alg(index-1, partialSum, numberOfUsedArrays, numberOfUsedElements, false));
else
return Math.min(
alg(index-1, partialSum+array[index], numberOfUsedArrays+1, numberOfUsedElements+1, true),
alg(index-1, partialSum, numberOfUsedArrays, numberOfUsedElements, false));
}
}
[旧的不起作用的解决方案]: 计数器示例:{1, 100, 2, 2, 100, 1} x = 1,y = 2
我找到了这个算法。除非我弄错了,复杂度应该是O(Y×数组长度)。
注意:所有这些都假设您的X
变量意味着“子数组的最大数量”。
Start: Find min in array not in result
add it to result
did we reach the max number of result?
(Yes: end and print results) (No: go to Continue)
Continue: Compute min subarrays in the results.
Is it equals to the max subarray (`Y`)?
(No: go to Start) (Yes:Continue2)
Continue2: Find minimum value near one subarray.
go to Continue.
我知道这可能有点令人困惑,而且这还不是最糟糕的部分。
我写了以下代码来测试我的算法。这可能是我写过的最糟糕的代码。
我试图保持简单,避免任何“特殊情况控制”,例如,在某些if中应该有控制以避免任何索引超出边界异常(但由于懒惰,我只放置了一些大而无用的值)。
我尝试了所有你的组合,并且它工作正常。我没有找到任何反例。
如果您需要更多澄清,请随时询问(由于在意大利这里是晚上,我将在明天回复)
这是代码:
public class Main {
public static void main(String[] args){
int X=1000,
Y=3,
temp,
tempIndex,
subarray=0,
resultsSize=0;
int array[] = {9999,2,1,10,40,5,6,9999};
boolean used[] = {false, false, false, false, false, false, false, false};
while(true){
try{
temp=9998;
tempIndex=-1;
for(int i=0; i<array.length; i++){
if(array[i]<temp && !used[i]){
temp=array[i];
tempIndex=i;
}
}
used[tempIndex] = true;
resultsSize++;
System.out.print(temp+" ");
if(resultsSize == Y ) {
System.out.println("\nDone.");
return;
}
if(used[tempIndex-1] && used[tempIndex+1]){
subarray--;
}else if(!used[tempIndex-1] && !used[tempIndex+1]){
subarray++;
}
while(subarray == X){
temp=9998;
tempIndex=-1;
for(int i=0; i<array.length; i++){
if(array[i]<temp && !used[i] && (used[i-1] || used[i+1])){
temp=array[i];
tempIndex=i;
}
}
if(temp==9998){
System.out.println("\nYou shloud not ever come here.\ncheck if your 'Y' value is bigger than the length of the array");
return;
}
used[tempIndex] = true;
resultsSize++;
System.out.print(temp+" ");
if(resultsSize == Y ){
System.out.println("\nDone.");
return;
}
if(used[tempIndex-1] && used[tempIndex+1]){
subarray--;
}
}
} catch(Throwable e){
e.printStackTrace();
return;
}
}
}
}
如果这篇文章对你有用,或者只是浪费了你的时间,请告诉我;)
x
是什么?是子数组数量的上限吗? - DAlex
的定义与最后一个示例不符。你说它是子数组的数量,但在最后一个示例中它并不是。你不能期望对一个定义不清的问题得到有用的答案。 - Gene