502.IPO

本周选择了贪心算法类的题型加以巩固,在LeetCode中难度系数为Hard。

题目:

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.

示例:

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.

题解:

      这种题型属于greedy类,为了能在允许条件下执行相应任务,则现有的W必须大于等于该任务的Capital。首先先对所有的Capital进行排序,当前最优选择就是在所有的能执行的任务中,选择能获益Profits最大的任务。且这个选择也是下一个子问题的最优选择。

      则对当前问题,可以分解为,每执行一次任务,先找出所有能被执行的任务,再从中挑选获益最大的任务。

      最开始我选择的是用冒泡排序,根据Profits值从大到小排序,当Profits相同时,则根据Capital的值从小到大排序。每次执行task则从头开始遍历,当遍历到第一个可以执行的任务时,就执行该任务,并标记起来。当然这种方法是可行的,也能得到正确的答案,但是若用这种方法来提交则会提示超时。测试用例中有一道给出了一个极端的情况,即Profits={1,2,3,4,...,5000},Capital={1,2,3,4,...,5000}。如果用前面提到的方法来算则每次执行任务时都需要从头到尾遍历一遍,非常耗时。故需要用到一开始提到的题解思路来做,以尽可能的减少遍历时间,优化算法。

      构建一个结构体来存储Profits和Capital,目的是当基于Profits值或者Capital排序时,另一个相关值能随之变化。将数据压入priority_queue队列中,根据Capital的数值以小到大进行排序。每执行一次任务,便将可执行的任务从队列1中移出,并压入队列2中。队列2是根据Profits的值从大到小进行排序,每次返回队列顶部的任务作为此次执行的任务,必定是在当前可执行任务所有选择中获益最大的任务,也是最优的选择。以此类推,实现代码如下:

代码:

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <queue>
 5 
 6 class Solution {
 7 public:
 8     //定义一个结构体用于存储profit和capital
 9     struct N
10     {
11         int profit;
12         int capital;
13         N(int p, int c){
14             profit = p;
15             capital = c;
16         }
17     };
18     //根据capital的数值进行排序,从小到大
19     struct cmp1{
20         bool operator () (N n1, N n2){ return n1.capital>n2.capital; }
21     };
22     //根据profit的数值进行排序,从大到小
23     struct cmp2{
24         bool operator () (N n1, N n2){ return n1.profit < n2.profit; }
25     };
26     int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) {
27         std::priority_queue<N, vector<N>, cmp1> C;
28         std::priority_queue<N, vector<N>, cmp2> P;
29         //先对所有的任务根据Capital的数值进行排序,从小到大
30         for (int i = 0; i < Profits.size(); ++i)
31         {
32             N temp = { Profits[i], Capital[i] };
33             C.push(temp);
34         }
35         int tasksize = k < Profits.size() ? k : Profits.size();
36         //可执行的任务中根据Profits的数值进行排序,从大到小
37         for (int i = tasksize; i > 0; i--){
38             while (!C.empty() && C.top().capital <= W){
39                 N temp = C.top();
40                 P.push(temp);
41                 C.pop();
42             }
43             if (!P.empty()){
44                 W += P.top().profit;
45                 P.pop();
46             }
47         }
48         return W;
49     }
50 };
原文地址:https://www.cnblogs.com/MT-ComputerVision/p/6641579.html