Business Logic Coded | Commented and Explained | Straigtforward
Intuition\n Describe your first thoughts on how to solve this problem. \nThis is actually a straightforward problem for those with a business degree background,
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is actually a straightforward problem for those with a business degree background, you just need to look at it as such. When a business proposal is put forward, a list of sunk costs, opportunity costs, opportunities, and max upfront costs are laid out. These are explained as followed \n\nSunk costs are where you are going to need to spend the money for no gain whatsoever. These are to be avoided in business, but sometimes are necessary \n\nOpportunities are chances to make some money back (ie profit is not 0) but you will still lose some money on going into this (your ventures) \n\nOpportunity costs are the costs of exploring those opportunities \n\nUpfront costs are the costs of doing business where you will gain a profit \n\nThe max upfront cost is the largest of these \n\nAll together then, you can see that the capital needed for the business venture is the sum of the sunk costs, the opportunity costs, and the maximum opportunities overall loss (either the max upfront cost, or the max of the opportunities profits, whichever is greater, is needed to kick off the capital cycle)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFollowing the above logic, you can lay out a business proposal spreadsheet, which is what this hard is likely really about. \nAdmittedly, this is not the best layout. Done right, you can run this in one loop instead of four list comprehensions and get this up with the top score (which this is what they do, just not explained as well). However, this way lets you show the use cases and explain it well in interview, which is ultimately better to show overall. \n\n# Complexity\n- Time complexity: O(N) \n\n- Space complexity: O(N) \n\n# Code\n```\nclass Solution:\n    def minimumMoney(self, transactions: List[List[int]]) -> int:\n        # in order to complete a transaction i, money >= cost_i \n        # after transaction, money becomes money - cost_i + change_i \n        # return the minimum amount of money required before any transaction so that \n        # all of the transactions can be completed, regardless of order \n            # return the minimum amount of money required so that transactions \n            # with the highest cost and lowest cashback can be completed \n        # business logic problem \n        # the capital needed is what the business requires to be succesful through the stages of the coming transactions \n        capital_needed = 0 \n        # sunk costs are those where you must spend money and get nothing for it \n        sunk_costs = [transaction[0] for transaction in transactions if transaction[1] == 0] \n        # opportunities are chances to make money where you\'ll need to spend some and it\'s not a sunk cost \n        opportunities = [transaction[1] for transaction in transactions if (transaction[0] > transaction[1] and transaction[1] != 0)]\n        # opportunity costs are the differences in costs and profit when you will make some profit but ultimately lose capital overall\n        opportunity_costs = [cost - profit for cost, profit in transactions if (cost > profit and profit != 0)] \n        # upfront_costs is the costs where costs are strictly matched out by the profits -> cost of doing business \n        upfront_costs = [transaction[0] for transaction in transactions if transaction[0] <= transaction[1]]\n        # max upfront cost is the max of those if it exists, otherwise 0 \n        max_upfront_cost = 0 if len(upfront_costs) == 0 else max(upfront_costs)\n        # opportunities has the max upfront cost appended so we have to consider it as part of the capital layout \n        opportunities.append(max_upfront_cost) \n        # capital needed is the total of your sunk costs, plus the total of your losers where you make some money, plus the max of \n        # either the opportunities profit, or the max upfront cost. If the opportunities profit, you need at least that much to make\n        # up for the opportunities costs. If max upfront cost, you need at least that much to kick off the virtuous cycle of doing business \n        capital_needed = sum(sunk_costs) + sum(opportunity_costs) + max(opportunities)\n        return capital_needed\n```
['Python3']