leetcode1014
这道题暴力算法,会超时:
class Solution(object):
def maxScoreSightseeingPair(self, A: 'List[int]') -> int:
n = len(A)
maxsum = 0
for i in range(n):
for j in range(i+1,n):
cursum = A[i] + A[j] + i - j
maxsum = max(maxsum,cursum)
return maxsum
因此,需要使用动态规划解决:
class Solution(object):
def maxScoreSightseeingPair(self, A: 'List[int]') -> int:
n = len(A)
ans = 0
pre = A[0]
for i in range(1,n):
pre = max(pre,A[i-1]+i-1)
ans = max(ans,A[i]+pre-i)
return ans
leetcode1014的更多相关文章
- [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 ...
随机推荐
- Hadoop概念学习系列之Java调用Shell命令和脚本,致力于hadoop/spark集群(三十六)
前言 说明的是,本博文,是在以下的博文基础上,立足于它们,致力于我的大数据领域! http://kongcodecenter.iteye.com/blog/1231177 http://blog.cs ...
- Apollo分布式配置中心部署以及使用
一.简介Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用于微服务配置管理场 ...
- Java-Runoob-高级教程-实例-方法:06. Java 实例 – 方法覆盖
ylbtech-Java-Runoob-高级教程-实例-方法:06. Java 实例 – 方法覆盖 1.返回顶部 1. Java 实例 - 方法覆盖 Java 实例 前面章节中我们已经学习了 Jav ...
- [转][layui]table 的一个BUG
转换静态表格,一直只能显示 10 行,研究发现解决方法有两个:1.参数里: limit: 30, 添加参数以确保显示更多行2.修改 table.js 里面的 F.prototype.config ,添 ...
- lucene索引查看工具luke和文本提取工具Tika
luke可以方便的查看lucene的索引信息,当然也可以查看solr和es中的索引信息(基于lucene实现). 查看索引前,要注意lucene版本的问题,高版本的lucene用低版本的luke工具就 ...
- android 将项目下的数据库拷贝到sd卡中
/** * 将项目下的数据库拷贝到sd卡中 */ public static boolean copyDbToSdCard() { FileInputStream fis = null; FileOu ...
- Git配置SSH Key
最近看到很多人在配置Git时,遇到很问题,网上教程千篇一律.这儿自己单独记录一份. Git配置SSH Key 1.检查本机是否有ssh key设置,切换到.ssh目录 $ cd ~/.ssh 或cd ...
- CentOS用户和用户组管理
groupadd grptest1 按照系统默认的gid创建组.根uid一样,gid也是从1000开始的. groupadd -g 1008 grptest2 创建gid=1008的用户组:g ...
- windows 查询文件被什么程序占用
运行Resmon CPU选项卡全选 在[关联的句柄]里查询: 需要的时间挺多的...
- DB通用类:SQL Server 通用类库
SQLServer通用类A using System; using System.Data; using System.Data.SqlClient; using System.Collections ...