2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > [Swift]LeetCode502. IPO(首次公开募股) | Initial Public Offerings

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

时间:2020-12-29 10:45:07

相关推荐

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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

➤微信公众号:山青咏芝(shanqingyongzhi)

➤博客园地址:山青咏芝(/strengthen/)

➤GitHub地址:/strengthen/LeetCode

➤原文地址:/strengthen/p/10392224.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 mostkdistinct projects before the IPO. Help LeetCode design the best way to maximize its total capital after finishing at mostkdistinct projects.

You are given several projects. For each projecti, it has a pure profitPiand a minimum capital ofCiis needed to start the corresponding project. Initially, you haveWcapital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital.

To sum up, pick a list of at mostkdistinct 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: 4Explanation: 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:

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

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

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

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

示例 1:

输入: k=2, W=0, Profits=[1,2,3], Capital=[0,1,1].输出: 4解释:由于你的初始资本为 0,你尽可以从 0 号项目开始。在完成后,你将获得 1 的利润,你的总资本将变为 1。此时你可以选择开始 1 号或 2 号项目。由于你最多可以选择两个项目,所以你需要完成 2 号项目以获得最大的资本。因此,输出最后最大化的资本,为 0 + 1 + 3 = 4。

注意:

假设所有输入数字都是非负整数。表示利润和资本的数组的长度不超过 50000。答案保证在 32 位有符号整数范围内。Runtime:268 ms Memory Usage:20.3 MB

1 class Solution { 2func 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 W12 }13 var usedProjects:Set<Int> = Set<Int>()14 while(k > 0)15 {16 k -= 117 var pIndex:Int = -118 var profit:Int = 019 for i in 0..<Profits.count20 {21 if !usedProjects.contains(i)22 {23 if W - Capital[i] >= 0 && profit < Profits[i]24 {25profit = Profits[i]26pIndex = i27 }28 }29 }30 if pIndex > -131 {32 W += Profits[pIndex]33 usedProjects.insert(pIndex)34 }35 }36 return W37}38 }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。