题目如下:

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. PHP 最全的正则表达式

    一.校验数字的表达式  1 数字:^[0-9]*$2 n位的数字:^\d{n}$3 至少n位的数字:^\d{n,}$4 m-n位的数字:^\d{m,n}$5 零和非零开头的数字:^(0|[1-9][0 ...

  2. 黑马lavarel教程---2、获取用户输入

    黑马lavarel教程---2.获取用户输入 一.总结 一句话总结: lavarel中获取用户输入可以通过Input外观模式和Request外观模式,两者的对应的方法啥的都一样,比如get.all.o ...

  3. loj#501 「LibreOJ β Round」ZQC 的树列

    分析 代码(我的代码是瞎jb水过去的) #include<bits/stdc++.h> using namespace std; #define li long long li a[]; ...

  4. eclipse下实现热部署,tomcat不重新reload context

    1. 打开server的编辑器 2. 在modules页签内,修改auto load属性为disabled

  5. Git错误总结

    1.error: failed to push some refs to ‘git@github.com:XXXX/XXXX‘ hint: Updates were rejected because ...

  6. 如何根据字典值的大小,对字典中的项排序---Python数据结构与算法相关问题与解决技巧

    实际案例: 某班英语成绩以字典形式存储为: { 'LiLei' : 90, 'Jim' : 88, 'Lucy': 92 } 如何根据成绩高低,计算学生排名 -- 根据分数,进行排名,并且把排名信息添 ...

  7. 转载 IDEA搭建maven项目详细步骤(解决没有src及其下面的文件)

    转载自

  8. SAP选择屏幕开发(二)(转)

    原文链接:https://blog.csdn.net/wtxhai/article/details/90698683 1.2.SAP屏幕框架的创建        SAP页面设计中的框架不但可以保证SA ...

  9. 数据库之Query Builder

    Yii的查询构造器提供了一个用面向对象的方法来构造SQL语句.他让开发人员可以用类的方法,属性来作为SQL语句的一部分.然后把不同部分组装到一个正确的SQL语句中,调用DAO的方法来执行.下面的例子演 ...

  10. 阿里云云监控agent插件 - Linux版

    阿里云云监控agent插件使用指南 1.安装(注意,要以“root”权限运行,复制 sudo后面的就行,别把#也复制进去) #64位 # sudo bash -c "wget -e 'htt ...