Skip to content

Commit 44b8480

Browse files
authored
feat: add solutions to lc problem: No.1014 (#3552)
No.1014.Best Sightseeing Pair
1 parent 977a6bf commit 44b8480

File tree

10 files changed

+112
-61
lines changed

10 files changed

+112
-61
lines changed

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

+36-20
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### 方法一:枚举 + 维护前缀最大值
60+
### 方法一:枚举
6161

62-
我们可以在 $[1,..n - 1]$ 的范围内枚举 $j$,那么我们要在 $[0,..j - 1]$ 的范围内找到一个 $i$,使得 $values[i] + values[j] + i - j$ 的值最大。我们可以维护一个前缀最大值,即 $values[i] + i$ 的最大值,那么我们只需要在枚举 $j$ 的过程中,不断地更新答案即可
62+
我们可以从左到右枚举 $j$,同时维护 $j$ 左侧元素中 $values[i] + i$ 的最大值 $mx$,这样对于每个 $j$,最大得分为 $mx + values[j] - j$。我们取所有位置的最大得分的最大值即为答案
6363

64-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度
64+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{values}$ 的长度。空间复杂度 $O(1)$。
6565

6666
<!-- tabs:start -->
6767

@@ -70,10 +70,10 @@ tags:
7070
```python
7171
class Solution:
7272
def maxScoreSightseeingPair(self, values: List[int]) -> int:
73-
ans, mx = 0, values[0]
74-
for j in range(1, len(values)):
75-
ans = max(ans, values[j] - j + mx)
76-
mx = max(mx, values[j] + j)
73+
ans = mx = 0
74+
for j, x in enumerate(values):
75+
ans = max(ans, mx + x - j)
76+
mx = max(mx, x + j)
7777
return ans
7878
```
7979

@@ -82,9 +82,9 @@ class Solution:
8282
```java
8383
class Solution {
8484
public int maxScoreSightseeingPair(int[] values) {
85-
int ans = 0, mx = values[0];
86-
for (int j = 1; j < values.length; ++j) {
87-
ans = Math.max(ans, values[j] - j + mx);
85+
int ans = 0, mx = 0;
86+
for (int j = 0; j < values.length; ++j) {
87+
ans = Math.max(ans, mx + values[j] - j);
8888
mx = Math.max(mx, values[j] + j);
8989
}
9090
return ans;
@@ -98,9 +98,9 @@ class Solution {
9898
class Solution {
9999
public:
100100
int maxScoreSightseeingPair(vector<int>& values) {
101-
int ans = 0, mx = values[0];
102-
for (int j = 1; j < values.size(); ++j) {
103-
ans = max(ans, values[j] - j + mx);
101+
int ans = 0, mx = 0;
102+
for (int j = 0; j < values.size(); ++j) {
103+
ans = max(ans, mx + values[j] - j);
104104
mx = max(mx, values[j] + j);
105105
}
106106
return ans;
@@ -112,9 +112,10 @@ public:
112112
113113
```go
114114
func maxScoreSightseeingPair(values []int) (ans int) {
115-
for j, mx := 1, values[0]; j < len(values); j++ {
116-
ans = max(ans, values[j]-j+mx)
117-
mx = max(mx, values[j]+j)
115+
mx := 0
116+
for j, x := range values {
117+
ans = max(ans, mx+x-j)
118+
mx = max(mx, x+j)
118119
}
119120
return
120121
}
@@ -124,16 +125,31 @@ func maxScoreSightseeingPair(values []int) (ans int) {
124125

125126
```ts
126127
function maxScoreSightseeingPair(values: number[]): number {
127-
let ans = 0;
128-
let mx = values[0];
129-
for (let j = 1; j < values.length; ++j) {
130-
ans = Math.max(ans, values[j] - j + mx);
128+
let [ans, mx] = [0, 0];
129+
for (let j = 0; j < values.length; ++j) {
130+
ans = Math.max(ans, mx + values[j] - j);
131131
mx = Math.max(mx, values[j] + j);
132132
}
133133
return ans;
134134
}
135135
```
136136

137+
#### Rust
138+
139+
```rust
140+
impl Solution {
141+
pub fn max_score_sightseeing_pair(values: Vec<i32>) -> i32 {
142+
let mut ans = 0;
143+
let mut mx = 0;
144+
for (j, &x) in values.iter().enumerate() {
145+
ans = ans.max(mx + x - j as i32);
146+
mx = mx.max(x + j as i32);
147+
}
148+
ans
149+
}
150+
}
151+
```
152+
137153
<!-- tabs:end -->
138154

139155
<!-- solution:end -->

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

+38-18
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ tags:
5555

5656
<!-- solution:start -->
5757

58-
### Solution 1
58+
### Solution 1: Enumeration
59+
60+
We can enumerate $j$ from left to right while maintaining the maximum value of $values[i] + i$ for elements to the left of $j$, denoted as $mx$. For each $j$, the maximum score is $mx + values[j] - j$. The answer is the maximum of these maximum scores for all positions.
61+
62+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{values}$. The space complexity is $O(1)$.
5963

6064
<!-- tabs:start -->
6165

@@ -64,10 +68,10 @@ tags:
6468
```python
6569
class Solution:
6670
def maxScoreSightseeingPair(self, values: List[int]) -> int:
67-
ans, mx = 0, values[0]
68-
for j in range(1, len(values)):
69-
ans = max(ans, values[j] - j + mx)
70-
mx = max(mx, values[j] + j)
71+
ans = mx = 0
72+
for j, x in enumerate(values):
73+
ans = max(ans, mx + x - j)
74+
mx = max(mx, x + j)
7175
return ans
7276
```
7377

@@ -76,9 +80,9 @@ class Solution:
7680
```java
7781
class Solution {
7882
public int maxScoreSightseeingPair(int[] values) {
79-
int ans = 0, mx = values[0];
80-
for (int j = 1; j < values.length; ++j) {
81-
ans = Math.max(ans, values[j] - j + mx);
83+
int ans = 0, mx = 0;
84+
for (int j = 0; j < values.length; ++j) {
85+
ans = Math.max(ans, mx + values[j] - j);
8286
mx = Math.max(mx, values[j] + j);
8387
}
8488
return ans;
@@ -92,9 +96,9 @@ class Solution {
9296
class Solution {
9397
public:
9498
int maxScoreSightseeingPair(vector<int>& values) {
95-
int ans = 0, mx = values[0];
96-
for (int j = 1; j < values.size(); ++j) {
97-
ans = max(ans, values[j] - j + mx);
99+
int ans = 0, mx = 0;
100+
for (int j = 0; j < values.size(); ++j) {
101+
ans = max(ans, mx + values[j] - j);
98102
mx = max(mx, values[j] + j);
99103
}
100104
return ans;
@@ -106,9 +110,10 @@ public:
106110
107111
```go
108112
func maxScoreSightseeingPair(values []int) (ans int) {
109-
for j, mx := 1, values[0]; j < len(values); j++ {
110-
ans = max(ans, values[j]-j+mx)
111-
mx = max(mx, values[j]+j)
113+
mx := 0
114+
for j, x := range values {
115+
ans = max(ans, mx+x-j)
116+
mx = max(mx, x+j)
112117
}
113118
return
114119
}
@@ -118,16 +123,31 @@ func maxScoreSightseeingPair(values []int) (ans int) {
118123

119124
```ts
120125
function maxScoreSightseeingPair(values: number[]): number {
121-
let ans = 0;
122-
let mx = values[0];
123-
for (let j = 1; j < values.length; ++j) {
124-
ans = Math.max(ans, values[j] - j + mx);
126+
let [ans, mx] = [0, 0];
127+
for (let j = 0; j < values.length; ++j) {
128+
ans = Math.max(ans, mx + values[j] - j);
125129
mx = Math.max(mx, values[j] + j);
126130
}
127131
return ans;
128132
}
129133
```
130134

135+
#### Rust
136+
137+
```rust
138+
impl Solution {
139+
pub fn max_score_sightseeing_pair(values: Vec<i32>) -> i32 {
140+
let mut ans = 0;
141+
let mut mx = 0;
142+
for (j, &x) in values.iter().enumerate() {
143+
ans = ans.max(mx + x - j as i32);
144+
mx = mx.max(x + j as i32);
145+
}
146+
ans
147+
}
148+
}
149+
```
150+
131151
<!-- tabs:end -->
132152

133153
<!-- solution:end -->
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 ans = 0, mx = values[0];
5-
for (int j = 1; j < values.size(); ++j) {
6-
ans = max(ans, values[j] - j + mx);
4+
int ans = 0, mx = 0;
5+
for (int j = 0; j < values.size(); ++j) {
6+
ans = max(ans, mx + values[j] - j);
77
mx = max(mx, values[j] + j);
88
}
99
return ans;
1010
}
11-
};
11+
};
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
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)
2+
mx := 0
3+
for j, x := range values {
4+
ans = max(ans, mx+x-j)
5+
mx = max(mx, x+j)
56
}
67
return
7-
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution {
22
public int maxScoreSightseeingPair(int[] values) {
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);
3+
int ans = 0, mx = 0;
4+
for (int j = 0; j < values.length; ++j) {
5+
ans = Math.max(ans, mx + values[j] - j);
66
mx = Math.max(mx, values[j] + j);
77
}
88
return ans;
99
}
10-
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def maxScoreSightseeingPair(self, values: List[int]) -> int:
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)
3+
ans = mx = 0
4+
for j, x in enumerate(values):
5+
ans = max(ans, mx + x - j)
6+
mx = max(mx, x + j)
77
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
impl Solution {
2+
pub fn max_score_sightseeing_pair(values: Vec<i32>) -> i32 {
3+
let mut ans = 0;
4+
let mut mx = 0;
5+
for (j, &x) in values.iter().enumerate() {
6+
ans = ans.max(mx + x - j as i32);
7+
mx = mx.max(x + j as i32);
8+
}
9+
ans
10+
}
11+
}

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
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);
2+
let [ans, mx] = [0, 0];
3+
for (let j = 0; j < values.length; ++j) {
4+
ans = Math.max(ans, mx + values[j] - j);
65
mx = Math.max(mx, values[j] + j);
76
}
87
return ans;

solution/1000-1099/1018.Binary Prefix Divisible By 5/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ tags:
6464

6565
### 方法一:模拟
6666

67-
遍历数组,每一次遍历都将当前数字加到前面的数字上,然后对 $5$ 取模,如果结果为 $0$,则当前数字可以被 $5$ 整除,答案设置为 `true`,否则为 `false`
67+
我们用一个变量 $x$ 来表示当前的二进制前缀,然后遍历数组 $nums$,对于每个元素 $v$,我们将 $x$ 左移一位,然后加上 $v$,再对 $5$ 取模,判断是否等于 $0$,如果等于 $0$,则说明当前的二进制前缀可以被 $5$ 整除,我们将 $\textit{true}$ 加入答案数组,否则将 $\textit{false}$ 加入答案数组
6868

69-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度
69+
时间复杂度 $O(n)$,忽略答案数组的空间消耗,空间复杂度 $O(1)$。
7070

7171
<!-- tabs:start -->
7272

solution/1000-1099/1018.Binary Prefix Divisible By 5/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ Only the first number is divisible by 5, so answer[0] is true.
6060

6161
<!-- solution:start -->
6262

63-
### Solution 1
63+
### Solution 1: Simulation
64+
65+
We use a variable $x$ to represent the current binary prefix, then traverse the array $nums$. For each element $v$, we left shift $x$ by one bit, then add $v$, and take the result modulo $5$. If the result equals $0$, it means the current binary prefix is divisible by $5$, and we add $\textit{true}$ to the answer array; otherwise, we add $\textit{false}$ to the answer array.
66+
67+
The time complexity is $O(n)$, and ignoring the space consumption of the answer array, the space complexity is $O(1)$.
6468

6569
<!-- tabs:start -->
6670

0 commit comments

Comments
 (0)