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)
  • 相关阅读:
    Chevy equinox
    回家线路
    salesforce account hierarchy
    IOS8 对flex兼容性问题
    Chrome FeHelper 插件下载地址
    vue 项目抛出警告
    vue 干货
    Error in mounted hook: "TypeError: handlers[i].call is not a function" 原因
    vue 路由知识点(一级路由与二级路由嵌套)
    (转)ORA-01940: cannot drop a user that is currently connected 问题解析
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10152777.html
Copyright © 2011-2022 走看看