Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.2 kB
3
Indexable
Never
struct max_profit max_profit_period(struct day *day_one, int year) {
    // TODO: implement this function
    struct day *current = day_one;

    double max_profit = -10000000.00;
    double current_sum = 0;
    struct max_profit max_profit_struct = {0};
    
    struct date current_start_date = day_one->date;

    while (current != NULL) {
        if (current->date.year == year) {
            if (isWeekend(current->date)) {
                current_sum += (total_income(current) - 50);
            }

            else {
                current_sum += (total_income(current) - 35);
            }
            
            if (current_sum < 0) {
                current_sum = 0;
                current_start_date = current->date;
            }

            if (current_sum > max_profit) {
                max_profit = current_sum;
                max_profit_struct.start_date = current_start_date;
                max_profit_struct.end_date = current->date;
            }
        }
        current = current->next;

    }

    max_profit_struct.profit = max_profit;

    if (compare_date(max_profit_struct.end_date, max_profit_struct.start_date) == 0) {
        max_profit_struct.end_date.year = 0;
    }

    return max_profit_struct;

}
Leave a Comment