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 ...
随机推荐
- centos6安装自带php
一.Centos 6安装自带PHP CentOS 默认仓库中包含了php套件,我们可以直接使用yum安装.下面是最小化安装,我们使用php-fpm来解析php. yum install -y php- ...
- async.series
[async.series] series适用于顺序执行异步且前后无关联的调用.对于顺序执行异步且前后有叛逆的调用,则需要使用waterfall. If any functions in the se ...
- javascript学习笔记(七):事件详解
HTML事件处理 <!DOCTYPE html> <html> <head lang="en"> <meta chaset="U ...
- day24 面向对象三大特性之封装
本周内容 组合 封装 多态 面向对象高级 异常处理 网络协议 通讯原理 互联网协议 TCP/UDP 基于TCP协议的套接字 上周回顾 1.xml,os,os.path 2.ATM+购物车 三层结构 3 ...
- pta l2-6(树的遍历)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题意:给出一个二叉树的结点数目n, ...
- 树形DP(记忆化搜索) HYSBZ - 1509
题目链接:https://vjudge.net/problem/HYSBZ-1509 我参考的证明的论文:8.陈瑜希<多角度思考 创造性思维>_百度文库 https://wenku.ba ...
- Vue之数据监听存在的问题
Vue之数据监听 当数据监听的是列表时,数据发生改变,不会被监听到. // 用$set修改数组中的数组能够被监听 // app.$set(this.hobby, 0, "爱你哦") ...
- TOJ3112: 单词串串烧(回溯)
传送门(<--可以点击的) 时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte 描述 “单词串串烧”是一款拼词智力游戏,给定4*4的方格,随机取16个 ...
- http://www.bugku.com:Bugku——备份是个好习惯(http://120.24.86.145:8002/web16/)
看了bugku的这道题,陌生又熟悉. 题目首先说[备份是个好习惯],访问网站只有一串字符,,,,,emmmmm,这句话表明人家经常做备份,所以咯,肯定在网站哪里备份有网页信息.嘻嘻 1 ...
- 安装routeos
直接开机,会看到: 选择全部安装即可,按a.i即可. 默认账号admin,默认没有密码 基本使用 可通过/ip,/interface等可以进去不同子功能模块,可进行print,add,remove等操 ...