502 IPO 上市

详见:https://leetcode.com/problems/ipo/description/

C++:

class Solution {
public:
    int findMaximizedCapital(int k, int W, vector<int>& Profits, vector<int>& Capital) 
    {
        vector<pair<int, int>> v;
        for (int i = 0; i < Capital.size(); ++i)
        {
            v.push_back({Capital[i], Profits[i]});
        }
        sort(v.begin(), v.end());
        for (int i = 0; i < k; ++i)
        {
            int left = 0, right = v.size(), mx = 0, idx = 0;
            while (left < right)
            {
                int mid = left + (right - left) / 2;
                if (v[mid].first <= W)
                {
                    left = mid + 1;
                }
                else
                {
                    right = mid;
                }
            }
            for (int j = right - 1; j >= 0; --j) 
            {
                if (mx < v[j].second) 
                {
                    mx = v[j].second;
                    idx = j;
                }
            }
            W += mx;
            v.erase(v.begin() + idx);
        }
        return W;
    }
};

 参考:http://www.cnblogs.com/grandyang/p/6788811.html

posted on 2018-04-22 13:55  lina2014  阅读(138)  评论(0编辑  收藏  举报

导航