Skip to content

Commit 1bd7993

Browse files
committed
feat: add solutions to lcci question: 17.16.The Masseuse
添加《程序员面试金典》题解:面试题 17.16. 按摩师
1 parent 43cf1cf commit 1bd7993

File tree

4 files changed

+172
-56
lines changed

4 files changed

+172
-56
lines changed

lcci/17.16.The Masseuse/README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,49 @@
3232

3333
## 解法
3434
<!-- 这里可写通用的实现逻辑 -->
35+
动态规划求解。
3536

3637

3738
### Python3
3839
<!-- 这里可写当前语言的特殊实现逻辑 -->
3940

4041
```python
41-
42+
class Solution:
43+
def massage(self, nums: List[int]) -> int:
44+
if not nums:
45+
return 0
46+
n = len(nums)
47+
if n < 2:
48+
return nums[0]
49+
dp = [0 for _ in range(n)]
50+
dp[0], dp[1] = nums[0], max(nums[0], nums[1])
51+
for i in range(2, n):
52+
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
53+
return dp[n - 1]
4254
```
4355

4456
### Java
4557
<!-- 这里可写当前语言的特殊实现逻辑 -->
4658

4759
```java
48-
60+
class Solution {
61+
public int massage(int[] nums) {
62+
if (nums == null) {
63+
return 0;
64+
}
65+
int n = nums.length;
66+
if (n < 2) {
67+
return n == 0 ? 0 : nums[0];
68+
}
69+
int[] dp = new int[n];
70+
dp[0] = nums[0];
71+
dp[1] = Math.max(nums[0], nums[1]);
72+
for (int i = 2; i < n; ++i) {
73+
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
74+
}
75+
return dp[n - 1];
76+
}
77+
}
4978
```
5079

5180
### ...

lcci/17.16.The Masseuse/README_EN.md

Lines changed: 111 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,111 @@
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&shy; 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:&nbsp;</b>This problem is slightly different from the original one in the book.</p>
7-
8-
<p>&nbsp;</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&shy; 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:&nbsp;</b>This problem is slightly different from the original one in the book.</p>
9+
10+
11+
12+
<p>&nbsp;</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+
```

lcci/17.16.The Masseuse/Solution.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int massage(int[] nums) {
3+
if (nums == null) {
4+
return 0;
5+
}
6+
int n = nums.length;
7+
if (n < 2) {
8+
return n == 0 ? 0 : nums[0];
9+
}
10+
int[] dp = new int[n];
11+
dp[0] = nums[0];
12+
dp[1] = Math.max(nums[0], nums[1]);
13+
for (int i = 2; i < n; ++i) {
14+
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
15+
}
16+
return dp[n - 1];
17+
}
18+
}

lcci/17.16.The Masseuse/Solution.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def massage(self, nums: List[int]) -> int:
3+
if not nums:
4+
return 0
5+
n = len(nums)
6+
if n < 2:
7+
return nums[0]
8+
dp = [0 for _ in range(n)]
9+
dp[0], dp[1] = nums[0], max(nums[0], nums[1])
10+
for i in range(2, n):
11+
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
12+
return dp[n - 1]

0 commit comments

Comments
 (0)