diff options
author | Jack Sangdahl <[email protected]> | 2024-12-25 19:27:08 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-12-26 21:09:14 -0700 |
commit | 96f4b139b722ffd32f1130499733f39000eabab4 (patch) | |
tree | 087cef5b7e5640a161ed3ff938d8af4a07adac03 | |
parent | fe6e689f0e172de9d58a2d94fa55a438813464ba (diff) | |
download | leetcode-96f4b139b722ffd32f1130499733f39000eabab4.tar.gz leetcode-96f4b139b722ffd32f1130499733f39000eabab4.zip |
1014. best sightseeing pair
-rw-r--r-- | best_sightseeing_pair.c | 26 | ||||
-rw-r--r-- | best_sightseeing_pair.py | 29 |
2 files changed, 55 insertions, 0 deletions
diff --git a/best_sightseeing_pair.c b/best_sightseeing_pair.c new file mode 100644 index 0000000..43bc205 --- /dev/null +++ b/best_sightseeing_pair.c @@ -0,0 +1,26 @@ +// 1014. Best Sightseeing Pair +#include "leetcode.h" + +/* + * you are given an integer array 'values' where 'values[i]' represents the + * value of the i'th sightseeing spot. two sightseeing spots 'i' and 'j' have a + * distance 'j - i' between them. the score of a pair ('i < j') of sightseeing + * spots is 'values[i] + values[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. + */ + +int maxScoreSightseeingPair(int *values, int valuesSize) { + int ans = 0, curr = 0; + for (int i = 0; i < valuesSize; i++) { + ans = fmax(ans, curr + values[i]); + curr = fmax(curr, values[i]) - 1; + } + return ans; +} + +int main() { + int v1[] = {8, 1, 5, 2, 6}, v2[] = {1, 2}; + printf("%d\n", maxScoreSightseeingPair(v1, ARRAY_SIZE(v1))); // expect: 11 + printf("%d\n", maxScoreSightseeingPair(v2, ARRAY_SIZE(v2))); // expect: 2 +} diff --git a/best_sightseeing_pair.py b/best_sightseeing_pair.py new file mode 100644 index 0000000..7fecb66 --- /dev/null +++ b/best_sightseeing_pair.py @@ -0,0 +1,29 @@ +# 1014. Best Sightseeing Pair + +""" +you are given an integer array 'values' where 'values[i]' represents the +value of the i'th sightseeing spot. two sightseeing spots 'i' and 'j' have a +distance 'j - i' between them. the score of a pair ('i < j') of sightseeing +spots is 'values[i] + values[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. +""" + + +class Solution(object): + def maxScoreSightseeingPair(self, values): + """ + :type values: List[int] + :rtype: int + """ + ans, curr = 0, 0 + for i in values: + ans = max(ans, curr + i) + curr = max(curr, i) - 1 + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.maxScoreSightseeingPair(values=[8, 1, 5, 2, 6])) + print(obj.maxScoreSightseeingPair(values=[1, 2])) |