|
1 |
| -# [17.16. The Masseuse](https://leetcode-cn.com/problems/the-masseuse-lcci) |
2 |
| - |
3 |
| -## Description |
4 |
| -<p>A popular masseuse receives a sequence of back-to-back appointment requests and is debating which ones to accept. She needs a break between appointments and therefore she cannot accept any adjacent requests. Given a sequence of back-to-back appoint­ ment requests, find the optimal (highest total booked minutes) set the masseuse can honor. Return the number of minutes.</p> |
5 |
| -
|
6 |
| -<p><b>Note: </b>This problem is slightly different from the original one in the book.</p> |
7 |
| -
|
8 |
| -<p> </p> |
9 |
| -
|
10 |
| -<p><strong>Example 1: </strong></p> |
11 |
| -
|
12 |
| -<pre> |
13 |
| -<strong>Input: </strong> [1,2,3,1] |
14 |
| -<strong>Output: </strong> 4 |
15 |
| -<strong>Explanation: </strong> Accept request 1 and 3, total minutes = 1 + 3 = 4 |
16 |
| -</pre> |
17 |
| -
|
18 |
| -<p><strong>Example 2: </strong></p> |
19 |
| -
|
20 |
| -<pre> |
21 |
| -<strong>Input: </strong> [2,7,9,3,1] |
22 |
| -<strong>Output: </strong> 12 |
23 |
| -<strong>Explanation: </strong> Accept request 1, 3 and 5, total minutes = 2 + 9 + 1 = 12 |
24 |
| -</pre> |
25 |
| -
|
26 |
| -<p><strong>Example 3: </strong></p> |
27 |
| -
|
28 |
| -<pre> |
29 |
| -<strong>Input: </strong> [2,1,4,5,3,1,1,3] |
30 |
| -<strong>Output: </strong> 12 |
31 |
| -<strong>Explanation: </strong> Accept request 1, 3, 5 and 8, total minutes = 2 + 4 + 3 + 3 = 12 |
32 |
| -</pre> |
33 |
| - |
34 |
| - |
35 |
| - |
36 |
| -## Solutions |
37 |
| - |
38 |
| - |
39 |
| -### Python3 |
40 |
| - |
41 |
| -```python |
42 |
| - |
43 |
| -``` |
44 |
| - |
45 |
| -### Java |
46 |
| - |
47 |
| -```java |
48 |
| - |
49 |
| -``` |
50 |
| - |
51 |
| -### ... |
52 |
| -``` |
53 |
| - |
54 |
| -``` |
| 1 | +# [17.16. The Masseuse](https://leetcode-cn.com/problems/the-masseuse-lcci) |
| 2 | + |
| 3 | +## Description |
| 4 | +<p>A popular masseuse receives a sequence of back-to-back appointment requests and is debating which ones to accept. She needs a break between appointments and therefore she cannot accept any adjacent requests. Given a sequence of back-to-back appoint­ ment requests, find the optimal (highest total booked minutes) set the masseuse can honor. Return the number of minutes.</p> |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +<p><b>Note: </b>This problem is slightly different from the original one in the book.</p> |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +<p> </p> |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +<p><strong>Example 1: </strong></p> |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +<pre> |
| 21 | + |
| 22 | +<strong>Input: </strong> [1,2,3,1] |
| 23 | + |
| 24 | +<strong>Output: </strong> 4 |
| 25 | + |
| 26 | +<strong>Explanation: </strong> Accept request 1 and 3, total minutes = 1 + 3 = 4 |
| 27 | + |
| 28 | +</pre> |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +<p><strong>Example 2: </strong></p> |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +<pre> |
| 37 | + |
| 38 | +<strong>Input: </strong> [2,7,9,3,1] |
| 39 | + |
| 40 | +<strong>Output: </strong> 12 |
| 41 | + |
| 42 | +<strong>Explanation: </strong> Accept request 1, 3 and 5, total minutes = 2 + 9 + 1 = 12 |
| 43 | + |
| 44 | +</pre> |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +<p><strong>Example 3: </strong></p> |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +<pre> |
| 53 | + |
| 54 | +<strong>Input: </strong> [2,1,4,5,3,1,1,3] |
| 55 | + |
| 56 | +<strong>Output: </strong> 12 |
| 57 | + |
| 58 | +<strong>Explanation: </strong> Accept request 1, 3, 5 and 8, total minutes = 2 + 4 + 3 + 3 = 12 |
| 59 | + |
| 60 | +</pre> |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +## Solutions |
| 66 | + |
| 67 | + |
| 68 | +### Python3 |
| 69 | + |
| 70 | +```python |
| 71 | +class Solution: |
| 72 | + def massage(self, nums: List[int]) -> int: |
| 73 | + if not nums: |
| 74 | + return 0 |
| 75 | + n = len(nums) |
| 76 | + if n < 2: |
| 77 | + return nums[0] |
| 78 | + dp = [0 for _ in range(n)] |
| 79 | + dp[0], dp[1] = nums[0], max(nums[0], nums[1]) |
| 80 | + for i in range(2, n): |
| 81 | + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]) |
| 82 | + return dp[n - 1] |
| 83 | +``` |
| 84 | + |
| 85 | +### Java |
| 86 | + |
| 87 | +```java |
| 88 | +class Solution { |
| 89 | + public int massage(int[] nums) { |
| 90 | + if (nums == null) { |
| 91 | + return 0; |
| 92 | + } |
| 93 | + int n = nums.length; |
| 94 | + if (n < 2) { |
| 95 | + return n == 0 ? 0 : nums[0]; |
| 96 | + } |
| 97 | + int[] dp = new int[n]; |
| 98 | + dp[0] = nums[0]; |
| 99 | + dp[1] = Math.max(nums[0], nums[1]); |
| 100 | + for (int i = 2; i < n; ++i) { |
| 101 | + dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); |
| 102 | + } |
| 103 | + return dp[n - 1]; |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### ... |
| 109 | +``` |
| 110 | +
|
| 111 | +``` |
0 commit comments