[Swift]LeetCode502. IPO(首次公开募股) | Initial Public Offerings

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-pjgoehdy-me.html 
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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 kdistinct projects.git

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.github

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.api

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 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. 

Note:微信

  1. You may assume all numbers in the input are non-negative integers.
  2. The length of Profits array and Capital array will not exceed 50,000.
  3. The answer is guaranteed to fit in a 32-bit signed integer.

假设 LeetCode 即将开始其 IPO。为了以更高的价格将股票卖给风险投资公司,LeetCode但愿在 IPO 以前开展一些项目以增长其资本。 因为资源有限,它只能在 IPO 以前完成最多 k 个不一样的项目。帮助 LeetCode 设计完成最多 k 个不一样项目后获得最大总资本的方式。spa

给定若干个项目。对于每一个项目 i,它都有一个纯利润 Pi,而且须要最小的资本 Ci 来启动相应的项目。最初,你有 W 资本。当你完成一个项目时,你将得到纯利润,且利润将被添加到你的总资本中。设计

总而言之,从给定项目中选择最多 k 个不一样项目的列表,以最大化最终资本,并输出最终可得到的最多资本。code

示例 1:htm

输入: k=2, W=0, Profits=[1,2,3], Capital=[0,1,1].

输出: 4

解释:
因为你的初始资本为 0,你尽能够从 0 号项目开始。
在完成后,你将得到 1 的利润,你的总资本将变为 1。
此时你能够选择开始 1 号或 2 号项目。
因为你最多能够选择两个项目,因此你须要完成 2 号项目以得到最大的资本。
所以,输出最后最大化的资本,为 0 + 1 + 3 = 4。 

注意:

  1. 假设全部输入数字都是非负整数。
  2. 表示利润和资本的数组的长度不超过 50000。
  3. 答案保证在 32 位有符号整数范围内。

Runtime: 268 ms
Memory Usage: 20.3 MB
 1 class Solution {
 2     func findMaximizedCapital(_ k: Int, _ W: Int, _ Profits: [Int], _ Capital: [Int]) -> Int {
 3         var W = W
 4         var k = k
 5         if k == Profits.count
 6         {
 7             for i in 0..<Profits.count
 8             {
 9                 W += Profits[i]
10             }
11             return W
12         }
13         var usedProjects:Set<Int> = Set<Int>()
14         while(k > 0)
15         {
16             k -= 1
17             var pIndex:Int = -1
18             var profit:Int = 0
19             for i in 0..<Profits.count
20             {
21                 if !usedProjects.contains(i)
22                 {
23                     if W - Capital[i] >= 0 && profit < Profits[i]
24                     {
25                         profit = Profits[i]
26                         pIndex = i
27                     }
28                 }
29             }
30             if pIndex > -1
31             {
32                 W += Profits[pIndex]
33                 usedProjects.insert(pIndex)
34             }
35         }
36         return W
37     }
38 }
相关文章
相关标签/搜索