Skip to content

Add solution for 1014 #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Your ideas/fixes/algorithms are more than welcome!
|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|
|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|
|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|
|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|
|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|
|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|
|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|
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/fishercoder/solutions/_1014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.fishercoder.solutions;

/**
* 1014. Best Sightseeing Pair
*
* Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot,
* and two sightseeing spots i and j have distance j - i between them.
*
* The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) :
* the sum of the values of the sightseeing spots, minus the distance between them.
*
* Return the maximum score of a pair of sightseeing spots.
*
* Note:
* * 2 <= A.length <= 50000
* * 1 <= A[i] <= 1000
*/

public class _1014 {
public static class Solution1 {
public int maxScoreSightseeingPair(int[] A) {
int bestPrevious = A[0];
int maxSum = 0;

for (int i = 1; i < A.length; ++i) {
maxSum = Math.max(maxSum, bestPrevious + A[i] - i);
bestPrevious = Math.max(bestPrevious, A[i] + i);
}

return maxSum;
}
}
}
26 changes: 26 additions & 0 deletions src/test/java/com/fishercoder/_1014Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.fishercoder;

import com.fishercoder.solutions._1014;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class _1014Test {
private static _1014.Solution1 solution1;

@BeforeClass
public static void setup() {
solution1 = new _1014.Solution1();
}

@Test
public void test1() {
assertEquals(solution1.maxScoreSightseeingPair(new int[]{1, 3, 5}), 7);
}

@Test
public void test2() {
assertEquals(solution1.maxScoreSightseeingPair(new int[]{8, 1, 5, 2, 6}), 11);
}
}