Lintcode1448 · Card Game go

/**
1448 · Card Game
Algorithms
Medium
Accepted Rate
34%

DescriptionSolutionNotesDiscussLeaderboard
Description
A card game that gives you the number of cards n,and two non-negative integers: totalProfit, totalCost. Then it will give you the profit value of every card a[i] and the cost value of every card b[i].It is possible to select any number of cards from these cards, form a scheme. Now we want to know how many schemes are satisfied that all selected cards’ profit values are greater than totalProfit and the costs are less than totalCost.

Since this number may be large, you only need to return the solution number mod 1e9 + 7.
0 \leq n \leq 1000≤n≤100
0 \leq totalProfit\leq 1000≤totalProfit≤100
0 \leq totalCost \leq 1000≤totalCost≤100
0 \leq a[i] \leq 1000≤a[i]≤100
0 \leq b[i] \leq 1000≤b[i]≤100
Example
Example 1:

Input:n = 2,totalProfit = 3,totalCost = 5,a = [2,3],b = [2,2]
Output:1
Explanation:
At this time, there is only one legal scheme, which is to select both cards. At this time, a[1]+a[2] = 5 > totalProfit and b[1] + b[2] < totalCost.
Example 2:

Input: n = 3,totalProfit = 5,totalCost = 10,a = [6,7,8],b = [2,3,5]
Output: 6
Explanation:
Suppose a legal scheme (i,j) indicates that the i-th card and the j-th card are selected.
The legal solutions at this time are:
(1),(2),(3),(1,2),(1,3),(2,3)
Tags
Company
Google

https://blog.youkuaiyun.com/qq_46105170/article/details/110121972

https://www.lintcode.com/problem/1448/
*/

/**
 * @param n: The number of cards
 * @param totalProfit: The totalProfit
 * @param totalCost: The totalCost
 * @param a: The profit of cards
 * @param b: The cost of cards
 * @return: Return the number of legal plan
 */
func numOfPlan (n int, totalProfit int, totalCost int, a []int, b []int) int {
	// Write your code here
	var mod int = 1e9 + 7
	var dp [][][]int = make([][][]int, n)
	for i := 0; i < n; i++ {
		dp[i] = make([][]int, totalProfit + 2)
		for j := 0; j < totalProfit + 2; j++ {
			dp[i][j] = make([]int, totalCost)
		}
	}
	dp[0][0][0] = 1
	for i := 0; i <= Min(a[0], totalProfit + 1); i++ {
		dp[0][i][b[0]] += 1
	}
	for i := 1; i < n; i++ {
		for j := 0; j <= totalProfit + 1; j++ {
			for k := 0; k < totalCost; k++ {
				dp[i][j][k] = dp[i - 1][j][k]
				if k >= b[i] {
					dp[i][j][k] += dp[i - 1][Max(0, j - a[i])][k - b[i]]
				}
				dp[i][j][k] %= mod
			}
		}
	}
	var res int = 0
	for i := 0; i < totalCost; i++ {
		res += dp[n - 1][totalProfit + 1][i]
		res %= mod
	}
	return res
}

func Min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func Max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值