3

Lead a life problem

 3 years ago
source link: https://www.geeksforgeeks.org/lead-a-life-problem/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Lead a life problem

Last Updated: 24-08-2020

You are working in Samara, Russia for a few days. Each day has a new pay per unit of work and a new cost per unit of food. Working 1 unit costs 1 unit of energy, and eating 1 unit of food adds 1 unit of energy. Here are some specifications of your employment: 

  • You arrive with no money, but with energy.
  • You can never have more energy than you arrive with, and it can never be negative.
  • You can do any amount of work every day (possibly not do any work at all), limited only by your energy.
  • You cannot work when your energy is zero. You can eat any amount of food every day (possibly not have any food at all), limited by the money you have.
  • You cannot eat when the money you have is zero. You can eat food at the end of the day, and cannot return to work after eating.
  • You can return to work the next day.

Your true goal is to return home with as much money as possible. Compute the maximum amount of money you can take home.

Examples:

Input: N = 3, Earning=[1, 2, 4], Cost[]=[1, 3, 6], E = 5
Output: 20
Explanation:
Day 1: 1 unit work is worth 1, and 1 unit food costs 1. There is no financial incentive to go to work this day.
Day 2: 1 unit work earns 2, and 1 unit food costs 3. Thus, you spend more to eat then the total earning so there is no financial incentive to go to work on this day.
Day 3: You earn 4 units per unit of work. The cost of food is irrelevant this day, as you are leaving for the home straight from work.
You spend all of your energy working, the pay is 5 × 4 = 20 units of money and go home without buying dinner.

Input: N=2, Earning=[1, 2], Cost=[1, 4], E=5
Output: Total Profit = 0 + 10 = 10
Explanation: 
First Day: Skip
Second Day: 5*2=10

Approach: The approach is to traverse the given earning array and the cost array and calculate the net profit for each day. Below are the steps:

  1. For each element in earnings[] and cost[], compare earning[i] with cost[i].
  2. If earning[i] is less than or equal to cost[i], then skip the work on that day as the cost of expenses is greater than the earning. Thus, making no profit at all.
  3. If earning[i] is greater than to cost[i], calculate the total earnings by multiplying earning on that day with total units of energy and then subtract the product of the cost of food on that day and units of energy to calculate profit for that day.
  4. If there is last working day, then calculate the total earnings by multiplying earning on that day with total units of energy and that will be your profit for that day as no more energy is required for work.
  5. Print the total profit after the above steps.

Below is the implementation of the above approach:

  • Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <iostream>
using namespace std;
// Function that calculates the profit
// with the earning and cost of expenses
int calculateProfit(int n, int* earnings,
int* cost, int e)
{
// To store the total Profit
int profit = 0;
// Loop for n number of days
for (int i = 0; i < n; i++) {
int earning_per_day = 0;
int daily_spent_food = 0;
// If last day, no need to buy food
if (i == (n - 1)) {
earning_per_day = earnings[i] * e;
profit = profit + earning_per_day;
break;
}
// Else buy food to gain
// energy for next day
if (cost[i] < earnings[i]) {
// Update earning per day
earning_per_day = earnings[i] * e;
daily_spent_food = cost[i] * e;
// Update profit with daily spent
profit = profit + earning_per_day
- daily_spent_food;
}
}
// Print the profit
cout << profit << endl;
}
// Driver Code
int main()
{
// Given days
int n = 4;
// Given earnings
int earnings[] = { 1, 8, 6, 7 };
// Given cost
int cost[] = { 1, 3, 4, 1 };
// Given energy e
int e = 5;
// Function Call
calculateProfit(n, earnings, cost, e);
}
Output: 
70

Time Complexity: O(N), where N is the number of days
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
thumb_up
Be the First to upvote.
3.3

Based on 3 vote(s)
Please write to us at [email protected] to report any issue with the above content.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK