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

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

概述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

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.

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.

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.

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,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 { 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 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                     {25                         profit = Profits[i]26                         pIndex = i27                     }28                 }29             }30             if pIndex > -131             {32                 W += Profits[pIndex]33                 usedProjects.insert(pIndex)34             }35         }36         return W37     }38 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode502. IPO | Initial Public Offerings(首次公开募股)全部内容,希望文章能够帮你解决[Swift]LeetCode502. IPO | Initial Public Offerings(首次公开募股)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1019921.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存