【leetcode】1021. Best Sightseeing Pair
题目如下:
Given an array
A
of positive integers,A[i]
represents the value of thei
-th sightseeing spot, and two sightseeing spotsi
andj
have distancej - 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:
2 <= A.length <= 50000
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的更多相关文章
- 【LeetCode】1021. Best Sightseeing Pair 最佳观光组合(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】1021. Remove Outermost Parentheses
题目如下: A valid parentheses string is either empty (""), "(" + A + ")", ...
- 【LeetCode】1021. Remove Outermost Parentheses 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【leetcode】668. Kth Smallest Number in Multiplication Table
题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...
- 【LeetCode】面试题13. 机器人的运动范围
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】1162. 地图分析 As Far from Land as Possible(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 这个题想考察什么? 剩下的任务就是套模板! 日期 题目 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
随机推荐
- cannot be resolved to a type 错误解决方法
引言: eclipse新导入的项目经常可以看到“XX cannot be resolved to a type”的报错信息.本文将做以简单总结. 正文: (1)jdk不匹配(或不存在) ...
- Delphi IdHttp组件+IdHttpServer组件实现文件下载服务
http://blog.csdn.net/xxkku521/article/details/16864759 Delphi IdHttp组件+IdHttpServer组件实现文件下载服务 2013- ...
- Nginx 在各种语言框架下的配置 - 以 codeigniter 为例
对于各种语言常用的框架,Nginx 在官方的 Wiki 页面的 入门 部分提供了示例配置文件.具体可以参考这个页面的 Pre-canned Configurations 部分,这里列出了各种框架. 直 ...
- ETCD 简介及基本用法
ETCD 项目地址 ETCD 官方文档 1. 简介 ETCD 是一个高可用的分布式键值数据库,可用于服务发现.ETCD 采用 raft 一致性算法,基于 Go 语言实现. 特点 简单:安装配置使用简单 ...
- HTML-字符实体,平方米(㎡)m²
转载自:https://blog.csdn.net/hangGe0111/article/details/80983250
- SVG绘制多个圆
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Postman 测试Xfire webservice
权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/u013177381/article/det ...
- telnet批量检测端口状态(linux)
批量检测端口通信: 准备文件树状图 telnet.sh 脚本内容如下: 文件说明 telnet_alive.txt : 活动的端口 telnet_die.txt : 离线的端口 telnet ...
- Array.prototype.includes
if (!Array.prototype.includes) { Array.prototype.includes = function(searchElement /*, fromIndex*/ ...
- 浏览器调起摄像头(jquery+layui)
/* 实例化camvas配置参数 config = { video:{width:Number(scale*4),height:Number(scale*3)},//视频比例4:3 canvasId: ...