【Problem Description】
一个有名的按摩师会收到源源不断的预约请求,每个预约都可以选择接或不接。在每次预约服务之间要有休息时间,因此她不能接受相邻的预约。给定一个预约请求序列,替按摩师找到最优的预约集合(总预约时间最长),返回总的分钟数。
注意:本题相对原题稍作改动
示例 1:
输入: [1,2,3,1]
输出: 4
解释: 选择 1 号预约和 3 号预约,总时长 = 1 + 3 = 4。
示例 2:
输入: [2,7,9,3,1]
输出: 12
解释: 选择 1 号预约、 3 号预约和 5 号预约,总时长 = 2 + 9 + 1 = 12。
示例 3:
输入: [2,1,4,5,3,1,1,3]
输出: 12
解释: 选择 1 号预约、 3 号预约、 5 号预约和 8 号预约,总时长 = 2 + 4 + 3 + 3 = 12。
[Thinking] answer
1. Dynamic Programming to-back two-dimensional state variables to think clearly every step
Time complexity: O (N) space complexity: O (N)
- Design status
- dp [i] [0] represents: the interval [0, i] in the reservation request accepted, and the subscript i is the maximum length of the day are not accepted reservation;
- dp [i] [1] represents: the interval [0, i] in the accepted reservation request, and the maximum duration for that day subscript i is accepts reservation.
- State transition equation
- Today not accept reservation: yesterday or not to accept the reservation or booking last received, whichever is the maximum value, namely: dp [i] [0] = max (dp [i - 1] [0], dp [i --1] [1]);
- Today, by appointment: just do not accept an appointment from yesterday transferred from, coupled with today's often, namely: dp [i] [1] = dp [i - 1] [0] + nums [i]?.
3. Initialize
dp [0] [0] = 0 and dp [0] [1] = nums [0];
public int massage(int[] nums) {
int len = nums.length;
if (len == 0) {
return 0;
}
if (len == 1) {
return nums[0];
}
// dp[i][0]:区间 [0, i] 里接受预约请求,并且下标为 i 的这一天不接受预约的最大时长
// dp[i][1]:区间 [0, i] 里接受预约请求,并且下标为 i 的这一天接受预约的最大时长
int[][] dp = new int[len][2];
dp[0][0] = 0;
dp[0][1] = nums[0];
for (int i = 1; i < len; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = dp[i - 1][0] + nums[i];
}
return Math.max(dp[len - 1][0], dp[len - 1][1]);
}
Optimization: an array of multi-state setting line, in order to avoid extreme example will be discussed with
public class Solution {
public int massage(int[] nums) {
int len = nums.length;
// dp 数组多设置一行,相应地定义就要改变,遍历的一些细节也要相应改变
// dp[i][0]:区间 [0, i) 里接受预约请求,并且下标为 i 的这一天不接受预约的最大时长
// dp[i][1]:区间 [0, i) 里接受预约请求,并且下标为 i 的这一天接受预约的最大时长
int[][] dp = new int[len + 1][2];
// 注意:外层循环从 1 到 =len,相对 dp 数组而言,引用到 nums 数组的时候就要 -1
for (int i = 1; i <= len; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = dp[i - 1][0] + nums[i - 1];
}
return Math.max(dp[len][0], dp[len][1]);
}
}
Optimization [Scroll Array] three variables time complexity: complexity of O (N) space: O (1)
public class Solution {
public int massage(int[] nums) {
int len = nums.length;
if (len == 0) {
return 0;
}
if (len == 1) {
return nums[0];
}
// dp[i & 1][0]:区间 [0, i] 里接受预约请求,并且下标为 i 的这一天不接受预约的最大时长
// dp[i & 1][1]:区间 [0, i] 里接受预约请求,并且下标为 i 的这一天接受预约的最大时长
int[][] dp = new int[2][2];
dp[0][0] = 0;
dp[0][1] = nums[0];
for (int i = 1; i < len; i++) {
dp[i & 1][0] = Math.max(dp[(i - 1) & 1][0], dp[(i - 1) & 1][1]);
dp[i & 1][1] = dp[(i - 1) & 1][0] + nums[i];
}
return Math.max(dp[(len - 1) & 1][0], dp[(len - 1) & 1][1]);
}
}
###### 2. Dynamic Programming a one-dimensional variable-to-back state to think clearly every step of the
time complexity: O (N) space complexity: O (N)
public int massage(int[] nums) {
int len = nums.length;
if (len == 0) {
return 0;
}
if (len == 1) {
return nums[0];
}
// dp[i]:区间 [0, i] 里接受预约请求的最大时长
int[] dp = new int[len];
//初始化状态
dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);
for (int i = 2; i < len; i++) {
// 今天在选与不选中,选择一个最优的
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
}
return dp[len - 1];
}
Optimization [Scroll Array] three variables time complexity: complexity of O (N) space: O (1)
class Solution {
public int massage(int[] nums) {
int len = nums.length;
if (len == 0) {
return 0;
}
if (len == 1) {
return nums[0];
}
// dp[i % 3]:区间 [0,i] 里接受预约请求的最大时长
int[] dp = new int[3];
dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);
for (int i = 2; i < len; i++) {
// 今天在选与不选中,选择一个最优的
dp[i % 3] = Math.max(dp[(i - 1) % 3], dp[(i - 2) % 3] + nums[i]);
}
return dp[(len - 1) % 3];
}
}
【to sum up】
1. Dynamic Planning Process
- Step 1: Design status
- Step 2: state transition equation
- Step 3: Consider initialization
- Step 4: Consider Output
- Step 5: Consider whether compressed state
2. Bottom-up dynamic programming state transition
[General Programming] top-down
[Memory Recursive "at any time may face new problems
Reference link: https: //leetcode-cn.com/problems/the-masseuse-lcci/solution/dong-tai-gui-hua-by-liweiwei1419-8/