1021. 最佳观光组合

 显示英文描述

 
  • 用户通过次数91
  • 用户尝试次数246
  • 通过次数92
  • 提交次数619
  • 题目难度Medium

给定正整数数组 AA[i] 表示第 i 个观光景点的评分,并且两个景点 i 和 j 之间的距离为 j - i

一对景点(i < j)组成的观光组合的得分为(A[i] + A[j] + i - j):景点的评分之和减去它们两者之间的距离。

返回一对观光景点能取得的最高分。

示例:

输入:[8,1,5,2,6]
输出:11
解释:i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11

提示:

  1. 2 <= A.length <= 50000
  2. 1 <= A[i] <= 1000
class Solution {
public:
//A[i] + i + A[j] - j
int maxScoreSightseeingPair(vector<int>& A) {
vector<int> vec(A.size(),);
vec[] = A[];
for(int i=;i < A.size();i++){
vec[i] = max(vec[i-],A[i]+i); //vec[i] 在i以及i之前和的最大值
}
int ans = INT_MIN;
for(int j=;j < A.size();j++){
ans = max(ans,vec[j-]+A[j]-j);
}
return ans;
}
};

_

Leetcode 1021. 最佳观光组合的更多相关文章

  1. [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 ...

  2. 【LeetCode】1021. Best Sightseeing Pair 最佳观光组合(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【JavaScript】Leetcode每日一题-组合总和4

    [JavaScript]Leetcode每日一题-组合总和4 [题目描述] 给你一个由 不同 整数组成的数组 nums ,和一个目标整数 target .请你从 nums 中找出并返回总和为 targ ...

  4. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. LeetCode 77 Combinations(排列组合)

    题目链接:https://leetcode.com/problems/combinations/#/description    Problem:给两个正数分别为n和k,求出从1,2.......n这 ...

  7. 【LeetCode】Combination Sum(组合总和)

    这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...

  8. LeetCode 1021. 删除最外层的括号(Remove Outermost Parentheses)

    1021. 删除最外层的括号 1021. Remove Outermost Parentheses 题目描述 有效括号字符串为空 ("")."(" + A + ...

  9. [LeetCode] 77. Combinations 全组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

随机推荐

  1. LightOJ 1258 Making Huge Palindromes(KMP)

    题意 给定一个字符串 \(S\) ,一次操作可以在这个字符串的右边增加任意一个字符.求操作之后的最短字符串,满足操作结束后的字符串是回文. \(1 \leq |S| \leq 10^6\) 思路 \( ...

  2. js url参数和对象互转

    function param(a) { var s = [], rbracket = /\[\]$/, isArray = function(obj) { return Object.prototyp ...

  3. 小程序学习一 .json 文件配置

    微信小程序——配置 以下就是小编对小程序配置的资料进行的系统的整理,希望能对开发者有帮助. 我们使用app.json文件来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置 ...

  4. mysql Percona-XtraBackup backup

    全量备份FULL BACKUP https://www.percona.com/doc/percona-xtrabackup/2.4/backup_scenarios/full_backup.html ...

  5. 内连接查询输出到datagridView

    实现步骤: 1. 新建两张对应表的类 例如: 第一张表对应的类 { class ManagerInfo { public Table1 group { get; set; } //重点 需要内连接的字 ...

  6. 使用JDBC从数据库中查询数据的方法

    * ResultSet 结果集:封装了使用JDBC 进行查询的结果 * 1. 调用Statement 对象的 executeQuery(sql) 方法可以得到结果集 * 2. ResultSet 返回 ...

  7. linux c/c++ 获取文件大小

    linux c/c++ 获取文件大小 #include <sys/stat.h> int FileSize(const char* fname) { struct stat statbuf ...

  8. 00-python-内置函数笔记

    01.enumerate()函数用于将一个可遍历的数据对象(如 列表.元组或字符串)组合为一个索引序列,同时列出数据和数据包下标,一般用在for循环中 for i, element in enumer ...

  9. 使用spss做方差分析

    还记得上学那会老师专门敲了黑板,强调方差分析很重要..单因素方差分析(Analysis of Variance, ANOVA),如果变量多,就是多因素方差分析,还需要考虑到多重共线性, 也就是线性代数 ...

  10. django-simple-captcha 验证码插件介绍 django-simple-captcha 使用 以及添加动态ajax刷新验证

    django-simple-captcha作为一款django的验证码插件,使用方法非常简单,能够快速应用到web应用中. 文档官网地址:django-simple-captcha 参考博客:http ...