Java 使一个数组的元素和等于数组向量中的数字k
我有一个向量,包含n个整数数组(我们称之为数组)和一个数字k。我必须找到一种方法来构造一个向量,我们称之为Sol,它的所有元素之和是k,Sol[I]来自数组[I]。 例: 第一个是n,第二个是k,然后是数组 输入:Java 使一个数组的元素和等于数组向量中的数字k,java,arrays,algorithm,Java,Arrays,Algorithm,我有一个向量,包含n个整数数组(我们称之为数组)和一个数字k。我必须找到一种方法来构造一个向量,我们称之为Sol,它的所有元素之和是k,Sol[I]来自数组[I]。 例: 第一个是n,第二个是k,然后是数组 输入: 3 10 1 2 3 2 5 7 4 6 8 控制台: 26 我可以简单地使用回溯,但这是一个巨大的复杂性。我试图制作一个从底部开始的算法,对于每个点,它将下面的点组合起来,形成一个可能的解决方案列表,如: 3 10 1 2 3 2 5 7 4 6 8 例如: 8
3 10
1 2 3
2 5 7
4 6 8
控制台:
26
我可以简单地使用回溯,但这是一个巨大的复杂性。我试图制作一个从底部开始的算法,对于每个点,它将下面的点组合起来,形成一个可能的解决方案列表,如:
3 10
1 2 3
2 5 7
4 6 8
例如:
8<10,可行的解决方案
6<10,可行的解决方案
4<10,可行的解决方案
7+8=15<10 false不再检查此路径
7+6=13<10 false不再检查此路径
...
即使我这样做了,也有一些情况非常复杂。我的目标是O(m*k)复杂性,其中m是所有输入数组长度的总和
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Vector;
public class Main {
static Vector<Vector<Integer>> Arrays;
static int Arrays_lenght;
static int sum;
public static void main(String[] args) throws FileNotFoundException
{
Scanner data_in = new Scanner(new File("data.in"));
Arrays_lenght = data_in.nextInt();
sum = data_in.nextInt();
Arrays = new Vector<>();
data_in.nextLine();
//read vectors
for(int i = 0; i < numar_vectori; i++)
{
String temp = data_in.nextLine();
Scanner _temp = new Scanner(temp);
Vector<Integer> temp_vector = new Vector<>();
while (_temp.hasNext()) {
temp_vector.add(_temp.nextInt());
}
Arrays.add(temp_vector);
}
Iterator<Vector<Integer>> itr = Arrays.iterator();
while (itr.hasNext())
System.out.printf("%s\n", itr.next().toString());
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Iterator;
导入java.util.Scanner;
导入java.util.Vector;
公共班机{
静态矢量阵列;
静态整数数组长度;
静态整数和;
公共静态void main(字符串[]args)引发FileNotFoundException
{
扫描仪数据_in=新扫描仪(新文件(“data.in”);
数组_lenght=data_in.nextInt();
sum=数据_in.nextInt();
数组=新向量();
.nextLine()中的数据_;
//读取向量
对于(int i=0;i
下面是我在java中读取输入文件的代码。如何在O(m*k)复杂度下生成Sol向量,其中m是所有输入数组的长度之和?动态规划解决方案(我假设输入数组
A[][]
包含自然数):
创建二维数组B[][]
-N行,K+1列,用零填充
for every element of the first input array el=A[0][ix]
set B[0][el] = ix+1
// incrementing is useful to separate filled entries from empty ones
for i = 1 to n-1
for every element of i-th input array `el=A[i][ix]`:
for every ik index in range 0..Sum-el
if B[i - 1, ik] is filled then
set B[i, ik + el] = ix+1
at the end:
if B[N-1, K] is filled
unwind indexes of elements that produce needed sum
第二阶段对输入矩阵的每个元素执行多达K次(第一个数组行除外),因此时间复杂度为O(K*M)。动态规划解决方案(我假设输入数组
A[][]
包含自然数):
创建二维数组B[][]
-N行,K+1列,用零填充
for every element of the first input array el=A[0][ix]
set B[0][el] = ix+1
// incrementing is useful to separate filled entries from empty ones
for i = 1 to n-1
for every element of i-th input array `el=A[i][ix]`:
for every ik index in range 0..Sum-el
if B[i - 1, ik] is filled then
set B[i, ik + el] = ix+1
at the end:
if B[N-1, K] is filled
unwind indexes of elements that produce needed sum
第二阶段对输入矩阵的每个元素执行多达K次(第一个数组行除外),因此时间复杂度为O(K*M)。你的问题是什么?我如何在O(M*K)复杂度下生成Sol向量,其中M是所有输入数组的长度之和。对不起,如果不清楚的话。我会修改我的帖子。你考虑过动态规划吗?您将需要大小约为k*n的存储阵列(取决于实现),请阅读。不知道如何实施。伪码的一些概念?你的问题是什么?我如何在O(m*k)复杂度下生成Sol向量,其中m是所有输入数组的长度之和。对不起,如果不清楚的话。我会修改我的帖子。你考虑过动态规划吗?您将需要大小约为k*n的存储阵列(取决于实现),请阅读。不知道如何实施。伪代码的一些概念?