[Java/C++/Python] Easy and Coincise 🔥
Intuition\nThe worst case is losing all money first,\nthen we earn money later.\n\n\n# Explanation\nFirst we calculate the sum of all money we can lose.\nThat i
# **Intuition**\nThe worst case is losing all money first,\nthen we earn money later.\n<br>\n\n# **Explanation**\nFirst we calculate the sum of all money we can lose.\nThat is the sum of lost money is\n`sum_lose = sum(max(0, cost[i] - cashback[i]))`\n\nAssume before the last transaction thet lose money,\nwe have `money = cost[i]`,\nwe spent all money as cost for this transaction,\nwe have no money left.\nAfter transaction, we have `money = cashback[i]`.\n\nSo the initial money is at least `sum_lose + cashback[i]`\nand after all losing money,\nwe need at least `max(cost[i])`,\nwhere `cost[i]` is the cost for all earning money transaction.\n<br>\n\n# **Complexity**\nTime `O(n)`\nSpace `O(1)`\n<br>\n\n**Java**\n```java\n    public long minimumMoney(int[][] A) {\n        long res = 0; int v = 0;\n        for (int[] a : A) {\n            v = Math.max(v, Math.min(a[0], a[1]));\n            res += Math.max(a[0] - a[1], 0);\n        }\n        return res + v;\n    }\n```\n\n**C++**\n```cpp\n    long long minimumMoney(vector<vector<int>>& A) {\n        long long res = 0; int v = 0;\n        for (auto& a : A) {\n            v = max(v, min(a[0], a[1]));\n            res += max(a[0] - a[1], 0);\n        }\n        return res + v;\n    }\n```\n\n**Python**\n```py\n    def minimumMoney(self, A):\n        res = v = 0\n        for i,j in A:\n            res += max(0, i - j)\n            v = max(v, min(i, j))\n        return res + v\n```\n**Python 1-line**\n```py\n    def minimumMoney(self, A):\n        return sum(max(0, i - j) for i, j in A) + max(map(min, A))\n```\n
['C', 'Python', 'Java']