Skip to content

Commit 2e94975

Browse files
committed
Add solution #1014
1 parent 24159bd commit 2e94975

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,088 LeetCode solutions in JavaScript
1+
# 1,089 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -822,6 +822,7 @@
822822
1011|[Capacity To Ship Packages Within D Days](./solutions/1011-capacity-to-ship-packages-within-d-days.js)|Medium|
823823
1012|[Numbers With Repeated Digits](./solutions/1012-numbers-with-repeated-digits.js)|Hard|
824824
1013|[Partition Array Into Three Parts With Equal Sum](./solutions/1013-partition-array-into-three-parts-with-equal-sum.js)|Easy|
825+
1014|[Best Sightseeing Pair](./solutions/1014-best-sightseeing-pair.js)|Medium|
825826
1022|[Sum of Root To Leaf Binary Numbers](./solutions/1022-sum-of-root-to-leaf-binary-numbers.js)|Easy|
826827
1023|[Camelcase Matching](./solutions/1023-camelcase-matching.js)|Medium|
827828
1028|[Recover a Tree From Preorder Traversal](./solutions/1028-recover-a-tree-from-preorder-traversal.js)|Hard|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 1014. Best Sightseeing Pair
3+
* https://leetcode.com/problems/best-sightseeing-pair/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array values where values[i] represents the value of the ith sightseeing
7+
* spot. Two sightseeing spots i and j have a distance j - i between them.
8+
*
9+
* The score of a pair (i < j) of sightseeing spots is values[i] + values[j] + i - j: the sum of the
10+
* values of the sightseeing spots, minus the distance between them.
11+
*
12+
* Return the maximum score of a pair of sightseeing spots.
13+
*/
14+
15+
/**
16+
* @param {number[]} values
17+
* @return {number}
18+
*/
19+
var maxScoreSightseeingPair = function(values) {
20+
let maxLeft = values[0];
21+
let result = 0;
22+
23+
for (let j = 1; j < values.length; j++) {
24+
result = Math.max(result, maxLeft + values[j] - j);
25+
maxLeft = Math.max(maxLeft, values[j] + j);
26+
}
27+
28+
return result;
29+
};

0 commit comments

Comments
 (0)