502. IPO

Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the IPO. Since it has limited resources, it can only finish at mostkdistinct projects before the IPO. Help LeetCode design the best way to maximize its total capital after finishing at mostkdistinct projects.

You are given several projects. For each projecti, it has a pure profitPiand a minimum capital ofCiis needed to start the corresponding project. Initially, you haveWcapital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital.

To sum up, pick a list of at mostkdistinct projects from given projects to maximize your final capital, and output your final maximized capital.

Example 1:

Input:
 k=2, W=0, Profits=[1,2,3], Capital=[0,1,1].


Output:
 4


Explanation:
 Since your initial capital is 0, you can only start the project indexed 0.
             After finishing it you will obtain profit 1 and your capital becomes 1.
             With capital 1, you can either start the project indexed 1 or the project indexed 2.
             Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital.
             Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4.

Note:

  1. You may assume all numbers in the input are non-negative integers.
  2. The length of Profits array and Capital array will not exceed 50,000.
  3. The answer is guaranteed to fit in a 32-bit signed integer.

Analysis:

At any point, we follow simple strategy, that is to pick out one most profitable project that has a capital requirement less than W.

first, use a treemap to bucket all Profits based on Capital. key is capital, value here is a list of Profits. Put it all in the treemap.

second, for loop for k times. every round, we put all "do-able(capital requirement <= W)" projects' profits into maxheap, and remove the entry from map after dumping into treemap. poll one out and add it to W.

If maxheap at anypoint is empty, that means with current capital, we cannot do any project anymore, dont need to wait k round, we can break loop and return W;

Code:

public class Solution {
    public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) {
        if(Profits == null || Profits.length == 0)
        {
            return W;
        }

        TreeMap<Integer, List<Integer>> map = new TreeMap<Integer, List<Integer>>();
        for(int i=0; i<Profits.length; i++)
        {
            if(map.get(Capital[i]) == null)
            {
                map.put(Capital[i], new LinkedList<Integer>());
            }
            map.get(Capital[i]).add(Profits[i]);
        }

        PriorityQueue<Integer> maxheap = new PriorityQueue<Integer>(Profits.length, Collections.reverseOrder());
        for(int i=0; i<k; i++)
        {
            Iterator<Map.Entry<Integer, List<Integer>>> itr = map.entrySet().iterator();
            while(itr.hasNext())
            {
                Map.Entry<Integer, List<Integer>> cur = itr.next();
                if(cur.getKey().compareTo(W) <= 0)
                {
                    for(Integer prof : cur.getValue())
                    {
                        maxheap.offer(prof);
                    }
                    itr.remove();
                }
                else
                {
                    break;
                }
            }
            if(maxheap.isEmpty())
            {
                return W;
            }
            W += maxheap.poll();
        }
        return W;
    }
}

results matching ""

    No results matching ""