leetcode 798. Smallest Rotation with Highest Score(java)

leetcode798. Smallest Rotation with Highest Score(java)


原题地址:https://leetcode.com/problems/smallest-rotation-with-highest-score/

题目

Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1], A{K+2], … A[A.length - 1], A[0], A[1], …, A[K-1]. Afterward, any entries that are less than or equal to their index are worth 1 point.

For example, if we have [2, 4, 1, 3, 0], and we rotate by K = 2, it becomes [1, 3, 0, 2, 4]. This is worth 3 points because 1 > 0 [no points], 3 > 1 [no points], 0 <= 2 [one point], 2 <= 3 [one point], 4 <= 4 [one point].

Over all possible rotations, return the rotation index K that corresponds to the highest score we could receive. If there are multiple answers, return the smallest such index K.

Example 1:
Input: [2, 3, 1, 4, 0]
Output: 3
Explanation:  
Scores for each K are listed below: 
K = 0,  A = [2,3,1,4,0],    score 1
K = 1,  A = [3,1,4,0,2],    score 3
K = 2,  A = [1,4,0,2,3],    score 3
K = 3,  A = [4,0,2,3,1],    score 4
K = 4,  A = [0,2,3,1,4],    score 3
So we should choose K = 3, which has the highest score.
Example 2:
Input: [1, 3, 0, 2, 4]
Output: 0
Explanation:  A will always have 3 points no matter how it shifts.
So we will choose the smallest K, which is 0.

Note:

  • A will have length at most 20000.
  • A[i] will be in the range [0, A.length]

注意

题目的Example 1中,K = 0, A = [2,3,1,4,0], score 1,应该为 score 2

python3超时代码(TLE)

#该代码通过了26/36的测试数据
class Solution:
    def bestRotation(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        K = 0
        score_end = 0
        length = len(A) 
        score = [0 for _ in range(length)]
        for i in range(length):
            value = A[i]
            index = (i - value + length) % length
            for j in range(length - value):
                score[index] += 1
                index -= 1
                if index < 0:
                    index = length - 1

        for i in range(length):
            if score[i] > score_end:
                K, score_end = i, score[i]

        return K

java代码(850ms)

/*
 *这种方法的时间复杂度为O(n^2),但是这道题本质是要找一个O(n)的算法,在代码中尽量减少时间的
 *耗费,很幸运的可以通过。python用这种方法就会TLE超时,T^T想不出O(n)的做法。 
 */

class Solution {
    public int bestRotation(int[] A) {
        int K = 0;
        int tempScore = 0;
        int len = A.length;
        int[] score = new int[len];   //每个K对应的得分数组
        for (int i = 0; i < len; i ++) {
            int value = A[i];
            int index = (i - value + len) % len;
            //计算len-value次
            for (int j = 0; j < len - value; j ++) {
                score[index]++;
                index--;
                if(index < 0) 
                    index = len - 1;
            }
        }
        /*
        取出数组中得分最高的索引,即K值
        */
        for (int i = 0; i < len; i ++) {
            if (score[i] > tempScore) {
                K = i;
                tempScore = score[i];
            }
        }

        return K;
    }
}

版权声明:转载注明 http://blog.csdn.net/birdreamer/article/details/79518860

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值