Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 867dc31

Browse filesBrowse files
committed
add 1014
1 parent 0d73c0e commit 867dc31
Copy full SHA for 867dc31

File tree

3 files changed

+89
-0
lines changed
Filter options

3 files changed

+89
-0
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
| 50 | [Pow(x, n)][050] | Math, Binary Search |
8989
| 56 | [Merge Intervals][056] | Array, Sort |
9090
| 554 | [Brick Wall][554] | Hash Table |
91+
| 1014 | [最佳观光组合][1014] | 数组 |
9192

9293

9394
## Hard
@@ -170,6 +171,7 @@
170171
[050]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/050/README.md
171172
[056]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/056/README.md
172173
[554]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/554/README.md
174+
[1014]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/1014/README.md
173175

174176
[004]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/004/README.md
175177
[010]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/010/README.md

‎note/1014/README.md

Copy file name to clipboard
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# [最佳观光组合][title]
2+
3+
## 题目描述
4+
5+
给定正整数数组 `A``A[i]` 表示第 `i` 个观光景点的评分,并且两个景点 `i``j` 之间的距离为 `j - i`
6+
7+
一对景点(`i < j`)组成的观光组合的得分为(`A[i] + A[j] + i - j`):景点的评分之和**减去**它们两者之间的距离。
8+
9+
返回一对观光景点能取得的最高分。
10+
11+
**示例:**
12+
13+
```
14+
输入:[8,1,5,2,6]
15+
输出:11
16+
解释:i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
17+
```
18+
19+
**提示:**
20+
21+
1. `2 <= A.length <= 50000`
22+
2. `1 <= A[i] <= 1000`
23+
24+
**标签:** 数组
25+
26+
27+
## 思路
28+
29+
直接暴力两层 for 循环肯定过不了关,我们把公式变化为 `(A[i] + i) + (A[j] - j)`,看到此应该就可以想到在每次遍历 `j` 时,只需要知道 `max(A[i] + i)` 即可。
30+
31+
```java
32+
class Solution {
33+
34+
public int maxScoreSightseeingPair(int[] A) {
35+
int ans = 0, cur = A[0] + 0;
36+
for (int j = 1; j < A.length; j++) {
37+
ans = Math.max(ans, cur + A[j] - j); // 计算当前最大得分
38+
cur = Math.max(cur, A[j] + j); // 更新最大的 A[i] + i
39+
}
40+
return ans;
41+
}
42+
43+
public static void main(String[] args) {
44+
Solution solution = new Solution();
45+
int[] A = new int[]{8, 1, 5, 2, 6};
46+
System.out.println(solution.maxScoreSightseeingPair(A));
47+
}
48+
}
49+
50+
```
51+
52+
53+
## 结语
54+
55+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-java-leetcode][ajl]
56+
57+
58+
59+
[title]: https://leetcode-cn.com/problems/best-sightseeing-pair/
60+
[ajl]: https://github.com/Blankj/awesome-java-leetcode
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.blankj.medium._1014;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2020/06/18
8+
* desc :
9+
* </pre>
10+
*/
11+
public class Solution {
12+
13+
public int maxScoreSightseeingPair(int[] A) {
14+
int ans = 0, cur = A[0] + 0;
15+
for (int j = 1; j < A.length; j++) {
16+
ans = Math.max(ans, cur + A[j] - j); // 计算当前最大得分
17+
cur = Math.max(cur, A[j] + j); // 更新最大的 A[i] + i
18+
}
19+
return ans;
20+
}
21+
22+
public static void main(String[] args) {
23+
Solution solution = new Solution();
24+
int[] A = new int[]{8, 1, 5, 2, 6};
25+
System.out.println(solution.maxScoreSightseeingPair(A));
26+
}
27+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.