作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/best-sightseeing-pair/

题目描述

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

题目大意

对于数组中的两个下标i, j,求A[i] + A[j] + i - j最大值.

解题方法

一般暴力解法是两重循环,即先找到一个位置 i 之后,从其后位置 j 继续循环遍历,这样的复杂度是O(N ^ 2) 的。看到题目给出的数据范围,数组的长度是 50000, 对于这个题O(N ^ 2) 的时间复杂度肯定不可取。

对于这个题,我们知道肯定只能用 O(N) 的解法,把公式稍加变化成(A[i] + i) + (A[j] - j),我们可以看出要找出这两部分和的最大值。

具体方法是保存每个位置 j ,时刻维护其前面的A[i] + i的最大值出现的位置 pre_i。最后结果就是对于每个位置j对应的A[i] + A[j] + i - j最大值。

Python代码如下:

class Solution(object):
def maxScoreSightseeingPair(self, A):
"""
:type A: List[int]
:rtype: int
"""
pre_i = 0
res = 0
for j in range(1, len(A)):
res = max(A[j] - j + A[pre_i] + pre_i, res)
if A[j] + j > A[pre_i] + pre_i:
pre_i = j
return res

参考资料:https://leetcode.com/problems/best-sightseeing-pair/discuss/260850/JavaC%2B%2BPython-One-Pass

日期

2019 年 3 月 24 日 —— 这个周赛太悲催了

【LeetCode】1021. Best Sightseeing Pair 最佳观光组合(Python)的更多相关文章

  1. Leetcode 1021. 最佳观光组合

    1021. 最佳观光组合  显示英文描述 我的提交返回竞赛   用户通过次数91 用户尝试次数246 通过次数92 提交次数619 题目难度Medium 给定正整数数组 A,A[i] 表示第 i 个观 ...

  2. [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 ...

  3. 【leetcode】1021. Best Sightseeing Pair

    题目如下: Given an array A of positive integers, A[i]represents the value of the i-th sightseeing spot, ...

  4. Leetcode 1014. Best Sightseeing Pair

    本题是leetcode121这道题的翻版,做法完全一样,也是扫一遍数组,维护两个值,一个是a[i]+i的最大值,另一个是a[i]+a[j]+i-j的最大值. class Solution: def m ...

  5. 【LeetCode】456. 132 Pattern 解题报告(Python)

    [LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  6. 6个最佳的开源Python应用服务器

    6个最佳的开源Python应用服务器 首先,你知道什么是应用服务器吗?应用服务器通常被描述为是存在于服务器中心架构中间层的一个软件框架. AD: 首先,你知道什么是应用服务器吗?应用服务器通常被描述为 ...

  7. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  8. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  9. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. 关于AnnotationHub的一些应用

    AnnotationHub是一个包含大量注释信息的数据库,里面有很多物种,以及来源于很多数据库的注释信息. 1,安装这个包 source("https://bioconductor.org/ ...

  2. phpMyAdmin简介及安装

    phpMyAdmin是一个MySQL数据库管理工具,通过Web接口管理数据库方便快捷. Linux系统安装phpMyAdmin phpMyAdmin是一个MySQL数据库管理工具,通过Web接口管理数 ...

  3. 52-Linked List Cycle

    Linked List Cycle My Submissions QuestionEditorial Solution Total Accepted: 102785 Total Submissions ...

  4. gg=G

    1.代码格式化对齐 2.直接按下ESE模式下就可以来执行了

  5. WebRTC本地分享屏幕,录制屏幕

    WebRTC有分享屏幕的功能.使用的是getDisplayMedia方法.用户同意分享屏幕后,可以拿到视频流. 再结合MediaRecorder和Blob,把视频流数据存下来,就能得到录制屏幕的视频. ...

  6. 在 windows 系统上 安装与配置 PHP + Apache

    参考:http://www.cnblogs.com/pharen/archive/2012/02/06/2340628.html 在大学时候上过一门PHP课时,因为课堂需要配置过一次PHP+Mysql ...

  7. ssh : connect to host XXX.XXX.XXX.XXX port : 22 connect refused

    初学者 写博客 如有不对之处请多多指教 我是要在俩个主机的俩个虚拟机上 用scp (security copy)进行文件远程复制. 但是 终端 提示 ssh : connect to host XXX ...

  8. linux 定时导出sql查询结果文件

    如果想在服务器端生成sql查询结果的txt文件. 大体思路就是: 1.创建一个到处txt文件的sql脚本. set ARRAYSIZE 50 --从数据库往客户端一次发送记录数 set linesiz ...

  9. 优化 if-else 代码的 8 种方案

    前言 代码中如果if-else比较多,阅读起来比较困难,维护起来也比较困难,很容易出bug,接下来,本文将介绍优化if-else代码的八种方案. 方案. 优化方案一:提前return,去除不必要的el ...

  10. 【Linux】【Services】【Docker】网络

    容器的网络模型: closed container: 仅有一个接口:loopback 不参与网络通信,仅适用于无须网络通信的应用场景,例如备份.程序调试等: --net none bridged co ...