【LeetCode】1021. Best Sightseeing Pair 最佳观光组合(Python)
作者: 负雪明烛
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:
2 <= A.length <= 50000
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)的更多相关文章
- Leetcode 1021. 最佳观光组合
1021. 最佳观光组合 显示英文描述 我的提交返回竞赛 用户通过次数91 用户尝试次数246 通过次数92 提交次数619 题目难度Medium 给定正整数数组 A,A[i] 表示第 i 个观 ...
- [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 ...
- 【leetcode】1021. Best Sightseeing Pair
题目如下: Given an array A of positive integers, A[i]represents the value of the i-th sightseeing spot, ...
- Leetcode 1014. Best Sightseeing Pair
本题是leetcode121这道题的翻版,做法完全一样,也是扫一遍数组,维护两个值,一个是a[i]+i的最大值,另一个是a[i]+a[j]+i-j的最大值. class Solution: def m ...
- 【LeetCode】456. 132 Pattern 解题报告(Python)
[LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 6个最佳的开源Python应用服务器
6个最佳的开源Python应用服务器 首先,你知道什么是应用服务器吗?应用服务器通常被描述为是存在于服务器中心架构中间层的一个软件框架. AD: 首先,你知道什么是应用服务器吗?应用服务器通常被描述为 ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
随机推荐
- UE4 C++工程以Game模式启动
UE4版本:4.24.3源码编译版本 Windows10 + VS2019环境 UE4 C++工程,默认情况下VS中直接运行是启动Editor模式: 有时为了调试等目的需要以Game模式启动,可以避免 ...
- 商业创新不能等?用友低代码开发平台YonBuilder为您加速!
随着云计算.人工智能.物联网.大数据.5G等新一代技术的快速发展,越来越多的企业希望借助技术的力量加速数智化转型,期许通过更加敏捷和强大的应用系统推动企业的商业创新速度.但传统软件开发周期长.开发成本 ...
- 学习java的第二十天
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
- academy
academy at/in school都行,academy一般用at. The word comes from the Academy in ancient Greece, which derive ...
- Hbase(6)【Java Api Phoenix操作Hbase】
目录 两种方式操作Phoenix 1.Thick Driver 2.Thin Driver 3.注意事项 两种方式操作Phoenix 官网:http://phoenix.apache.org/faq. ...
- LinkBinTree
package ch11; import java.util.ArrayList; import java.util.List; import java.util.Stack; public clas ...
- OpenStack之八: network服务(端口9696)
注意此处用的一个网络,暂时不用启动第二个网官网地址 https://docs.openstack.org/neutron/stein/install/controller-install-rdo.ht ...
- File类及常用操作方法
import java.io.File; import java.io.IOException; public class file { public static void main(String[ ...
- 锁对象-条件对象-synchronized关键字
1 import java.util.concurrent.locks.Condition; 2 import java.util.concurrent.locks.Lock; 3 import ja ...
- 使用jquery刷新页面以及javascript的一些基本函数
如何使用jquery刷新当前页面 下面介绍全页面刷新方法:有时候可能会用到 1.window.location.reload()刷新当前页面. 2.parent.location.reload()刷新 ...