Java algorithm problem

Problem statement: in Java, given an integer array, can you select a group of integers so that the group is added to the given target, and has the following additional constraints: if there are numbers in the array, they must be selected or none of them must be selected For example, for the array {1,2,5,2}, you must select or deselect all three 2 in the middle, all as a group (a loop can be used to find the range of the same value)

groupSumClump(0,{2,4,8},10) → true      
 groupSumClump(0,{1,8,1},14) → true                   
 groupSumClump(0,14) → false --> Failing Test Case               
 groupSumClump(0,{8,9) → true   --> Failing Test Case      
 groupSumClump(0,11) → false --> NegativeArraySizeException

I did some preliminary analysis, and some of the codes are as follows

public boolean groupSumClump(int start,int[] nums,int target) {
    start = 0;
    boolean flag = false;

    // get the highest int from the list of array we have
    int highestInteger = getTheBiggest(nums);
    if (highestInteger > target) {
        flag = false;
    } else {
        int variable = 0;
        for (int i = 0; i < nums.length; i++) {
            variable += nums[i];
        }
        if (variable == target) {
            flag = true;
        } else {
            if (variable < target) {
                flag = false;
            } else {
                // here goes ur grouping logic here
                flag = summate(highestInteger,target,nums);
            }
        }
    }

    return flag;
}

private  boolean summate(int highestInteger,int target,int[] nums) {
    boolean val = false;
    if (highestInteger == target) {
        val = true;
    } else {
        int[] temp = new int[nums.length - 1];
        int var = 0;            

        if ((target - highestInteger) > 0) {
                 for (int j = 0; j < nums.length-1; j++) {
                   if (nums[j] != highestInteger) {
                     temp[var] = nums[j];
                     if (temp[var] == (target - highestInteger)) {
                        val = true;
                        return val;
                     }
                     var++;
                   }
                 }
             val = summate(getTheBiggest(temp),target - highestInteger,temp);  

         }                                      
     }      
    return val;
}

private int getTheBiggest(int[] nums) {
    int biggestInteger = 0;
    for (int i = 0; i < nums.length; i++) {
        if (biggestInteger < nums[i]) {
            biggestInteger = nums[i];
        }
    }
    return biggestInteger;
}

Please note: I don't know how to deal with the logic of the following problem statement: there is an additional constraint to this problem, that is, if the numbers in the array are adjacent and the values are the same, they must be selected all or not For example, all as a group (a loop can be used to find the range of the same value)

How should I deal with this logic in the above problem I've been trying to do that, I don't know Your advice will be greatly appreciated Culd, you let me know what's wrong with the code / how to deal with the additional constraints in this problem, –:((

Additional constraints mean that you choose to be a group instead of a group So I don't know how to continue If you can help me I would appreciate it

Edit user – > missigno: I have added the following code construct to the main code above, which prints out the wrong value I was wrong.

Groupsumcluster (0,14) → false fails again 2 8 4 the flag is – > true, which is an error

for(int number=0;number<nums.length-1;number++){
      if(nums[number]==nums[number+1]){
          nums[number]=nums[number]+nums[number+1];                                
      }        
    }

Solution

I will convert the array into a simpler array, which can be solved using the previous method by aggregating adjacent values:

{1,2} --> {1,6,2}

You may want to keep some additional bookkeeping information so that you can find the original solution from the solution

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>