Skip to content

Commit 11211e8

Browse files
committed
[LeetCode Sync] Runtime - 71 ms (48.93%), Memory - 23.3 MB (21.48%)
1 parent a5d0540 commit 11211e8

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<p>You are given an integer array <code>values</code> where values[i] represents the value of the <code>i<sup>th</sup></code> sightseeing spot. Two sightseeing spots <code>i</code> and <code>j</code> have a <strong>distance</strong> <code>j - i</code> between them.</p>
2+
3+
<p>The score of a pair (<code>i &lt; j</code>) of sightseeing spots is <code>values[i] + values[j] + i - j</code>: the sum of the values of the sightseeing spots, minus the distance between them.</p>
4+
5+
<p>Return <em>the maximum score of a pair of sightseeing spots</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> values = [8,1,5,2,6]
12+
<strong>Output:</strong> 11
13+
<strong>Explanation:</strong> i = 0, j = 2, values[i] + values[j] + i - j = 8 + 5 + 0 - 2 = 11
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> values = [1,2]
20+
<strong>Output:</strong> 2
21+
</pre>
22+
23+
<p>&nbsp;</p>
24+
<p><strong>Constraints:</strong></p>
25+
26+
<ul>
27+
<li><code>2 &lt;= values.length &lt;= 5 * 10<sup>4</sup></code></li>
28+
<li><code>1 &lt;= values[i] &lt;= 1000</code></li>
29+
</ul>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def maxScoreSightseeingPair(self, values: List[int]) -> int:
3+
# score = values[i] + values[j] + i - j
4+
n = len(values)
5+
6+
# left score initially is value of first element
7+
max_left_score = values[0]
8+
9+
res = 0
10+
11+
for i in range(1,n):
12+
current_right_score = values[i] - i
13+
res = max(res, max_left_score + current_right_score)
14+
15+
current_left_score = values[i] + i
16+
max_left_score = max(max_left_score, current_left_score)
17+
18+
return res

0 commit comments

Comments
 (0)