Leetcode part of the puzzle

Source: Internet
Author: User
Tags sort
Leetcode algorithm part. 502 "502" IPO

Suppose Leetcode would start its IPO soon. In order to sell a good the price of it shares to Venture Capital, Leetcode would like to work on some projects to increase I TS Capital before the IPO. Since It has limited resources, it can only finish at the most k distinct projects before the IPO. Help Leetcode design the best-of-the-maximize it total capital after finishing at the most k distinct projects.

You are given several projects. For each project I, it had a pure profit Pi and a minimum capital of Ci was needed to start the corresponding project. Initially, you have a W capital. When you finish a project, you'll obtain its pure profit and the profit'll be added to your total capital.

To sum up, pick a list of for 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 are 0, you can only start the project indexed 0.
After finishing it, you'll 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:
Assume all numbers in the input is non-negative integers.
The length of profits array and capital array would not exceed 50,000.
The answer is guaranteed to fit in a 32-bit signed integer.

The main idea is to realize the maximization of funds. I realized it through the idea of greedy arithmetic, every step to maximize the profit. Here's my implementation.

    static bool Cmp1 (Pair<int, int> pc1, Pair<int, int> pc2) {return Pc1.first < Pc2.first;
    } static bool Cmp2 (Pair<int, int> pc1, Pair<int, int> pc2) {return Pc1.second < Pc2.second; } int findmaximizedcapital (int k, int W, vector<int>& profits, vector<int>& capital) {V
        Ector<pair<int, int>> pc;
        Pc.resize (Profits.size ());
            for (int i = 0; i < profits.size (); i++) {pc[i].first = profits[i];
        Pc[i].second = Capital[i];
        } sort (Pc.begin (), Pc.end (), CMP1);
        Sort (Pc.begin (), Pc.end (), CMP2);
        int sum = W; for (int i = 0, i < K; i++) {for (int j = pc.size ()-1; J >= 0; j--) {if (Pc[j].second &lt
                    ; = sum) {Sum+=pc[j].first;
                    Pc.erase (Pc.begin () +j);
                Break
    }}} return sum; }

It is possible that the algorithm has some flaws and has been modified. 33 Examples of 5 can not be passed, but also hope that people criticize correct.

Below I refer to other people's algorithms, which are responsible for about O (NLOGN)

struct Node {int profit, capital;}; int findmaximizedcapital (int k, int W, vector<int>& profits, vector<int>& capital) {if (Profit S.empty () | |
        Capital.empty ()) return W;
        Vector<node*> projects;
        for (int i = 0; i < profits.size (); i++) Projects.push_back (new Node ({profits[i], capital[i]});
        Multiset<int> PQ;
        Sort (Projects.begin (), Projects.end (), [&] (node* N1, node* n2) {return n1->capital < n2->capital;}); for (Auto start = Projects.begin (), K > 0; k--) {for (; Start! = Projects.end () && (*start)->cap Ital <= W;
                start++) {Pq.insert (*start)->profit);
            if (Pq.size () > K) pq.erase (Pq.begin ());
            } if (Pq.empty ()) break;
            W + = *pq.rbegin ();
        Pq.erase (prev (Pq.end ()));
    } return W; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.