1014. Best Sightseeing Pair - 最佳观光组合

https://leetcode-cn.com/problems/best-sightseeing-pair/

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

 

暴力超时法。。。:

public int getScore(int[] A,int i, int j){
        return A[i]+A[j]+i-j;
    }
    public int maxScoreSightseeingPair(int[] A) {
        if(A == null) return 0;
   
        int biggestScore = 0;
        for(int i = 0; i < A.length - 1; i++){
            for(int j = i+1 ; j < A.length; j++){
                int num = getScore(A,i,j);
                if(biggestScore < num)biggestScore = num;
            }
        }
        return biggestScore;
    }

优化一:对于观光点得分A[i]+A[j]+i-j,考虑到A[i]与i、A[j]与j 值固定,可以将式子拆分为 A[i]+i 与 A[j]-j ,简化成一层for循环:

public int maxScoreSightseeingPair(int[] A) {
        if(A == null) return 0;
        int biggestScore = 0;
        //初始化第一次循环数据,设定maxI为i组合的最大值
        int maxI = A[0] + 0;
    
        for (int j = 1; j < A.length; ++j) {
            int tempJ = A[j] - j;
            //遍历出 j组合+maxI的最大值 赋值给biggestScore
            biggestScore = Math.max(biggestScore, maxI + tempJ);
            //更新i组合的值
            maxI = Math.max(maxI, A[j] + j);
        }
        return biggestScore;
    }

优化二:对于观光点得分A[i]+A[j]+i-j,对于i-j,在遍历j时,随着j的增大(i - j)每次-1, i-j\epsilon \sqsubset -A.length,0\sqsupset;

    public int maxScoreSightseeingPair(int[] A) {
        int biggestScore = 0;
        int maxI = 0;
        for (int tempJ : A) {
            biggestScore = Math.max(biggestScore, maxI + tempJ);
            //maxI值为maxI'-1 或tempJ-1,如果初始的maxI已经是最大值,遍历只需要不断-1即可;
            //若之后出现更大的值,直接跳到对应位置,从该位置重新进行-1操作;
            //直到找到距离maxI近(减的值少)且tempJ大的j:biggestScore = maxI - n + tempJ
            maxI = Math.max(maxI, tempJ) - 1;
        }
        return biggestScore;
    }

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值