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. java集合: LinkedList源码浅析

    LinkedList 数据结构是双向链表,插入删除比较方便.LinkedList 是线程不安全的,允许元素为null  . 构造函数: 构造函数是空的. /** * Constructs an emp ...

  2. 织梦栏目判断 seotitle的小bug

    有的栏目有seotitle(中文字符),有的没有,页面显示需要把seotitle放在括号中,所以进行了以下代码: {dede:field name="seotitle" runph ...

  3. 100-days: Five

    Title: Feel better now ? The rise and rise of the anxiety economy(焦虑经济) rise and rise 一直上升 anxiety n ...

  4. python常见的数据结构

    https://www.cnblogs.com/5poi/p/7466760.html

  5. iOS-引用计数与ARC(转)

    以下是关于内存管理的学习笔记:引用计数与ARC. iOS5以前自动引用计数(ARC)是在MacOS X 10.7与iOS 5中引入一项新技术,用于代替之前的手工引用计数MRC(Manual Refer ...

  6. linux命令学习之:du

    du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 语法 du [选项][文件] 选项 -a或-all 显示目录中个 ...

  7. 关于界面绘制过程多次回调ondraw()方法产生的问题

    最近项目中,出现一个问题,要做成的效果是这样的,但是一进去就变成这样了, 后来发现,刚进去是正常的,一闪而过,就变成全部了. 界面绘制过程,ondraw() 会被多次回调. 就是说在第一次绘制的时候是 ...

  8. 微信小程序模板中使用循环

    原创文章 本篇介绍如何在模板里使用循环. 方法一: template.wxml,该文件内容为: <template name="msgItem1"> <block ...

  9. 在Eclipse中安装python插件的方法

    一个博士给了我一堆代码,原本以为是C++或者java写的,结果是python,我压根没学过呀,不过本着语言都是相通的原则,我硬着头皮开始学习Python,当然先学习安装IDE(以前学习一门新语言,我会 ...

  10. Struts2框架之Action类的访问

    1. 通过<action>标签中的method属性,访问到Action中的具体的方法. * 传统的配置方式,配置更清晰更好理解!但是扩展需要修改配置文件等! * 具体的实例如下: * 页面 ...