题目如下:

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:

  1. Input: [8,1,5,2,6]
  2. Output: 11
  3. 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,两者之和就是最终的结果。

代码如下:

  1. class Solution(object):
  2. def maxScoreSightseeingPair(self, A):
  3. """
  4. :type A: List[int]
  5. :rtype: int
  6. """
  7. l_1 = [-float('inf')] * len(A)
  8. l_2 = [-float('inf')] * len(A)
  9. max_1 = -float('inf')
  10. max_2 = -float('inf')
  11. res = -float('inf')
  12. for i in range(len(A)-1):
  13. max_1 = max(i + A[i],max_1)
  14. l_1[i] = max_1
  15.  
  16. max_2 = max(A[len(A) - 1-i] - (len(A) - 1-i),max_2)
  17. l_2[len(A) - 1-i] = max_2
  18.  
  19. if i >= len(A) / 2 -1:
  20. res = max(res,l_1[i] + l_2[i+1])
  21. res = max(res,l_1[len(A) - 1-i-1] + l_2[len(A) - 1-i])
  22. #print l_1
  23. #print l_2
  24. 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. 纯css实现瀑布流(multi-column多列及flex布局)

    瀑布流的布局自我感觉还是很吸引人的,最近又看到实现瀑布流这个做法,在这里记录下,特别的,感觉flex布局实现瀑布流还是有点懵的样子,不过现在就可以明白它的原理了 1.multi-column多列布局实 ...

  2. Linux(RHEL7)下安装vsftp服务

    1.安装vsftp(没有配置yum源的先配置yum源) yum install -y vsftpd 2.启动ftp服务 systemctl start vsftpd.service 3.打开防火墙 f ...

  3. vue里面的this指向

    this.$http.jsonp(api).then(function(response){ console.log(response); console.log(this); this.list=r ...

  4. Vim 8.0 版本安装方法及添加Python支持

    利用Git安装 最简单也是最有效的方法 1. 获取Vim仓库: git clone https://github.com/vim/vim.git 2. 升级到最新的版本: cd vim git pul ...

  5. linux下的sleep()和usleep()的使用和区别

    函数名: sleep头文件: #include<windows.h> // 在VC中使用带上头文件              #include<unistd.h>    // ...

  6. mysql内存数据淘汰机制和大查询会不会把内存打爆?

    首先我们说一下大查询会不会把内存打爆? 比如说主机内存有5g,但是我们一个大查询的数据有10g,这样会不会把内存打爆呢? 答案:不会 为什么? 因为mysql读取数据是采取边读边发的策略 select ...

  7. java创建线程的两种方式及源码解析

    创建线程的方式有很多种,下面我们就最基本的两种方式进行说明.主要先介绍使用方式,再从源码角度进行解析. 继承Thread类的方式 实现Runnable接口的方式 这两种方式是最基本的创建线程的方式,其 ...

  8. 微信小程序(二)--逻辑层与界面层

    一.逻辑层与界面层分离 小程序开发框架将我们需要完成的编码,划分成了两种类型的编码:逻辑编码(由JavaScript完成,业务数据供给界面事件处理),界面编码(页面结构WXML,页面样式WXSS,展示 ...

  9. uploadify多图片和文件上传网站应用

    先要下载压缩包 www.uploadify.com/wp-content/uploads/files/uploadify.zip 1,模板文件引用 <!--引用jquery uploady*}- ...

  10. Swift编程语言学习1.6——可选值

    可选值 使用可选(optionals)来处理值可能缺失的情况.可选表示: 有值,等于 x   或者没有值 注意: C 和 Objective-C 中并没有可选这个概念.最接近的是 Objective- ...