Skip to content

Commit 90fb0e0

Browse files
committed
feat: add solutions to lc problem: No.1014
No.1014.Best Sightseeing Pair
1 parent c6fecc5 commit 90fb0e0

File tree

7 files changed

+103
-63
lines changed

7 files changed

+103
-63
lines changed

solution/1000-1099/1014.Best Sightseeing Pair/README.md

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45+
**方法一:枚举 + 维护前缀最大值**
46+
47+
我们可以在 $[1,..n - 1]$ 的范围内枚举 $j$,那么我们要在 $[0,..j - 1]$ 的范围内找到一个 $i$,使得 $values[i] + values[j] + i - j$ 的值最大。我们可以维护一个前缀最大值,即 $values[i] + i$ 的最大值,那么我们只需要在枚举 $j$ 的过程中,不断地更新答案即可。
48+
49+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。
50+
4551
<!-- tabs:start -->
4652

4753
### **Python3**
@@ -51,11 +57,11 @@
5157
```python
5258
class Solution:
5359
def maxScoreSightseeingPair(self, values: List[int]) -> int:
54-
res, mx = 0, values[0]
55-
for i in range(1, len(values)):
56-
res = max(res, values[i] - i + mx)
57-
mx = max(mx, values[i] + i)
58-
return res
60+
ans, mx = 0, values[0]
61+
for j in range(1, len(values)):
62+
ans = max(ans, values[j] - j + mx)
63+
mx = max(mx, values[j] + j)
64+
return ans
5965
```
6066

6167
### **Java**
@@ -65,12 +71,12 @@ class Solution:
6571
```java
6672
class Solution {
6773
public int maxScoreSightseeingPair(int[] values) {
68-
int res = 0, mx = values[0];
69-
for (int i = 1; i < values.length; ++i) {
70-
res = Math.max(res, values[i] - i + mx);
71-
mx = Math.max(mx, values[i] + i);
74+
int ans = 0, mx = values[0];
75+
for (int j = 1; j < values.length; ++j) {
76+
ans = Math.max(ans, values[j] - j + mx);
77+
mx = Math.max(mx, values[j] + j);
7278
}
73-
return res;
79+
return ans;
7480
}
7581
}
7682
```
@@ -81,26 +87,25 @@ class Solution {
8187
class Solution {
8288
public:
8389
int maxScoreSightseeingPair(vector<int>& values) {
84-
int res = 0, mx = values[0];
85-
for (int i = 1; i < values.size(); ++i) {
86-
res = max(res, values[i] - i + mx);
87-
mx = max(mx, values[i] + i);
90+
int ans = 0, mx = values[0];
91+
for (int j = 1; j < values.size(); ++j) {
92+
ans = max(ans, values[j] - j + mx);
93+
mx = max(mx, values[j] + j);
8894
}
89-
return res;
95+
return ans;
9096
}
9197
};
9298
```
9399
94100
### **Go**
95101
96102
```go
97-
func maxScoreSightseeingPair(values []int) int {
98-
res, mx := 0, values[0]
99-
for i := 1; i < len(values); i++ {
100-
res = max(res, values[i]-i+mx)
101-
mx = max(mx, values[i]+i)
103+
func maxScoreSightseeingPair(values []int) (ans int) {
104+
for j, mx := 1, values[0]; j < len(values); j++ {
105+
ans = max(ans, values[j]-j+mx)
106+
mx = max(mx, values[j]+j)
102107
}
103-
return res
108+
return
104109
}
105110
106111
func max(a, b int) int {
@@ -111,6 +116,20 @@ func max(a, b int) int {
111116
}
112117
```
113118

119+
### **TypeScript**
120+
121+
```ts
122+
function maxScoreSightseeingPair(values: number[]): number {
123+
let ans = 0;
124+
let mx = values[0];
125+
for (let j = 1; j < values.length; ++j) {
126+
ans = Math.max(ans, values[j] - j + mx);
127+
mx = Math.max(mx, values[j] + j);
128+
}
129+
return ans;
130+
}
131+
```
132+
114133
### **...**
115134

116135
```

solution/1000-1099/1014.Best Sightseeing Pair/README_EN.md

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,24 @@
4343
```python
4444
class Solution:
4545
def maxScoreSightseeingPair(self, values: List[int]) -> int:
46-
res, mx = 0, values[0]
47-
for i in range(1, len(values)):
48-
res = max(res, values[i] - i + mx)
49-
mx = max(mx, values[i] + i)
50-
return res
46+
ans, mx = 0, values[0]
47+
for j in range(1, len(values)):
48+
ans = max(ans, values[j] - j + mx)
49+
mx = max(mx, values[j] + j)
50+
return ans
5151
```
5252

5353
### **Java**
5454

5555
```java
5656
class Solution {
5757
public int maxScoreSightseeingPair(int[] values) {
58-
int res = 0, mx = values[0];
59-
for (int i = 1; i < values.length; ++i) {
60-
res = Math.max(res, values[i] - i + mx);
61-
mx = Math.max(mx, values[i] + i);
58+
int ans = 0, mx = values[0];
59+
for (int j = 1; j < values.length; ++j) {
60+
ans = Math.max(ans, values[j] - j + mx);
61+
mx = Math.max(mx, values[j] + j);
6262
}
63-
return res;
63+
return ans;
6464
}
6565
}
6666
```
@@ -71,26 +71,25 @@ class Solution {
7171
class Solution {
7272
public:
7373
int maxScoreSightseeingPair(vector<int>& values) {
74-
int res = 0, mx = values[0];
75-
for (int i = 1; i < values.size(); ++i) {
76-
res = max(res, values[i] - i + mx);
77-
mx = max(mx, values[i] + i);
74+
int ans = 0, mx = values[0];
75+
for (int j = 1; j < values.size(); ++j) {
76+
ans = max(ans, values[j] - j + mx);
77+
mx = max(mx, values[j] + j);
7878
}
79-
return res;
79+
return ans;
8080
}
8181
};
8282
```
8383
8484
### **Go**
8585
8686
```go
87-
func maxScoreSightseeingPair(values []int) int {
88-
res, mx := 0, values[0]
89-
for i := 1; i < len(values); i++ {
90-
res = max(res, values[i]-i+mx)
91-
mx = max(mx, values[i]+i)
87+
func maxScoreSightseeingPair(values []int) (ans int) {
88+
for j, mx := 1, values[0]; j < len(values); j++ {
89+
ans = max(ans, values[j]-j+mx)
90+
mx = max(mx, values[j]+j)
9291
}
93-
return res
92+
return
9493
}
9594
9695
func max(a, b int) int {
@@ -101,6 +100,20 @@ func max(a, b int) int {
101100
}
102101
```
103102

103+
### **TypeScript**
104+
105+
```ts
106+
function maxScoreSightseeingPair(values: number[]): number {
107+
let ans = 0;
108+
let mx = values[0];
109+
for (let j = 1; j < values.length; ++j) {
110+
ans = Math.max(ans, values[j] - j + mx);
111+
mx = Math.max(mx, values[j] + j);
112+
}
113+
return ans;
114+
}
115+
```
116+
104117
### **...**
105118

106119
```
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public:
33
int maxScoreSightseeingPair(vector<int>& values) {
4-
int res = 0, mx = values[0];
5-
for (int i = 1; i < values.size(); ++i) {
6-
res = max(res, values[i] - i + mx);
7-
mx = max(mx, values[i] + i);
4+
int ans = 0, mx = values[0];
5+
for (int j = 1; j < values.size(); ++j) {
6+
ans = max(ans, values[j] - j + mx);
7+
mx = max(mx, values[j] + j);
88
}
9-
return res;
9+
return ans;
1010
}
1111
};

solution/1000-1099/1014.Best Sightseeing Pair/Solution.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
func maxScoreSightseeingPair(values []int) int {
2-
res, mx := 0, values[0]
3-
for i := 1; i < len(values); i++ {
4-
res = max(res, values[i]-i+mx)
5-
mx = max(mx, values[i]+i)
1+
func maxScoreSightseeingPair(values []int) (ans int) {
2+
for j, mx := 1, values[0]; j < len(values); j++ {
3+
ans = max(ans, values[j]-j+mx)
4+
mx = max(mx, values[j]+j)
65
}
7-
return res
6+
return
87
}
98

109
func max(a, b int) int {
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution {
22
public int maxScoreSightseeingPair(int[] values) {
3-
int res = 0, mx = values[0];
4-
for (int i = 1; i < values.length; ++i) {
5-
res = Math.max(res, values[i] - i + mx);
6-
mx = Math.max(mx, values[i] + i);
3+
int ans = 0, mx = values[0];
4+
for (int j = 1; j < values.length; ++j) {
5+
ans = Math.max(ans, values[j] - j + mx);
6+
mx = Math.max(mx, values[j] + j);
77
}
8-
return res;
8+
return ans;
99
}
1010
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def maxScoreSightseeingPair(self, values: List[int]) -> int:
3-
res, mx = 0, values[0]
4-
for i in range(1, len(values)):
5-
res = max(res, values[i] - i + mx)
6-
mx = max(mx, values[i] + i)
7-
return res
3+
ans, mx = 0, values[0]
4+
for j in range(1, len(values)):
5+
ans = max(ans, values[j] - j + mx)
6+
mx = max(mx, values[j] + j)
7+
return ans
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function maxScoreSightseeingPair(values: number[]): number {
2+
let ans = 0;
3+
let mx = values[0];
4+
for (let j = 1; j < values.length; ++j) {
5+
ans = Math.max(ans, values[j] - j + mx);
6+
mx = Math.max(mx, values[j] + j);
7+
}
8+
return ans;
9+
}

0 commit comments

Comments
 (0)