Currency maximization in Java

Posted by dubrubru on Sat, 19 Oct 2019 20:41:43 +0200

1 problem description
Given a row of n coins, their denominations are all positive integers c1, c2,... cn, these integers are not necessarily two different. Please tell me how to select coins so that the total amount of coins selected is the largest when their original positions are not adjacent to each other.

2 solutions
2.1 dynamic planning method
The code idea in this paper is explained in the third edition of "Fundamentals of algorithm design and analysis", as follows:

package com.liuzhen.chapter8;

import java.util.ArrayList;

public class CoinRow {
    /*
     * Parameter Money: the number of denominations of a given coin combination
     * Function function: return the array subscript and the maximum total amount of coin combination in the form of array linked list.
     * The last element in the chain list is the maximum total amount, and other elements are the subscript of coin combination array.
     */
    public ArrayList<Integer> getMaxSumCoin(int[] Money){
        ArrayList<Integer> list = new ArrayList<Integer>();
        if(Money.length == 1){             //When there is only one coin, the maximum total amount is that coin
            list.add(0);                   //Coin storage location
            list.add(Money[0]);            //Total amount of coins deposited
            return list;
        }
        
        int[] tempMaxSum = new int[Money.length];     //The maximum total amount used to store traversal to the current coin position (traversal from front to back)
        tempMaxSum[0] = Money[0];
        if(Money[0] < Money[1])
            tempMaxSum[1] = Money[1];
        else
            tempMaxSum[1] = Money[0];
        
        for(int i = 2;i < Money.length;i++){
            if(tempMaxSum[i-1] >= tempMaxSum[i-2] + Money[i])
                tempMaxSum[i] = tempMaxSum[i-1];
            else
                tempMaxSum[i] = tempMaxSum[i-2] + Money[i];
        }
        
        System.out.println("\n Maximum amount of coins in current position:");
        for(int i = 0;i < Money.length;i++)
            System.out.print(tempMaxSum[i]+" ");
        //According to the tempMaxSum array element, find out the array subscript of the coin combination element with the maximum amount.
        for(int i = Money.length-2;i >=0;i--){
            int temp = tempMaxSum[i];
            if(temp < tempMaxSum[i+1]){
                list.add(i+1);             //Store the subscript of the maximum amount coin combination element array
                temp = tempMaxSum[i+1] - Money[i+1];
                for(int j = 0;j < Money.length;j++){   //Found the first element in the tempMaxSum array (sorted from small to large) equal to the temp value
                    if(tempMaxSum[j] == temp){
                        i = j;
                        break;
                    }
                }
            }
        }
        if(Money[0] >= Money[1])      //No matter how you choose the coin combination, you must choose one of the first two coins
            list.add(0);
        else
            list.add(1);
        list.add(tempMaxSum[Money.length-1]);     //Maximum total amount of coins stored
        return list;
    }
    
    public static void main(String[] args){
        CoinRow test = new CoinRow();
        int[] Money = {1,1,2,10,6,2,10,8,12};
        
        System.out.println("Amount of coins in current position:");
        for(int i = 0;i < Money.length;i++)
            System.out.print(Money[i]+" ");
        
        ArrayList<Integer> list = test.getMaxSumCoin(Money);
        
        System.out.println("\n The array subscripts of the coin combination with the maximum total amount are as follows:");
        for(int i = 0;i < list.size()-1;i++)
            System.out.print(list.get(i)+" ");
        
        System.out.println("\n The corresponding face value of the object array subscript of the coin combination with the maximum total amount is:");
        for(int i = 0;i < list.size()-1;i++)
            System.out.print(Money[list.get(i)]+" ");
        
        System.out.println("\n"+"The maximum total amount is:");
        System.out.println(list.get(list.size()-1));
    }
}

Operation result:

Amount of coins in current position:
1 2 10 6 2 10 8 12 
Maximum amount of coins in current position:
1 3 11 11 13 21 21 33 
The array subscripts of the coin combination with the maximum total amount are as follows:
6 3 0 
The corresponding face value of the object array subscript of the coin combination with the maximum total amount is:
10 10 1 
The maximum total amount is:

Topics: Java