您好,欢迎访问一九零五行业门户网

在Java中,将数组分割为基于给定查询的子数组后,找到子数组的最大子数组和

我们有两个整数数组,一个具有计算的元素,另一个具有分割数组以生成子集所需的分割点,我们必须计算每个分割中每个子集的总和并返回最大子集
让我们通过示例来理解:-输入− int arr[] = int arr[] = { 9, 4, 5, 6 , 7 } int splitpoints[] = { 0, 2, 3, 1 };
输出− 每次分割后的最大子数组和 [22, 13, 9, 9]
解释− 这里我们根据数组的分割点来分解数组,并在每次分割后获得最大子集和
第一次分割后 strong> → {9} 和 {4,5,6,7} >> 最大子数组总和为 - 22
第二次分割后 → {9}, {4,5 } 和 {6,7} >> 最大子数组和为 - 13
第三次分割后 →{9}、{4,5}、{6} 和 {7} >> 最大子数组和为 - 9
第四次分割后 →{9}、{4}、{5}、{6} 和 {7} >> 最大子数组和is- 9
输入−int arr[] = int arr[] = { 7, 8, 5, 9, 1 } int splitpoints[] = { 1, 2, 0, 3 };
输出−每次分割后的最大子数组和 [15, 115, 10, 9]
解释−这里我们根据数组的分割点来分解数组,并在每次分割后获得最大子集和
第一次分割后 → {7,8} 和 {5,9 ,1} >> 最大子数组和为 15
第二次分割后 → {7,8}、{5} 和 {9,1} >> 最大子数组和为 115
第三次分割后 →{7}、{8}、{5} 和 {9,1} >> 最大子数组总和为 10
第四次分割后 →{7}、{8}、{5}、{9} 和 {1} >> 最大子数组和为 9
以下程序中使用的方法是如下 -我们将从 main() 方法开始
输入数组任何给定长度,例如 arr[] 和 splitpoints[]。计算它们的长度并以calculatesubsetsum(arr.length, splitpoints.length, splitpoints, arr)的形式传递给方法。
在方法calculatesubsetsum中()
创建一个整数数组作为 sum[],并将 sum[0] 设置为 arr[0]。
开始循环 for 从 i 到 1 直到数组的长度,并将 sum[i] 设置为 sum[i - 1] + arr[i] 并将 temp[0] 设置为 new subsets(0, n - 1, sum[n - 1])。
继续添加 t2.add(temp[0]) 和 t1.add(0)
从 i 到 0 开始循环 for,直到 splitpoints 数组的长度。在循环内部将 currentsplitpoint 设置为 t1.floor(splitpoints[i]) 并从 t2 中删除为 t2.remove(temp[currentsplitpoint])
将 end 设置为 temp[currentsplitpoint] .last 和 temp[currentsplitpoint] 作为新的 subsets(currentsplitpoint, splitpoints[i], sum[splitpoints[i]] - (currentsplitpoint == 0 ? 0 : sum[currentsplitpoint - 1]))
使用 t2.add(temp[currentsplitpoint]) 和 temp[splitpoints[i] + 1] = new subsets(splitpoints[i] + 1, end, sum[end] - sum[splitpoints[i] 添加]])
使用 t2.add(temp[splitpoints[i] + 1])、t1.add(currentsplitpoint) 和 t1.add(splitpoints[i] + 进行添加1)
打印 t2.first() 值。
创建一个类类 subsets 并声明first、last和value作为其数据成员,并将默认构造函数定义为subsets(int f, int l, int v),并将first设置为f,last设置为l,value设置为v
创建一个类作为 utilitycomparator,它将实现 comparator
创建一个公共方法作为比较并检查 if s2.value 不等于到 s1.value,然后返回 s2.value - s1.value。
检查 s1.first 是否不等于 s2.first,然后返回 s2.first - s1.first p>
示例import java.io.ioexception;import java.io.inputstream;import java.util.*;class utilitycomparator implements comparator<subsets>{ public int compare(subsets s1, subsets s2){ if(s2.value != s1.value){ return s2.value - s1.value; } if(s1.first != s2.first){ return s2.first - s1.first; } return 0; }}class subsets{ int first; int last; int value; subsets(int f, int l, int v){ first = f; last = l; value = v; }}public class testclass{ static void calculatesubsetsum(int n, int k, int splitpoints[], int arr[]){ int sum[] = new int[n]; sum[0] = arr[0]; for (int i = 1; i < n; i++){ sum[i] = sum[i - 1] + arr[i]; } treeset<integer> t1 = new treeset<>(); treeset<subsets> t2 = new treeset<>(new utilitycomparator()); subsets temp[] = new subsets[n]; temp[0] = new subsets(0, n - 1, sum[n - 1]); t2.add(temp[0]); t1.add(0); system.out.println("maximum subarray sum after each split"); for (int i = 0; i < k; i++){ int currentsplitpoint = t1.floor(splitpoints[i]); t2.remove(temp[currentsplitpoint]); int end = temp[currentsplitpoint].last; temp[currentsplitpoint] = new subsets(currentsplitpoint, splitpoints[i], sum[splitpoints[i]] - (currentsplitpoint == 0 ? 0 : sum[currentsplitpoint - 1])); t2.add(temp[currentsplitpoint]); temp[splitpoints[i] + 1] = new subsets(splitpoints[i] + 1, end, sum[end] - sum[splitpoints[i]]); t2.add(temp[splitpoints[i] + 1]); t1.add(currentsplitpoint); t1.add(splitpoints[i] + 1); system.out.println(t2.first().value); } } public static void main(string[] args){ int arr[] = { 2, 1, 6, 8, 5, 10, 21, 13}; int splitpoints[] = { 3, 1, 2, 0, 4, 5 }; calculatesubsetsum(arr.length, splitpoints.length, splitpoints, arr); }}
输出如果我们运行上面的代码,它将生成以下输出
maximum subarray sum after each split494949494434
以上就是在java中,将数组分割为基于给定查询的子数组后,找到子数组的最大子数组和的详细内容。
其它类似信息

推荐信息