Skip to content

Commit 507b20a

Browse files
committed
feat: add solutions to lcci problem: No.17.16
No.17.16.The Masseuse
1 parent 781ba37 commit 507b20a

File tree

6 files changed

+150
-74
lines changed

6 files changed

+150
-74
lines changed

lcci/17.16.The Masseuse/README.md

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,24 @@
3636

3737
<!-- 这里可写通用的实现逻辑 -->
3838

39-
递推求解。
39+
**方法一:动态规划**
40+
41+
我们定义状态 $f[i]$ 表示考虑前 $i$ 个预约,且第 $i$ 个预约被接受的情况下,最长的预约时长;定义状态 $g[i]$ 表示考虑前 $i$ 个预约,且第 $i$ 个预约被拒绝的情况下,最长的预约时长。
42+
43+
考虑第 $i$ 个预约,如果第 $i$ 个预约被接受,那么第 $i-1$ 个预约一定不能被接受,即 $f[i]=g[i-1]+nums[i]$;如果第 $i$ 个预约被拒绝,那么第 $i-1$ 个预约可以被接受,也可以被拒绝,即 $g[i]=max(f[i-1],g[i-1])$。
44+
45+
所以,我们可以写出状态转移方程:
46+
47+
$$
48+
\begin{aligned}
49+
f[i] &= g[i-1]+nums[i] \\
50+
g[i] &= max(f[i-1],g[i-1])
51+
\end{aligned}
52+
$$
53+
54+
最终的答案即为 $max(f[n-1],g[n-1])$,其中 $n$ 为预约的数量。
55+
56+
我们可以将空间复杂度优化至 $O(1)$,即使用两个变量 $f$ 和 $g$ 来代替数组 $f$ 和 $g$。
4057

4158
<!-- tabs:start -->
4259

@@ -47,17 +64,10 @@
4764
```python
4865
class Solution:
4966
def massage(self, nums: List[int]) -> int:
50-
if not nums:
51-
return 0
52-
n = len(nums)
53-
if n < 2:
54-
return nums[0]
55-
a, b = nums[0], max(nums[0], nums[1])
56-
res = b
57-
for i in range(2, n):
58-
res = max(a + nums[i], b)
59-
a, b = b, res
60-
return res
67+
f = g = 0
68+
for x in nums:
69+
f, g = g + x, max(f, g)
70+
return max(f, g)
6171
```
6272

6373
### **Java**
@@ -67,22 +77,52 @@ class Solution:
6777
```java
6878
class Solution {
6979
public int massage(int[] nums) {
70-
if (nums == null) {
71-
return 0;
72-
}
73-
int n = nums.length;
74-
if (n < 2) {
75-
return n == 0 ? 0 : nums[0];
80+
int f = 0, g = 0;
81+
for (int x : nums) {
82+
int ff = g + x;
83+
int gg = Math.max(f, g);
84+
f = ff;
85+
g = gg;
7686
}
77-
int a = nums[0], b = Math.max(nums[0], nums[1]);
78-
int res = b;
79-
for (int i = 2; i < n; ++i) {
80-
res = Math.max(a + nums[i], b);
81-
a = b;
82-
b = res;
87+
return Math.max(f, g);
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int massage(vector<int>& nums) {
98+
int f = 0, g = 0;
99+
for (int& x : nums) {
100+
int ff = g + x;
101+
int gg = max(f, g);
102+
f = ff;
103+
g = gg;
83104
}
84-
return res;
105+
return max(f, g);
85106
}
107+
};
108+
```
109+
110+
### **Go**
111+
112+
```go
113+
func massage(nums []int) int {
114+
f, g := 0, 0
115+
for _, x := range nums {
116+
f, g = g+x, max(f, g)
117+
}
118+
return max(f, g)
119+
}
120+
121+
func max(a, b int) int {
122+
if a > b {
123+
return a
124+
}
125+
return b
86126
}
87127
```
88128

lcci/17.16.The Masseuse/README_EN.md

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,40 +55,63 @@
5555
```python
5656
class Solution:
5757
def massage(self, nums: List[int]) -> int:
58-
if not nums:
59-
return 0
60-
n = len(nums)
61-
if n < 2:
62-
return nums[0]
63-
a, b = nums[0], max(nums[0], nums[1])
64-
res = b
65-
for i in range(2, n):
66-
res = max(a + nums[i], b)
67-
a, b = b, res
68-
return res
58+
f = g = 0
59+
for x in nums:
60+
f, g = g + x, max(f, g)
61+
return max(f, g)
6962
```
7063

7164
### **Java**
7265

7366
```java
7467
class Solution {
7568
public int massage(int[] nums) {
76-
if (nums == null) {
77-
return 0;
69+
int f = 0, g = 0;
70+
for (int x : nums) {
71+
int ff = g + x;
72+
int gg = Math.max(f, g);
73+
f = ff;
74+
g = gg;
7875
}
79-
int n = nums.length;
80-
if (n < 2) {
81-
return n == 0 ? 0 : nums[0];
82-
}
83-
int a = nums[0], b = Math.max(nums[0], nums[1]);
84-
int res = b;
85-
for (int i = 2; i < n; ++i) {
86-
res = Math.max(a + nums[i], b);
87-
a = b;
88-
b = res;
76+
return Math.max(f, g);
77+
}
78+
}
79+
```
80+
81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
int massage(vector<int>& nums) {
87+
int f = 0, g = 0;
88+
for (int& x : nums) {
89+
int ff = g + x;
90+
int gg = max(f, g);
91+
f = ff;
92+
g = gg;
8993
}
90-
return res;
94+
return max(f, g);
9195
}
96+
};
97+
```
98+
99+
### **Go**
100+
101+
```go
102+
func massage(nums []int) int {
103+
f, g := 0, 0
104+
for _, x := range nums {
105+
f, g = g+x, max(f, g)
106+
}
107+
return max(f, g)
108+
}
109+
110+
func max(a, b int) int {
111+
if a > b {
112+
return a
113+
}
114+
return b
92115
}
93116
```
94117

lcci/17.16.The Masseuse/Solution.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int massage(vector<int>& nums) {
4+
int f = 0, g = 0;
5+
for (int& x : nums) {
6+
int ff = g + x;
7+
int gg = max(f, g);
8+
f = ff;
9+
g = gg;
10+
}
11+
return max(f, g);
12+
}
13+
};

lcci/17.16.The Masseuse/Solution.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func massage(nums []int) int {
2+
f, g := 0, 0
3+
for _, x := range nums {
4+
f, g = g+x, max(f, g)
5+
}
6+
return max(f, g)
7+
}
8+
9+
func max(a, b int) int {
10+
if a > b {
11+
return a
12+
}
13+
return b
14+
}

lcci/17.16.The Masseuse/Solution.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
class Solution {
22
public int massage(int[] nums) {
3-
if (nums == null) {
4-
return 0;
3+
int f = 0, g = 0;
4+
for (int x : nums) {
5+
int ff = g + x;
6+
int gg = Math.max(f, g);
7+
f = ff;
8+
g = gg;
59
}
6-
int n = nums.length;
7-
if (n < 2) {
8-
return n == 0 ? 0 : nums[0];
9-
}
10-
int a = nums[0], b = Math.max(nums[0], nums[1]);
11-
int res = b;
12-
for (int i = 2; i < n; ++i) {
13-
res = Math.max(a + nums[i], b);
14-
a = b;
15-
b = res;
16-
}
17-
return res;
10+
return Math.max(f, g);
1811
}
1912
}

lcci/17.16.The Masseuse/Solution.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
class Solution:
22
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-
a, b = nums[0], max(nums[0], nums[1])
9-
res = b
10-
for i in range(2, n):
11-
res = max(a + nums[i], b)
12-
a, b = b, res
13-
return res
3+
f = g = 0
4+
for x in nums:
5+
f, g = g + x, max(f, g)
6+
return max(f, g)

0 commit comments

Comments
 (0)