Finding the maximum total in Java

Suppose you have a set of numbers as shown below:

[A,B,C]

[6, 4.5, 6]
[7, 6, 5]
[9, 4.5, 6]

To find the largest amount, you can use only one number from each set of AND categories (A, B or C). In this case, A = 9, B = 6, and C = 6 will produce the largest amount of 21. The largest amount cannot be 22 (9 + 7 + 6), because 9 and 7 conflict with each other as category A.

How to do it in Java?

It’s hard for me to find the largest amount, because choosing the highest value in each category does not guarantee the largest amount. Some categories may be forced to lower values, reducing the amount. Remember that only one number from each set AND category can be selected.

+5
source share
3 answers

It sounds a bit like the Eight Queens Puzzle , where you need to place 8 queens on a chessboard without any of them in the way of the other. (If you do not know chess, do not worry by analogy).

Suppose your array of examples:

[6, 4.5, 6]
[7,   6, 5]
[9, 4.5, 6] 

Find the largest value as a whole (in this case 9) and lock its column and row.

Your new array looks like this (with x how the selection is no longer valid).

[x, 4.5, 6]
[x,   6, 5]
[x,   x, x]

Repeat this process over and over until you select one value from each column and each row.

Now, as a warning, the presence of several places for the current max (as in the second stage of the example, with two 6s) leads to several more conditions. I will stay with pleasure with you, but I will be happy to provide you with additional assistance if necessary.

Attention

, . ( :

[10, 9, 1]
[ 9, 9, 1]
[ 1, 1, 1]

, .

+1

N, N - . Matrix[i][Permutation[i]] .

0

, .

, 2d

int [] [] data = new int [rows] [columns]

A, B, C ..

, :

data[i][fix],

, A, 2d-, , :

int [] [] data = new int [3][3];

, A from, data [0][0], data[1][0] data[2][0]

EDIT:

Here is one possible solution for you.

//here is our data array.
int [][] data = new int[3][];

//fill up with som random data
data[0] = new int[]{10,20,4,5,56,87,9};
data[1] = new int[]{1,65,0,10,3};
data[2] = new int[]{34,5,6,67,3,54};

//get the biggest arrays length
int maxArrayLength = 0;
for(int i=1; i<data.length; i++)
{
    if(data[i].length > data[maxArrayLength].length)
        maxArrayLength = i;
}
maxArrayLength = data[maxArrayLength].length;

//collect the max in each category
int [] categoryMax = new int[maxArrayLength];

//loop through the categories
for(int i=0; i<maxArrayLength; i++)
{
    //in each iteration we get a winner
    int categoryWinner = 0;

    // now loop through the values of the current category
    for(int j=0; j<data.length; j++)
    {
        int [] actualArray = data[j];
        int [] winnerArray = data[categoryWinner];

        //check if we are in the bounds, if so, then perform a simple maxsearch
        if(i<actualArray.length)
            if(actualArray[i] > winnerArray[i])
            categoryWinner = j;
    }

    //set the current element of the winners array.
    categoryMax [i] = data[categoryWinner][i];
}

int sum = 0;

// we are ready, print it out!
for(int i=0; i<categoryMax.length; i++)
{
    System.out.println((i+1) + ". groups winner: " + categoryMax[i]);
    sum+=categoryMax[i];
}
System.out.println(sum);
-2
source

All Articles