Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在不能跳过数组中1个或多个连续数字的数组中查找最大额定值(数字)_Java_Arrays_Algorithm_Data Structures - Fatal编程技术网

Java 在不能跳过数组中1个或多个连续数字的数组中查找最大额定值(数字)

Java 在不能跳过数组中1个或多个连续数字的数组中查找最大额定值(数字),java,arrays,algorithm,data-structures,Java,Arrays,Algorithm,Data Structures,我们有一系列的评级,我们必须在这样一个距离中找到最大评级,我们不能跳过一个阵列中的一个或多个连续评级 Example-1: {9,-1,-3,-4,-5} output = 9 + -1 + -4 = 4 说明:我取了9,我们必须取-1或-3,我们不能直接跳到-4,因为我们不能跳过一个或多个连续的数字 Example-2: {-1,-2,-3,-4,-5} output = -2 + -4 = -6 Example-3: {-3,2,-4,-1,-2,5} output = 2 + -1

我们有一系列的评级,我们必须在这样一个距离中找到最大评级,我们不能跳过一个阵列中的一个或多个连续评级

Example-1: {9,-1,-3,-4,-5} output = 9 + -1 + -4 = 4

说明:我取了9,我们必须取-1或-3,我们不能直接跳到-4,因为我们不能跳过一个或多个连续的数字

Example-2: {-1,-2,-3,-4,-5} output = -2 + -4 = -6  
Example-3: {-3,2,-4,-1,-2,5} output = 2 + -1 + 5 = 6   
Example-4: {9,-1,-3,4,5} output = 9 + -1 + 4 + 5 = 17
我尝试了下面的代码,但它在示例2、3、4的情况下工作正常,但在其他场景中,示例1没有类似的失败

static int maximizeRatings(int[] ratings) {
    int current = 0;
    boolean result = false;
    for(int j=0; j<ratings.length;j++){
        if(ratings[j]<0){
            result = true;
        }else{
        result = false; 
        }
    }
    if(result){
        return allnegatine(ratings);
    }
    for(int i=0; i<ratings.length;i++){
        if(i == ratings.length-1){
            if(ratings[i] > 0)
                current += ratings[i];
        }else{
        if(ratings[i] >0 && ratings[i+1]>0){
            current = ratings[i]+ratings[i+1];
            i++;
        }
        if(ratings[i] > ratings[i+1]){
            current += ratings[i];
        }else{
            current += ratings[i+1];
            i++;
        }
        }

    }
    return current;
}

private static int allnegatine(int[] ratings) {
    int current =0;
    for(int i=0; i<ratings.length;i++){
        if(ratings.length%2==0){
            if(i%2 == 0)
            current += ratings[i];
        }else{
            if(i%2!=0)
                current += ratings[i];
        }
    }
    return current;
}
静态整数最大化(整数[]评级){
int电流=0;
布尔结果=假;
对于(int j=0;j0){
电流=额定值[i]+额定值[i+1];
i++;
}
if(评级[i]>评级[i+1]){
电流+=额定值[i];
}否则{
电流+=额定值[i+1];
i++;
}
}
}
回流;
}
专用静态整数allnegatine(整数[]评级){
int电流=0;

对于(inti=0;i这是一个动态规划问题

dp[i]
为可实现的最大额定值,仅考虑阵列中从零开始、在i结束并包括额定值[i]的部分

dp[0]=ratings[0]
dp[1]=max(ratings[1],ratings[0]+ratings[1])
dp[i]=max(dp[i-1],dp[i-2])+ratings[i]
回答:
max(dp[n-1],dp[n-2])
其中n是评级数组的大小


您还可以选择取消dp数组,并为dp[i-1]和dp[i-2]保留两个变量。

我认为您在检查for循环中的所有负数时犯了错误。如果数组中的最后一个元素为负数,则“result”变量将为true,这意味着所有数组都为负数,但实际上不是负数

必须将for循环替换为:

for(int j=0;j<ratings.length;j++){
    if(ratings[j]<0){
        result=true;
    }
    else{
        result = false;
        break;
    }
    }

for(int j=0;j这是典型的递归问题(只要输入数组相当长)。您应该检查项目并尝试所有可能的组合,然后选择最佳组合

因为它看起来像是典型的学校作业,我不确定我是否应该粘贴我的解决方案。你应该自己解决它,或者至少了解下一次自己能够实现它所发生的事情

public class RatingService {

    public int calculate(List<Integer> input) {
        return recursion(input, true, 0);
    }

    private int recursion(List<Integer> sublist, boolean canSkip, int sum) {
        if (sublist.isEmpty()) {
            return sum;
        }
        int skippedSum = Integer.MIN_VALUE;
        int notSkippedSum;
        Integer integer = sublist.get(0);

        if (canSkip) {
            skippedSum = recursion(sublist.subList(1, sublist.size()), false, sum);
        }
        notSkippedSum = recursion(sublist.subList(1, sublist.size()), true, integer + sum);

        return skippedSum > notSkippedSum ? skippedSum : notSkippedSum;
    }
    }
公共类分级服务{
公共整数计算(列表输入){
返回递归(输入,真,0);
}
私有整数递归(列表子列表、布尔canSkip、整数和){
if(sublist.isEmpty()){
回报金额;
}
int skippedSum=Integer.MIN\u值;
int notSkippedSum;
整数=子列表.get(0);
如果(可以跳过){
skippedSum=递归(sublist.sublist(1,sublist.size()),false,sum);
}
notSkippedSum=递归(sublist.sublist(1,sublist.size()),true,整数+和);
返回skippedSum>notSkippedSum?skippedSum:notSkippedSum;
}
}

“我们不能跳过一个或多个连续数字”我真的不明白这一点。在查找最大值时,您必须检查每个值,因为任何值都可能是最大值。那么为什么您要说“不能跳过值”从某种意义上说,跳过一些可能是可取的?我想他实际上的意思是不能跳过超过1个连续的数字,也就是说,在遍历数组时,一次最多可以跳过数组的1个元素。是这样吗,Hitesh?我一点也不知道示例是如何设置的。例如,哪些数字是被取的,哪些是被跳过的。这些输出如何计算?它应该是什么?这里的规则是什么?这是不清楚的…如果你试图找到最大值,在进行计算时也没有跳过元素这样的事情(例如1,最大值是9)。澄清你想要的不是OP想要的
max(一组数)
,而是
max(和(数的子集))
hello@user3150716,我没有得到您的解决方案,我尝试了示例1,但没有得到它是如何工作的,请您用一个示例解释一下,谢谢谢谢Hanks none,它正在工作,但无法理解,我需要检查递归是如何工作的。