0% found this document useful (0 votes)
45 views

Solution

The document defines a Result class with a main method that calculates the maximum profit from a list of days. It initializes a list of days with earnings and costs, calculates the profit using a calculateProfit method, and prints the result. The calculateProfit method filters profitable days, finds the most profitable starting day, and calculates the total profit by iterating through days and updating remaining energy.

Uploaded by

Doruk Kangal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Solution

The document defines a Result class with a main method that calculates the maximum profit from a list of days. It initializes a list of days with earnings and costs, calculates the profit using a calculateProfit method, and prints the result. The calculateProfit method filters profitable days, finds the most profitable starting day, and calculates the total profit by iterating through days and updating remaining energy.

Uploaded by

Doruk Kangal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

class Result {

public static void main(String[] args) {


int energy = 15;
List<Day> days = new ArrayList<>();
days.add(new Day(0, 2, 4));
days.add(new Day(1, 5, 4));
days.add(new Day(2, 10, 4));
days.add(new Day(3, 8, 1));
days.add(new Day(4, 5, 5));
days.add(new Day(5, 3, 1));
days.add(new Day(6, 1, 4));
days.add(new Day(7, 4, 4));
days.add(new Day(8, 6, 3));
days.add(new Day(9, 8, 5));
days.add(new Day(10, 6, 1));

int result = calculateProfit(


days.size(),
days.stream().map(Day::getEarning).collect(Collectors.toList()),
days.stream().map(Day::getCost).collect(Collectors.toList()),
energy
);
System.out.println(result);
}

public static int calculateProfit(int n, List<Integer> earning, List<Integer>


cost, int e) {
System.out.println("int energy = " + e + ";");
System.out.println("List<Day> days = new ArrayList<>();");

List<Day> days = new ArrayList<>();


for (int i = 0; i < n; i++) {
Day day = new Day(i, earning.get(i), cost.get(i));
System.out.println(day);
days.add(day);
}

List<Day> profitableDays = days.stream()


.filter(Day::isProfitable)
.collect(Collectors.toList());

int energyLimit = e;
int totalProfit = 0;
if (profitableDays.isEmpty()) {
totalProfit = days.get(days.size() - 1).getEarning() * e;
} else {
Day startDay = new Day(-1, Integer.MIN_VALUE, Integer.MAX_VALUE);
for (Day day : profitableDays) {
if (day.getProfit() > startDay.getProfit()) {
startDay = day;
} else {
break;
}
}
int startDayIndex = Math.max(0, profitableDays.indexOf(startDay));

for (int i = startDayIndex; i < profitableDays.size(); i++) {


Day day = profitableDays.get(i);
int profit = e * day.getEarning();
e = 0;
if (day.getIndex() != (n - 1)) {
e = Math.min(profit / day.getCost(), energyLimit);
profit -= e * day.getCost();
}
totalProfit += profit;
}
}

return totalProfit;
}
}

class Day {
private int index;
private int earning;
private int cost;
private int profit;

public Day(int index, int earning, int cost) {


this.index = index;
this.earning = earning;
this.cost = cost;
this.profit = earning - cost;
}

public int getIndex() {


return index;
}

public int getEarning() {


return earning;
}

public int getCost() {


return cost;
}

public int getProfit() {


return profit;
}

public boolean isProfitable() {


return profit > 0;
}

@Override
public String toString() {
return "days.add(new Day(" + index +
", " + earning +
", " + cost +
"));";
}
}

You might also like