502 - IPO

 JAVA

  • class Solution {
        public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
            int n = capital.length;
            PriorityQueue<int[]> q1 = new PriorityQueue<>((a, b) -> a[0] - b[0]);
            for (int i = 0; i < n; ++i) {
                q1.offer(new int[] {capital[i], profits[i]});
            }
            PriorityQueue<Integer> q2 = new PriorityQueue<>((a, b) -> b - a);
            while (k-- > 0) {
                while (!q1.isEmpty() && q1.peek()[0] <= w) {
                    q2.offer(q1.poll()[1]);
                }
                if (q2.isEmpty()) {
                    break;
                }
                w += q2.poll();
            }
            return w;
        }
    }
C++

  • using pii = pair<int, int>;
    
    class Solution {
    public:
        int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
            priority_queue<pii, vector<pii>, greater<pii>> q1;
            int n = profits.size();
            for (int i = 0; i < n; ++i) {
                q1.push({capital[i], profits[i]});
            }
            priority_queue<int> q2;
            while (k--) {
                while (!q1.empty() && q1.top().first <= w) {
                    q2.push(q1.top().second);
                    q1.pop();
                }
                if (q2.empty()) {
                    break;
                }
                w += q2.top();
                q2.pop();
            }
            return w;
        }
    };

Comments