502. IPO (List 与 vector 中的 erase 对比)

题目链接:

https://leetcode-cn.com/problems/ipo/

题目大意:

中文题目

具体思路:

贪心。   在进行高效率的存储的时候 ,vector 是比 list快的;但是如果有大量的增加和删除节点的操作,list更快一点   。list中sort的调用方法, sto.sort()。

AC代码:

class Solution {
public:
    
struct node{
int p, c;

node(){}
node(int xx,int yy){
p = xx;
c = yy;
}

bool friend operator < (node t1 , node t2){

  if( t1.p != t2.p )
    return t1.p > t2.p;
 return t1.c < t2.c;
}

}  ;

int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital)
{

    list<node>sto;

      int len = Profits.size() , ans = W;

      for(int i = 0 ; i < len ; i++){
        sto.push_back(node(Profits[i] , Capital[i]));
      }
      
      sto.sort();

      for(int i = 0 ; i < k; i++ ){
          
        if(sto.size() == 0)break;
        
        for(auto j = sto.begin(); j != sto.end() ; j++){

            if( j->c <= ans ){
                ans += j->p;
                sto.erase(j);
                break;
            }
        }
      }

    return ans;
}
    
};
    

猜你喜欢

转载自www.cnblogs.com/letlifestop/p/11438294.html