LintCode 1147: Work Plan (坐标型DP和前缀和DP)

1147. Work Plan

Xiaomei is the manager in charge of the team and needs to make the work plans for the team to help the team generate the most value. Every week, the team will have alternative tasks, one is a simple task, and the other is a complex task. In week i, the value of the team’s completion of simple tasks is low_ilow​i​​, and the value of completion of complex tasks is high_ihigh​i​​. Due to the technical difficulty of the complex task itself, if the team chooses to perform the complex task in week i, they need to concentrate on preparation without doing any task in week i-1. If the team choose to perform a simple task in week i, there is no need to make any preparations in advance. Now that Xiaomei's team has received a list of expected tasks for the next n weeks, please help Xiaomei determine the weekly work schedule that help the team generate the most value.

Example

Example 1:

Input:low=[4,2,3,7],hard=[3,5,6,9]
output:17
Explanation:
Pick simple task in the first week, value = 4
Prepare for the second week and pick complex task in the third week, value = 4 + 6 = 10
Pick a simple task in the fourth week, value = 10 + 7 = 17

Example 2:

Input:low=[1,3,5,9],high=[3,5,7,9]
Output:19

Notice

1 \leq |low|,|high| \leq 100001≤∣low∣,∣high∣≤10000
1 \leq low[i],high[i] \leq 100001≤low[i],high[i]≤10000

Input test data (one parameter per line)How to understand a testcase?

 

解法1:前缀和DP
dp[i] the max value upto the ith week
转移方程为:dp[i] = max(dp[i - 1] + low[i], dp[i - 2] + high[i])
当第i周从事low工作时,第i-1周可能是high或low,dp[i-1]包括了这2种可能。为什么不用考虑dp[i-2]呢?因为第i-2周从事high或low的情况都已经考虑在dp[i-1]周。
当第i周从事high工作时,第i-1周只可能休息,所以前i-1周的工作量和前i-2周的工作量一样,只需看dp[i-1]即可。

class Solution {
public:
    /**
     * @param low: the simple task
     * @param high: the complex task
     * @return: the most value
     */
    int workPlan(vector<int> &low, vector<int> &high) {
        int n = low.size();

        //dp[i] the max value upto the ith week
        vector<int> dp(n, 0);
        dp[0] = low[0]; 
        dp[1] = max(low[0] + low[1], high[1]);
        
        for (int i = 2; i < n; ++i) {
            dp[i] = max(dp[i - 1] + low[i], dp[i - 2] + high[i]);
        }
        
        return dp[n - 1];
    }
};

解法2:坐标型DP
dp[i][j]: the max value upto the ith week, and does the work j - 1:high, 0:low

class Solution {
public:
    /**
     * @param low: the simple task
     * @param high: the complex task
     * @return: the most value
     */
    int workPlan(vector<int> &low, vector<int> &high) {
        int n = low.size();

        //dp[i][j] the max value upto the ith week, and does the j work - 1:high, 0:low
        vector<vector<int>> dp(n, vector<int>(2, 0));
        dp[0][0] = low[0]; 
        dp[1][0] = low[0] + low[1];
        dp[1][1] = high[1];

        for (int i = 2; i < n; ++i) {
            dp[i][0] = max(dp[i - 1][0] + low[i], dp[i - 1][1] + low[i]);
            dp[i][1] = max(dp[i - 2][0] + high[i], dp[i - 2][1] + high[i]);
        }
        
        return max(dp[n - 1][0], dp[n - 1][1]);
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值