"Daily question" IPO

This is the daily question of 2021-9-8 on LeetCode: 「502. IPO」

1. Title Description

Suppose LeetCode is about to start IPO. In order to sell shares to venture capital companies at a higher price, Li Kou hopes to carry out some projects to increase its capital before IPO. Due to limited resources, it can only complete up to k different projects before IPO. Help force buckle design the way to get the maximum total capital after completing up to k different projects.

Here are n items for you. For each project I, it has a net profit profits[i], and the minimum capital[i] required to start the project.

Initially, your capital was w. When you complete a project, you will get a net profit, and the profit will be added to your total capital.

In summary, select a list of up to k different projects from a given project to maximize the final capital and output the maximum capital available.

The answer is guaranteed to be in the range of 32-bit signed integers.

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 from project 0.
After completion, you will get a profit of 1 and your total capital will become 1.
At this point, you can choose to start project 1 or 2.
Since you can select up to two projects, you need to complete project 2 to get the maximum capital.
Therefore, the output of the last maximized capital is 0 + 1 + 3 = 4. 

Example 2:

Input: k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
Output: 6

2. Answer

Greedy thought: the net profit of each investment project should be the largest

  1. First obtain the item array arr and combine the capital and profit by item number
  2. Sort the projects according to the required capital from small to large (if from large to small, it may be impossible to invest at the beginning)
  3. Insert the profits from all qualified projects into the maximum heap
  4. If the heap is not empty, take out the top of the heap, which is the maximum net profit and update your own capital w
  5. If the heap is empty, exit the loop directly because there are no items that meet the conditions to enter the heap
  6. Repeat steps 3 to 5 for a total of k times
  7. Finally, return the final capital w
// Default maximum heap
const defaultCmp = (x, y) => x > y;
// Exchange element
const swap = (arr, i, j) => ([arr[i], arr[j]] = [arr[j], arr[i]]);
// Heap class, default maximum heap
class Heap {
    constructor(cmp = defaultCmp) {
        this.container = [];
        this.cmp = cmp;
    }
    // insert
    insert(data) {
        const { container, cmp } = this;
        container.push(data);
        let index = this.size() - 1;
        while (index) {
            let parent = (index - 1) >> 1;
            if (!cmp(container[index], container[parent])) {
                return;
            }
            swap(container, index, parent);
            index = parent;
        }
    }
    // Eject the top of the heap and return
    pop() {
        const { container, cmp } = this;
        if (!this.size()) {
            return null;
        }

        swap(container, 0, this.size() - 1);
        const res = container.pop();
        const length = this.size();
        let index = 0,
            exchange = index * 2 + 1;

        while (exchange < length) {
            // //In the case of maximum heap: if there is a right node, and the value of the right node is greater than that of the left node
            let right = index * 2 + 2;
            if (right < length && cmp(container[right], container[exchange])) {
                exchange = right;
            }
            if (!cmp(container[exchange], container[index])) {
                break;
            }
            swap(container, exchange, index);
            index = exchange;
            exchange = index * 2 + 1;
        }

        return res;
    }
    // Get heap size
    size() {
        return this.container.length;
    }
}

const findMaximizedCapital = (k, w, profits, capital) => {
    const n = profits.length;
    const arr = new Array(n);
    // Combine capital and profit by item number [capital, net profit]
    for (let i = 0; i < n; i++) {
        arr[i] = [capital[i], profits[i]];
    }
    // Rank projects by required capital from small to large
    arr.sort((a, b) => a[0] - b[0]);

    // Create maximum heap
    const maxHeap = new Heap();
    let cur = 0;
    for (let i = 0; i < k; i++) {
        while (cur < n && arr[cur][0] <= w) {
            // Insert the profits from all qualified items into the heap
            maxHeap.insert(arr[cur++][1]);
        }
        if (maxHeap.size()) {
            // Heap is not empty
            // Take out the top of the pile, which is the maximum net profit and update your own capital w
            w += maxHeap.pop();
        } else {
            // If the heap is empty, exit the loop directly
            // Because no items that meet the conditions have entered the heap
            break;
        }
    }
    return w;
};

😄 Recently, an open source warehouse has been created to summarize the daily question of LeetCode. At present, there are C + + and JavaScript language versions. You are welcome to provide other language versions!

🖥️ Warehouse address: "Daily question series"

Tags: Javascript Algorithm leetcode

Posted on Wed, 24 Nov 2021 03:57:40 -0500 by giannisptr