diff --git a/10 May Max Profit from Two Machines b/10 May Max Profit from Two Machines new file mode 100644 index 0000000..888708e --- /dev/null +++ b/10 May Max Profit from Two Machines @@ -0,0 +1,34 @@ +class Solution { +public: + int maxProfit(int x, int y, vector &a, vector &b) { + + int n = a.size(); + + vector idx(n); + + for (int i = 0; i < n; i++) + idx[i] = i; + + // Important tasks first + sort(idx.begin(), idx.end(), [&](int i, int j) { + return abs(a[i] - b[i]) > abs(a[j] - b[j]); + }); + + long long ans = 0; + + for (int id : idx) { + + // Prefer A if profit larger + if ((a[id] >= b[id] && x > 0) || y == 0) { + ans += a[id]; + x--; + } + else { + ans += b[id]; + y--; + } + } + + return ans; + } +};