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.

Example 1:

Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11

Note:

  1. 2 <= A.length <= 50000
  2. 1 <= A[i] <= 1000

Idea 1.  max(A[i] + A[j] + i -j) (i < j) = max(max(A[i] + i) + A[j] -j), scan from left to right

Time complexity: O(n)

Space complexity: O(1)

 class Solution {
public int maxScoreSightseeingPair(int[] A) {
int maxScore = Integer.MIN_VALUE;
int sumVal = Integer.MIN_VALUE;
for(int i = 1; i < A.length; ++i) {
sumVal = Math.max(sumVal, A[i-1] + (i-1));
maxScore = Math.max(maxScore, A[i] - i + sumVal);
} return maxScore;
}
}

Idea 1.b scan from right to left, max(A[i] + A[j] + i -j) (i < j)  = max(max(A[j] -j) + A[i] + i)

 class Solution {
public int maxScoreSightseeingPair(int[] A) {
int maxScore = Integer.MIN_VALUE;
int sumVal = Integer.MIN_VALUE;
for(int i = A.length-2; i >= 0; --i) {
sumVal = Math.max(sumVal, A[i+1] - (i+1));
maxScore = Math.max(maxScore, A[i] + i + sumVal);
} return maxScore;
}
}

Best Sightseeing Pair LT1014的更多相关文章

  1. [Swift]LeetCode1014. 最佳观光组合 | Best Sightseeing Pair

    Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and t ...

  2. 【leetcode】1021. Best Sightseeing Pair

    题目如下: Given an array A of positive integers, A[i]represents the value of the i-th sightseeing spot, ...

  3. 【LeetCode】1021. Best Sightseeing Pair 最佳观光组合(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. Leetcode 1014. Best Sightseeing Pair

    本题是leetcode121这道题的翻版,做法完全一样,也是扫一遍数组,维护两个值,一个是a[i]+i的最大值,另一个是a[i]+a[j]+i-j的最大值. class Solution: def m ...

  5. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  6. Weekly Contest 129

    1020. Partition Array Into Three Parts With Equal Sum Given an array A of integers, return true if a ...

  7. HDU 1688 Sightseeing&HDU 3191 How Many Paths Are There(Dijkstra变形求次短路条数)

    Sightseeing Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. POJ 3621 Sightseeing Cows(最优比例环+SPFA检测)

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10306   Accepted: 3519 ...

  9. POJ 4046 Sightseeing

    Sightseeing Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID ...

随机推荐

  1. native和webview切换

    native和webview 标签(空格分隔): native和webview 现在目前大部分的app都是native和webview混合,对应的native上的元素可以通过uiautomatorvi ...

  2. Pandas基本功能之选取索引和过滤

    索引.选取和过滤 大部分的查询用法 类型 说明 obj[val] 选取DataFrame的单个列或一组列 obj.ix[val] 选取DataFrame的单个行或一组行 obj.ix[:,val] 选 ...

  3. linux按键映射

    .Xmodmap keycode = Mode_switch keysym k = k K Left keysym j = j J Down keysym l = l L Right keysym h ...

  4. struts2前后台传值的三种方法

    原文地址: http://laokaddk.blog.51cto.com/368606/1340816 多的不说,直接上代码; struts.xml代码: <?xml version=" ...

  5. vue 引用公共方法(例子:截取字符串固定字数,其余显示...)

    1.写公共js 2.main.js引入公共js 3.在组件中用this.common.方法名 引用

  6. f5 irules

    1.插入XFF when HTTP_REQUEST { if { [HTTP::header exists X-Forward-For] } { set old_xff [HTTP::header v ...

  7. declare -A color

    #!/bin/bash ## 声明变量 declare -A color # 定义颜色 # bc_color : background color color[red]="\e[1;31m& ...

  8. cloudera-hdfs 告警处理

    2018-03-13 11:15:17,215 WARN [org.apache.flume.sink.hdfs.HDFSEventSink] - HDFS IO error org.apache.h ...

  9. MYSQL分析慢查询

    mysql慢查询的日志文件路径一般为: /var/lib/mysql/slowquery.log,具体的路径可以通过mysql配置文件(/etc/my.cnf)查询,slow_query_log_fi ...

  10. 线特征---LineMatching代码运行(五)

    [1]    https://github.com/dlut-dimt/LineMatching The code is based on Matlab.  https://github.com/ka ...