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 ...
随机推荐
- ALGO-123_蓝桥杯_算法训练_A+B problem
问题描述 Given two integers A and B, your task is to output their sum, A+B. 输入格式 The input contains of o ...
- C/C++基础----重载运算与类型转换
非成员版本 data1 + data2: operator+(data1, data2); 成员版本 data1 += data2: data1.operator+=(data2); 不建议的重载 逻 ...
- 《Effective Java》笔记-服务提供者框架
静态工厂方法返回的对象所属的类,在编写包含该静态工厂方法的类时可以不必存在.这种灵活的静态工厂方法构成了服务提供者框架(Service Provider Framework)的基础,例如JDBC AP ...
- Java-Runoob-高级教程-实例-方法:11. Java 实例 – enum 和 switch 语句使用
ylbtech-Java-Runoob-高级教程-实例-方法:11. Java 实例 – enum 和 switch 语句使用 1.返回顶部 1. Java 实例 - enum 和 switch 语句 ...
- 学习笔记之Python调试 - pdb
python调试神器——pdb - 软谋python https://mp.weixin.qq.com/s/w3Xw8I_zh7MFq2dx5kdQXw 优秀开发者必备技能包:Python调试器 - ...
- openstack热添加磁盘
假定在虚拟机当中添加了磁盘,但是虚拟机没有识别出来:如何识别出来 可以使用命令 echo '- - -' >/sys/class/scsi_host/host0/scan 使用后就可以识别出来了 ...
- Scrapy学习篇(四)之数据存储
上一篇中,我们简单的实现了toscrapy网页信息的爬取,并存储到mongo,本篇文章信息看看数据的存储.这一篇主要是实现信息的存储,我们以将信息保存到文件和mongo数据库为例,学习数据的存储,依然 ...
- centos7.0KVM虚拟化
需要在图形化界面完成实验 查看CPU信息 # cat /proc/cpuinfo centos7默认安装的虚拟化包 # yum list open*tools 如果没安装就安装 #yum instal ...
- 经典算法冒泡排序java版
写个冒泡排序吧 冒泡排序(Bubble Sort)是一种典型的交换排序算法,通过交换数据元素的位置进行排序. public class BubbleSort{ public int[] bubbleS ...
- String MVC @RequestParam(required=false) int XXX 参数为空报错解决方法
今天在用@RequestParam(required=false) int XXX 取参数的时候,当参数没有的时候Spring默认赋值为空.而此时使用基本类型int,所以报错,建议使用包装类 Inte ...