Best Sightseeing Pair LT1014
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
Idea 1. max(A[i] + A[j] + i -j) (i < j) = max(max(A[i] + i) + A[j] -j), scan from left to right
Time complexity: O(n)
Space complexity: O(1)
class Solution {
public int maxScoreSightseeingPair(int[] A) {
int maxScore = Integer.MIN_VALUE;
int sumVal = Integer.MIN_VALUE;
for(int i = 1; i < A.length; ++i) {
sumVal = Math.max(sumVal, A[i-1] + (i-1));
maxScore = Math.max(maxScore, A[i] - i + sumVal);
} return maxScore;
}
}
Idea 1.b scan from right to left, max(A[i] + A[j] + i -j) (i < j) = max(max(A[j] -j) + A[i] + i)
class Solution {
public int maxScoreSightseeingPair(int[] A) {
int maxScore = Integer.MIN_VALUE;
int sumVal = Integer.MIN_VALUE;
for(int i = A.length-2; i >= 0; --i) {
sumVal = Math.max(sumVal, A[i+1] - (i+1));
maxScore = Math.max(maxScore, A[i] + i + sumVal);
} return maxScore;
}
}
Best Sightseeing Pair LT1014的更多相关文章
- [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】1021. Best Sightseeing Pair 最佳观光组合(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode 1014. Best Sightseeing Pair
本题是leetcode121这道题的翻版,做法完全一样,也是扫一遍数组,维护两个值,一个是a[i]+i的最大值,另一个是a[i]+a[j]+i-j的最大值. class Solution: def m ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- Weekly Contest 129
1020. Partition Array Into Three Parts With Equal Sum Given an array A of integers, return true if a ...
- HDU 1688 Sightseeing&HDU 3191 How Many Paths Are There(Dijkstra变形求次短路条数)
Sightseeing Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- POJ 3621 Sightseeing Cows(最优比例环+SPFA检测)
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10306 Accepted: 3519 ...
- POJ 4046 Sightseeing
Sightseeing Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID ...
随机推荐
- kangle请求控制添加的add_header怎么查看
请求控制里添加的add header不会显示在浏览器的请求里,因为是发送给源的,你们要查看可以用phinfo查看.回应控制里添加的会显示在浏览器的回应里
- javascript学习笔记(三):运算符、循环语句
javascript的运算符.条件语句.循环语句的使用方法大部分和c语言类似,但是值得注意的是,运算符中"=="和"==="的使用方法和c语言有区别:在java ...
- windows上java中文乱码-指定字符集 -Dfile.encoding=UTF-8
jvm启动中增加参数: -Dfile.encoding=UTF-8 重启即可.
- Jmeter+Ant+Jenkins实现接口自动化(转载)
转载自 http://www.cnblogs.com/chengtch/p/6145867.html 本文转载于上面的网址,稍作修改,实用性更强. Jmeter是压力测试.接口测试工具,Ant是基于 ...
- 【MongoDB】关于无法连接mongo的问题
今天使用MongoDB的时候发现直接输入mongo提示连接失败 首先想到的可能是服务还没启动 当我随便打开一个cmd输入net start MongoDB 提示:net start mongodb 发 ...
- 纯css3棋盘图案背景以及45度斜纹背景
css代码 .stripes { height: 250px; width: 375px; float: left; margin: 10px; ...
- postman使用方法
Postman sending requests 打开Postman,可以看到界面分成左右两个部分,右边是我们后头要讲的collection,左边是现在要讲的request builder.在requ ...
- layui复选框
效果图 layui复选框,一个主的复选框控制多个从复选框,主复选框和从复选框的颜色不一样 layui复选框的样式,都是在选然后才会有的,所以直接通过css设置就实现不了了.只可以通过js动态设置 ht ...
- HDU-1257.最少拦截系统(基础DP)
本题大意:给出n和n个整数,让你求出其中不上升子序列的个数. 本题思路:用dp[ i ]保存第i个防御系统攻击的最低的导弹,遍历数组,遇到更低的导弹则更新最小值,否则新加一个系统用来防御,并且更新最小 ...
- php的三种CLI常量:STDIN,STDOUT,STDERR
PHP CLI(command line interface)中,有三个系统常量,分别是STDIN.STDOUT.STDERR,代表文件句柄. 应用一: <?php while($line = ...