[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 }
相關文章
相關標籤/搜索