[Swift]LeetCode879. 盈利計劃 | Profitable Schemes

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-fbddqgtc-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

There are G people in a gang, and a list of various crimes they could commit.git

The i-th crime generates a profit[i] and requires group[i] gang members to participate.github

If a gang member participates in one crime, that member can't participate in another crime.微信

Let's call a profitable scheme any subset of these crimes that generates at least Pprofit, and the total number of gang members participating in that subset of crimes is at most G.ide

How many schemes can be chosen?  Since the answer may be very large, return it modulo 10^9 + 7ui

Example 1:spa

Input: G = 5, P = 3, group = [2,2], profit = [2,3] Output: 2 Explanation: To make a profit of at least 3, the gang could either commit crimes 0 and 1, or just crime 1. In total, there are 2 schemes. 

Example 2:code

Input: G = 10, P = 5, group = [2,3,5], profit = [6,7,8] Output: 7 Explanation: To make a profit of at least 5, the gang could commit any crimes, as long as they commit one. There are 7 possible schemes: (0), (1), (2), (0,1), (0,2), (1,2), and (0,1,2). 

Note:htm

  1. 1 <= G <= 100
  2. 0 <= P <= 100
  3. 1 <= group[i] <= 100
  4. 0 <= profit[i] <= 100
  5. 1 <= group.length = profit.length <= 100

幫派裏有 G 名成員,他們可能犯下各類各樣的罪行。blog

第 i 種犯罪會產生 profit[i] 的利潤,它要求 group[i] 名成員共同參與。

讓咱們把這些犯罪的任何子集稱爲盈利計劃,該計劃至少產生 P 的利潤。

有多少種方案能夠選擇?由於答案很大,因此返回它模 10^9 + 7 的值。 

示例 1:

輸入:G = 5, P = 3, group = [2,2], profit = [2,3]
輸出:2
解釋: 
至少產生 3 的利潤,該幫派能夠犯下罪 0 和罪 1 ,或僅犯下罪 1 。
總的來講,有兩種方案。

示例 2:

輸入:G = 10, P = 5, group = [2,3,5], profit = [6,7,8]
輸出:7
解釋:
至少產生 5 的利潤,只要他們犯其中一種罪就行,因此該幫派能夠犯下任何罪行 。
有 7 種可能的計劃:(0),(1),(2),(0,1),(0,2),(1,2),以及 (0,1,2) 。 

提示:

  1. 1 <= G <= 100
  2. 0 <= P <= 100
  3. 1 <= group[i] <= 100
  4. 0 <= profit[i] <= 100
  5. 1 <= group.length = profit.length <= 100

Runtime: 1032 ms
Memory Usage: 18.8 MB
 1 class Solution {
 2     func profitableSchemes(_ G: Int, _ P: Int, _ group: [Int], _ profit: [Int]) -> Int {
 3         var dp:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:G + 1),count:P + 1)
 4         dp[0][0] = 1
 5         var res:Int = 0
 6         var mod:Int = Int(1e9 + 7)
 7         for k in 0..<group.count
 8         {
 9             var g:Int = group[k]
10             var p:Int = profit[k]
11             for i in stride(from:P,through:0,by:-1)
12             {
13                 for j in stride(from:G - g,through:0,by:-1)
14                 {
15                     dp[min(i + p, P)][j + g] = (dp[min(i + p, P)][j + g] + dp[i][j]) % mod
16                 }
17             }
18         }
19         for x in dp[P]
20         {
21             res = (res + x) % mod
22         }
23         return res
24     }
25 }

Runtime: 1192 ms
Memory Usage: 19.4 MB
 1 class Solution {
 2     func profitableSchemes(_ G: Int, _ P: Int, _ group: [Int], _ profit: [Int]) -> Int {
 3         guard G >= 1 && G <= 100 && P >= 0 && P <= 100 && group.count >= 1 && group.count <= 100 && group.count == profit.count else {
 4             return 0
 5         }
 6         let mod = Int(1e9 + 7)
 7         var crimePlanCount = Array(repeating: Array(repeating: 0, count: G + 1), count: P + 1)
 8         crimePlanCount[0][0] = 1
 9         for (index, groupValue) in group.enumerated() {
10             let profitValue = profit[index]
11             for p in stride(from: P, through: 0, by: -1) {
12                 for g in stride(from: G, through: 0, by: -1) {
13                     if groupValue + g > G || crimePlanCount[p][g] == 0 {
14                         continue
15                     }
16                     let targetProfit = min(P, profitValue + p)
17                     let targetGroup = groupValue + g
18                     crimePlanCount[targetProfit][targetGroup] += crimePlanCount[p][g]
19                     crimePlanCount[targetProfit][targetGroup] %= mod
20                 }
21             }
22         }
23         var result = 0
24         for count in crimePlanCount[P] {
25             result += count
26         }
27         return result % mod
28     }
29 }
相關文章
相關標籤/搜索