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.
Input: N = 3, Earning=[1, 2, 4], Cost=[1, 3, 6], E = 5
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
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:
- For each element in earnings and cost, compare earning[i] with cost[i].
- 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.
- 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.
- 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.
- Print the total profit after the above steps.
Below is the implementation of the above approach:
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.
- Nuts & Bolts Problem (Lock & Key problem) | Set 1
- Secretary Problem (A Optimal Stopping Problem)
- Transportation Problem | Set 7 ( Degeneracy in Transportation Problem )
- Nuts & Bolts Problem (Lock & Key problem) | Set 2 (Hashmap)
- Applications of Minimum Spanning Tree Problem
- The Knight's tour problem | Backtracking-1
- Josephus problem | Set 1 (A O(n) Solution)
- Activity Selection Problem | Greedy Algo-1
- K Centers Problem | Set 1 (Greedy Approximate Algorithm)
- Set Cover Problem | Set 1 (Greedy Approximate Algorithm)
- Hungarian Algorithm for Assignment Problem | Set 1 (Introduction)
- Frobenius coin problem
- Bin Packing Problem (Minimize number of used Bins)
- Job Sequencing Problem | Set 2 (Using Disjoint Set)
- The Lazy Caterer's Problem
- Chocolate Distribution Problem
- Max Flow Problem Introduction
- Gold Mine Problem
- Perfect Sum Problem (Print all subsets with given sum)
- Josephus Problem Using Bit Magic
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@example.com. 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.