Skip to content

Commit 9357666

Browse files
havanagrawalfishercoder1534
authored andcommitted
Add solution for 1014 (#52)
1 parent 5a0ec83 commit 9357666

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Your ideas/fixes/algorithms are more than welcome!
3232
|1021|[Remove Outermost Parentheses](https://leetcode.com/problems/remove-outermost-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1021.java) | O(n) | O(n) | |Easy|
3333
|1020|[Number of Enclaves](https://leetcode.com/problems/number-of-enclaves/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1020.java) | O(mn) | O(mn) | |Medium|Graph, DFS, BFS, recursion|
3434
|1018|[Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1018.java) | O(n) | O(1) | |Easy|
35+
|1014|[Best Sightseeing Pair](https://leetcode.com/problems/best-sightseeing-pair/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1014.java) | O(n) | O(1) | |Medium|
3536
|1013|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1013.java) | O(n) | O(1) | |Easy|
3637
|1009|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1009.java) | O(n) | O(1) | |Easy|
3738
|1003|[Check If Word Is Valid After Substitutions](https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1003.java) | O(n) | O(n) | |Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1014. Best Sightseeing Pair
5+
*
6+
* Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot,
7+
* and two sightseeing spots i and j have distance j - i between them.
8+
*
9+
* The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) :
10+
* the sum of the values of the sightseeing spots, minus the distance between them.
11+
*
12+
* Return the maximum score of a pair of sightseeing spots.
13+
*
14+
* Note:
15+
* * 2 <= A.length <= 50000
16+
* * 1 <= A[i] <= 1000
17+
*/
18+
19+
public class _1014 {
20+
public static class Solution1 {
21+
public int maxScoreSightseeingPair(int[] A) {
22+
int bestPrevious = A[0];
23+
int maxSum = 0;
24+
25+
for (int i = 1; i < A.length; ++i) {
26+
maxSum = Math.max(maxSum, bestPrevious + A[i] - i);
27+
bestPrevious = Math.max(bestPrevious, A[i] + i);
28+
}
29+
30+
return maxSum;
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1014;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1014Test {
10+
private static _1014.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1014.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(solution1.maxScoreSightseeingPair(new int[]{1, 3, 5}), 7);
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(solution1.maxScoreSightseeingPair(new int[]{8, 1, 5, 2, 6}), 11);
25+
}
26+
}

0 commit comments

Comments
 (0)