题目如下:

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

解题思路:本题和【leetcode】123. Best Time to Buy and Sell Stock III 有点类似。题目要求求出A[i] + A[j] + i - j最大值,这个表达式可以等价变为 (A[i] + i ) + (A[j] - j),对于A中每一个元素来说,都对应着两个值,分别是A[i]+i和A[i]-i。本题的解法可以抽象成把A分成两部分,第一部分找出使得A[i]+i能得到最大值的i,第二部分找出A[j]-j能得到最大值的j,两者之和就是最终的结果。

代码如下:

class Solution(object):
def maxScoreSightseeingPair(self, A):
"""
:type A: List[int]
:rtype: int
"""
l_1 = [-float('inf')] * len(A)
l_2 = [-float('inf')] * len(A)
max_1 = -float('inf')
max_2 = -float('inf')
res = -float('inf')
for i in range(len(A)-1):
max_1 = max(i + A[i],max_1)
l_1[i] = max_1 max_2 = max(A[len(A) - 1-i] - (len(A) - 1-i),max_2)
l_2[len(A) - 1-i] = max_2 if i >= len(A) / 2 -1:
res = max(res,l_1[i] + l_2[i+1])
res = max(res,l_1[len(A) - 1-i-1] + l_2[len(A) - 1-i])
#print l_1
#print l_2
return res

【leetcode】1021. Best Sightseeing Pair的更多相关文章

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

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

  2. 【leetcode】1021. Remove Outermost Parentheses

    题目如下: A valid parentheses string is either empty (""), "(" + A + ")", ...

  3. 【LeetCode】1021. Remove Outermost Parentheses 解题报告(Python)

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

  4. 【LeetCode】堆 heap(共31题)

    链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...

  5. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  6. 【leetcode】668. Kth Smallest Number in Multiplication Table

    题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...

  7. 【LeetCode】面试题13. 机器人的运动范围

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

  8. 【LeetCode】1162. 地图分析 As Far from Land as Possible(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 这个题想考察什么? 剩下的任务就是套模板! 日期 题目 ...

  9. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

随机推荐

  1. 170821-关于SpringMVC的知识点

    1.SpringMVC 概述以及优势 SpringMVC和Spring的关系:  软件开发的三层架构: web层[表示层.表现层]---->Service层---->Dao[DataBas ...

  2. 阿里云数据库导出-本地mysql导入

    因阿里云数据库标准访问模式不支持外网接入 因此导出一份到本地,注意选择编码为utf8 mysql 命令行用source导入有utf8编码的sql文件时导入的数据有乱码解决办法 set names ut ...

  3. Gym 100917M Matrix, The

    题目链接: http://codeforces.com/gym/100917/problem/M --------------------------------------------------- ...

  4. sqlalchemy.exc.IntegrityError: (mysql.connector.errors.IntegrityError) 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

    排错: 看到 Duplicate entry '1' for key 'PRIMARY'是主键错误 看一看自己添加的数据id是1 再查询一下数据库中的表,发现id=1的记录已经存在了 所以在代码中让i ...

  5. mysql_DCL_grant/revoke

    授权就是为某个用户赋予某些权限.例如,可以为新建的用户赋予查询所有数据库和表的权限.合理的授权能够保证数据库的安全.不合理的授权会使数据库存在安全隐患. MySQL中使用GRANT关键字来为用户设置权 ...

  6. Delphi IdHttp组件+IdHttpServer组件实现文件下载服务

     http://blog.csdn.net/xxkku521/article/details/16864759 Delphi IdHttp组件+IdHttpServer组件实现文件下载服务 2013- ...

  7. MapReduce(1): Prepare input for Mappers

    According to Wikipedia MapReduce, there are two ways to illustrate MapReduce. One contains three ste ...

  8. hdu 6298 Maximum Multiple(规律)

    hdu6298 Maximum Multiple 题目传送门 题意: 给你一个整数n,从中找出可以被n整除的三个数x,y,z: 要求x+y+z=n,且x*y*z最大. 思路: 开始一看T到1e6,n也 ...

  9. KindEditor 文本编辑器

    官网:http://kindeditor.net/docs/usage.html 目前支持ASP.ASP.NET.PHP.JSP.

  10. elasticsearch 基础 —— 分布式文档存储原理

    路由一个文档到一个分片中 当索引一个文档的时候,文档会被存储到一个主分片中. Elasticsearch 如何知道一个文档应该存放到哪个分片中呢?当我们创建文档时,它如何决定这个文档应当被存储在分片  ...