zoukankan      html  css  js  c++  java
  • 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 most k distinct projects before the IPO. Help LeetCode design the best way to maximize its total capital after finishing at most k distinct projects.

    You are given several projects. For each project i, it has a pure profit Pi and a minimum capital of Ci is needed to start the corresponding project. Initially, you have W capital. 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 most k distinct 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.
     

    Approach #1: C++.

    class Solution {
    public:
        int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
            priority_queue<int> pq;
            vector<pair<int, int>> temp;
            for (int i = 0; i < Capital.size(); ++i) 
                temp.push_back(make_pair(Capital[i], Profits[i]));
            sort(temp.begin(), temp.end());
            int index = 0;
            while (k--) {
                while (index < Capital.size() && temp[index].first <= W) {
                    pq.push(temp[index].second);
                    index++;
                }
                if (pq.empty()) break;
                W += pq.top();
                pq.pop();
            }
            return W;
        }
    };
    

      

    Approach #2: Java.

    class Solution {
        public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) {
            PriorityQueue<int[]> pqCap = new PriorityQueue<>((a, b)->(a[0] - b[0]));
            PriorityQueue<int[]> pqPro = new PriorityQueue<>((a, b)->(b[1] - a[1]));
            
            for (int i = 0; i < Profits.length; ++i) {
                pqCap.add(new int[] {Capital[i], Profits[i]});
            }
            
            for (int i = 0; i < k; ++i) {
                while (!pqCap.isEmpty() && pqCap.peek()[0] <= W) {
                    pqPro.add(pqCap.poll());
                }
                
                if (pqPro.isEmpty()) break;
                
                W += pqPro.poll()[1];
            }
            
            return W;
        }
    }
    

      

    Approach #3: Python.

    class Solution(object):
        def findMaximizedCapital(self, k, W, Profits, Capital):
            """
            :type k: int
            :type W: int
            :type Profits: List[int]
            :type Capital: List[int]
            :rtype: int
            """
            heap = []
            projects = sorted(zip(Profits, Capital), key=lambda l:l[1])
            i = 0
            for _ in range(k):
                while i < len(projects) and projects[i][1] <= W:
                    heapq.heappush(heap, -projects[i][0])
                    i += 1
                if heap: W -= heapq.heappop(heap)
            return W
    

      

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    替换URL传递的参数
    执行SQl语句得到xml结果集
    table中文本太长换行
    org.xml.sax.SAXNotRecognizedException
    WAMP+CMSeasy快速搭建学校网站
    推荐几个web前台开发的小工具
    来园子里注册啦
    C++ Virtual的背后
    Games101观后补充笔记
    Lua语法入门
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10152777.html
Copyright © 2011-2022 走看看