I’ll try to explain it better.
So k = 2.
In[]profits = new {1, 2, 3, 1, 6, 10}
For the first part it should be:
1,2,3
Then look for the least value in {1,2,3} which is 1. Then add that to the next value which is 1. So it would be 1+1. The outcome would be {2,3,2}. Again look for the least value which is 2 and add it to the next value 6. So it would be 2+6. The outcome would be {3, 2, 8}. Finally, for the least value in {3,2,8} and add it to the next value 10. So it would be and 10+2. The outcome is {2, 8, 10}. Then look for the least value in that which is 2.
If you guys could at least guide me on how to do this?
This is the tester:
import java.util.Arrays ;
/**
Presents some problems to the BillBoard class.
*/
public class BillboardTester {
public static void main(String[] args) {
int[] profits = new int[]{1, 2, 3, 1, 6, 10} ;
int k = 2 ;
System.out.println("Profits: " + Arrays.toString(profits) + " k = " + k) ;
Billboard bb = new Billboard(profits, k) ;
System.out.println("Maximum Profit = " + bb.maximumProfit()) ;
System.out.println(bb) ;
k = 3 ;
profits = new int[]{7, 4, 5, 6, 1, 7, 8, 9, 2, 5} ;
System.out.println("Profits: " + Arrays.toString(profits) + " k = " + k) ;
bb = new Billboard(profits, k) ;
System.out.println("Maximum Profit = " + bb.maximumProfit()) ;
System.out.println(bb) ;
}
}
This is what I have so far for the class:
public class Billboard {
private int maximumprofit; // The max profit
private int finalcost; // The final cost of removing the billboards
public Billboard(int[]profits, int k) {
for (int i = 0; i < profits.length; i++) {
maximumprofit+= profits[i];
}
}
public int maximumProfit() {
return maximumprofit;
}
}
If I understand your problem correctly, you have
k+1possible totals, and you add each new element of profits to the LOWEST current total profit no matter what it is. So in your example, it’s really1, 2, 31+1=2, 2, 32+6=8, 2, 38, 2+10=12, 3For a final array of
8, 12, 3-> Lowest value is actually3If this is what you meant (you never said what
kis), this code will do the job. I left a line of debugging code in there to help you check for correctness.This code outputs
To change what the
Billboardclass outputs when you put it inSystem.out.println(bb), you have to override thetoStringmethod of theBillboardclass, i.e.Assuming you set the fields in the class to have the correct values earlier.