LintCode 1853: Efficient Job Processing Service (01背包经典题)

  1. Efficient Job Processing Service

Twitter is testing a new job processing service called Pigeon.

Pigeon processes any task in double the actual duration of the task and every task has a weight. Also, Pigeon can serve only for a limited duration(maximum runtime) in an hour.

Given the maximum runtime of the Pigon service, the list of tasks with their lengths and weights, determine the maximum total weight that the Pigeon service can achieve in an hour.

The input contains these parameters:

nn: the number of tasks
weightsweights: the weight for every task
taskstasks: the actual duration of every task
pp: maximum runtime for Pigeon in an hour
Example
Example 1

Input:
4
[2,4,4,5]
[2,2,3,4]
15
Output: 10
Explanation:You can run No.0 No.1 and No.2 task. It will cost 2 * (2 + 2 + 3) = 14 minutes and get 2 + 4 + 4 = 10 weight.
Example 2

Input:
3
[3,2,2]
[3,2,2]
9
Output: 4
Explanation:You can run No.1 and No.2 task. It will cost 2 * (2 + 2) = 8 minutes and get 2 + 2 = 4 weight.
Clarification
Every task can be processed only once.

Notice
1 \leq n \leq 10^31≤n≤10
​3
​​
1 \leq weights[i] \leq 10^61≤weights[i]≤10
​6
​​
1 \leq tasks[i] \leq 1001≤tasks[i]≤100
1 \leq p \leq 10^31≤p≤10
​3

​​解法1:01背包2维数组

class Solution {
public:
    /**
     * @param n: the number of tasks
     * @param weights: the weight for every task
     * @param tasks: the actual duration of every task
     * @param p: maximum runtime for Pigeon in an hour
     * @return: the maximum total weight that the Pigeon service can achieve in an hour
     */
    int maxWeight(int n, vector<int> &weights, vector<int> &tasks, int p) {
        vector<vector<int>> dp(n + 1, vector<int>(p + 1));
        
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= p; ++j) {
                if (j < 2 * tasks[i - 1]) {
                    dp[i][j] = dp[i - 1][j];
                } else {
                    dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 2 * tasks[i - 1]] + weights[i - 1]);
                }
            }
        }
        
        return dp[n][p];
    }
};

解法2:01背包1维数组

class Solution {
public:
    /**
     * @param n: the number of tasks
     * @param weights: the weight for every task
     * @param tasks: the actual duration of every task
     * @param p: maximum runtime for Pigeon in an hour
     * @return: the maximum total weight that the Pigeon service can achieve in an hour
     */
    int maxWeight(int n, vector<int> &weights, vector<int> &tasks, int p) {
        vector<int> dp(p + 1);
        
        for (int i = 0; i < n; ++i) {
            for (int j = p; j >= 2 * tasks[i]; --j) {
                dp[j] = max(dp[j], dp[j - 2 * tasks[i]] + weights[i]);
            }
        }
        
        return dp[p];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值